blob: 7353933483bb32683a2fe303563efe5ace4c0e80 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Robert Peterson7ae8fa82007-05-09 09:37:57 -05003 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
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 Whitehousefd88de562006-05-05 16:59:11 -040016#include <linux/pagevec.h>
Steven Whitehouse9b124fb2006-01-30 11:55:32 +000017#include <linux/mpage.h>
Steven Whitehoused1665e42006-02-14 11:54:42 +000018#include <linux/fs.h>
Steven Whitehousea8d638e2007-01-15 13:52:17 +000019#include <linux/writeback.h>
Steven Whitehouse7765ec22007-10-16 01:25:07 -070020#include <linux/swap.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include <linux/gfs2_ondisk.h>
Fabio Massimo Di Nitto7d308592006-09-19 07:56:29 +020022#include <linux/lm_interface.h>
Steven Whitehouse47e83b52007-10-18 11:15:50 +010023#include <linux/backing-dev.h>
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +010024#include <linux/pagevec.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000025
26#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050027#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028#include "bmap.h"
29#include "glock.h"
30#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000031#include "log.h"
32#include "meta_io.h"
33#include "ops_address.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000034#include "quota.h"
35#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000036#include "rgrp.h"
Robert Petersoncd81a4b2007-05-14 12:42:18 -050037#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050038#include "util.h"
Steven Whitehouse4340fe62006-07-11 09:46:33 -040039#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000040
Steven Whitehouseba7f7292006-07-26 11:27:10 -040041
42static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
43 unsigned int from, unsigned int to)
44{
45 struct buffer_head *head = page_buffers(page);
46 unsigned int bsize = head->b_size;
47 struct buffer_head *bh;
48 unsigned int start, end;
49
50 for (bh = head, start = 0; bh != head || !start;
51 bh = bh->b_this_page, start = end) {
52 end = start + bsize;
53 if (end <= from || start >= to)
54 continue;
Benjamin Marzinskiddf4b422007-06-01 14:21:38 -050055 if (gfs2_is_jdata(ip))
56 set_buffer_uptodate(bh);
Steven Whitehouseba7f7292006-07-26 11:27:10 -040057 gfs2_trans_add_bh(ip->i_gl, bh, 0);
58 }
59}
60
David Teiglandb3b94fa2006-01-16 16:50:04 +000061/**
Steven Whitehouse4ff14672006-01-30 09:39:10 +000062 * gfs2_get_block - Fills in a buffer head with details about a block
David Teiglandb3b94fa2006-01-16 16:50:04 +000063 * @inode: The inode
64 * @lblock: The block number to look up
65 * @bh_result: The buffer head to return the result in
66 * @create: Non-zero if we may add block to the file
67 *
68 * Returns: errno
69 */
70
Steven Whitehouse4ff14672006-01-30 09:39:10 +000071int gfs2_get_block(struct inode *inode, sector_t lblock,
72 struct buffer_head *bh_result, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +000073{
Steven Whitehouse23591252006-10-13 17:25:45 -040074 return gfs2_block_map(inode, lblock, create, bh_result);
David Teiglandb3b94fa2006-01-16 16:50:04 +000075}
76
77/**
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -040078 * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
David Teiglandb3b94fa2006-01-16 16:50:04 +000079 * @inode: The inode
80 * @lblock: The block number to look up
81 * @bh_result: The buffer head to return the result in
82 * @create: Non-zero if we may add block to the file
83 *
84 * Returns: errno
85 */
86
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -040087static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
88 struct buffer_head *bh_result, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +000089{
David Teiglandb3b94fa2006-01-16 16:50:04 +000090 int error;
91
Steven Whitehouse23591252006-10-13 17:25:45 -040092 error = gfs2_block_map(inode, lblock, 0, bh_result);
David Teiglandb3b94fa2006-01-16 16:50:04 +000093 if (error)
94 return error;
Wendy Chengde986e82007-09-18 09:19:13 -040095 if (!buffer_mapped(bh_result))
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -040096 return -EIO;
Steven Whitehouse623d9352006-08-31 12:14:44 -040097 return 0;
98}
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -040099
100static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
101 struct buffer_head *bh_result, int create)
102{
Steven Whitehouse23591252006-10-13 17:25:45 -0400103 return gfs2_block_map(inode, lblock, 0, bh_result);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400104}
105
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106/**
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100107 * gfs2_writepage_common - Common bits of writepage
108 * @page: The page to be written
109 * @wbc: The writeback control
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110 *
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100111 * Returns: 1 if writepage is ok, otherwise an error code or zero if no error.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112 */
113
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100114static int gfs2_writepage_common(struct page *page,
115 struct writeback_control *wbc)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000117 struct inode *inode = page->mapping->host;
Steven Whitehousef4387142006-08-08 13:23:19 -0400118 struct gfs2_inode *ip = GFS2_I(inode);
119 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000120 loff_t i_size = i_size_read(inode);
121 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
122 unsigned offset;
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100123 int ret = -EIO;
124
125 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
126 goto out;
127 ret = 0;
128 if (current->journal_info)
129 goto redirty;
130 /* Is the page fully outside i_size? (truncate in progress) */
131 offset = i_size & (PAGE_CACHE_SIZE-1);
132 if (page->index > end_index || (page->index == end_index && !offset)) {
133 page->mapping->a_ops->invalidatepage(page, 0);
134 goto out;
135 }
136 return 1;
137redirty:
138 redirty_page_for_writepage(wbc, page);
139out:
140 unlock_page(page);
141 return 0;
142}
143
144/**
145 * gfs2_writeback_writepage - Write page for writeback mappings
146 * @page: The page
147 * @wbc: The writeback control
148 *
149 */
150
151static int gfs2_writeback_writepage(struct page *page,
152 struct writeback_control *wbc)
153{
154 int ret;
155
156 ret = gfs2_writepage_common(page, wbc);
157 if (ret <= 0)
158 return ret;
159
160 ret = mpage_writepage(page, gfs2_get_block_noalloc, wbc);
161 if (ret == -EAGAIN)
162 ret = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
163 return ret;
164}
165
166/**
167 * gfs2_ordered_writepage - Write page for ordered data files
168 * @page: The page to write
169 * @wbc: The writeback control
170 *
171 */
172
173static int gfs2_ordered_writepage(struct page *page,
174 struct writeback_control *wbc)
175{
176 struct inode *inode = page->mapping->host;
177 struct gfs2_inode *ip = GFS2_I(inode);
178 int ret;
179
180 ret = gfs2_writepage_common(page, wbc);
181 if (ret <= 0)
182 return ret;
183
184 if (!page_has_buffers(page)) {
185 create_empty_buffers(page, inode->i_sb->s_blocksize,
186 (1 << BH_Dirty)|(1 << BH_Uptodate));
187 }
188 gfs2_page_add_databufs(ip, page, 0, inode->i_sb->s_blocksize-1);
189 return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
190}
191
192/**
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100193 * __gfs2_jdata_writepage - The core of jdata writepage
194 * @page: The page to write
195 * @wbc: The writeback control
196 *
197 * This is shared between writepage and writepages and implements the
198 * core of the writepage operation. If a transaction is required then
199 * PageChecked will have been set and the transaction will have
200 * already been started before this is called.
201 */
202
203static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
204{
205 struct inode *inode = page->mapping->host;
206 struct gfs2_inode *ip = GFS2_I(inode);
207 struct gfs2_sbd *sdp = GFS2_SB(inode);
208
209 if (PageChecked(page)) {
210 ClearPageChecked(page);
211 if (!page_has_buffers(page)) {
212 create_empty_buffers(page, inode->i_sb->s_blocksize,
213 (1 << BH_Dirty)|(1 << BH_Uptodate));
214 }
215 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
216 }
217 return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
218}
219
220/**
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100221 * gfs2_jdata_writepage - Write complete page
222 * @page: Page to write
223 *
224 * Returns: errno
225 *
226 */
227
228static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
229{
230 struct inode *inode = page->mapping->host;
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100231 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232 int error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000233 int done_trans = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100235 error = gfs2_writepage_common(page, wbc);
236 if (error <= 0)
237 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000238
Steven Whitehousebf36a712007-10-17 08:35:19 +0100239 if (PageChecked(page)) {
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100240 if (wbc->sync_mode != WB_SYNC_ALL)
241 goto out_ignore;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000242 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
243 if (error)
244 goto out_ignore;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000245 done_trans = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246 }
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100247 error = __gfs2_jdata_writepage(page, wbc);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000248 if (done_trans)
249 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000251
252out_ignore:
253 redirty_page_for_writepage(wbc, page);
254 unlock_page(page);
255 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256}
257
258/**
Steven Whitehouse55610932007-10-17 08:47:38 +0100259 * gfs2_writeback_writepages - Write a bunch of dirty pages back to disk
Steven Whitehousea8d638e2007-01-15 13:52:17 +0000260 * @mapping: The mapping to write
261 * @wbc: Write-back control
262 *
Steven Whitehouse55610932007-10-17 08:47:38 +0100263 * For the data=writeback case we can already ignore buffer heads
Steven Whitehousea8d638e2007-01-15 13:52:17 +0000264 * and write whole extents at once. This is a big reduction in the
265 * number of I/O requests we send and the bmap calls we make in this case.
266 */
Steven Whitehouse55610932007-10-17 08:47:38 +0100267static int gfs2_writeback_writepages(struct address_space *mapping,
268 struct writeback_control *wbc)
Steven Whitehousea8d638e2007-01-15 13:52:17 +0000269{
Steven Whitehouse55610932007-10-17 08:47:38 +0100270 return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
Steven Whitehousea8d638e2007-01-15 13:52:17 +0000271}
272
273/**
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +0100274 * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
275 * @mapping: The mapping
276 * @wbc: The writeback control
277 * @writepage: The writepage function to call for each page
278 * @pvec: The vector of pages
279 * @nr_pages: The number of pages to write
280 *
281 * Returns: non-zero if loop should terminate, zero otherwise
282 */
283
284static int gfs2_write_jdata_pagevec(struct address_space *mapping,
285 struct writeback_control *wbc,
286 struct pagevec *pvec,
287 int nr_pages, pgoff_t end)
288{
289 struct inode *inode = mapping->host;
290 struct gfs2_sbd *sdp = GFS2_SB(inode);
291 loff_t i_size = i_size_read(inode);
292 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
293 unsigned offset = i_size & (PAGE_CACHE_SIZE-1);
294 unsigned nrblocks = nr_pages * (PAGE_CACHE_SIZE/inode->i_sb->s_blocksize);
295 struct backing_dev_info *bdi = mapping->backing_dev_info;
296 int i;
297 int ret;
298
299 ret = gfs2_trans_begin(sdp, nrblocks, 0);
300 if (ret < 0)
301 return ret;
302
303 for(i = 0; i < nr_pages; i++) {
304 struct page *page = pvec->pages[i];
305
306 lock_page(page);
307
308 if (unlikely(page->mapping != mapping)) {
309 unlock_page(page);
310 continue;
311 }
312
313 if (!wbc->range_cyclic && page->index > end) {
314 ret = 1;
315 unlock_page(page);
316 continue;
317 }
318
319 if (wbc->sync_mode != WB_SYNC_NONE)
320 wait_on_page_writeback(page);
321
322 if (PageWriteback(page) ||
323 !clear_page_dirty_for_io(page)) {
324 unlock_page(page);
325 continue;
326 }
327
328 /* Is the page fully outside i_size? (truncate in progress) */
329 if (page->index > end_index || (page->index == end_index && !offset)) {
330 page->mapping->a_ops->invalidatepage(page, 0);
331 unlock_page(page);
332 continue;
333 }
334
335 ret = __gfs2_jdata_writepage(page, wbc);
336
337 if (ret || (--(wbc->nr_to_write) <= 0))
338 ret = 1;
339 if (wbc->nonblocking && bdi_write_congested(bdi)) {
340 wbc->encountered_congestion = 1;
341 ret = 1;
342 }
343
344 }
345 gfs2_trans_end(sdp);
346 return ret;
347}
348
349/**
350 * gfs2_write_cache_jdata - Like write_cache_pages but different
351 * @mapping: The mapping to write
352 * @wbc: The writeback control
353 * @writepage: The writepage function to call
354 * @data: The data to pass to writepage
355 *
356 * The reason that we use our own function here is that we need to
357 * start transactions before we grab page locks. This allows us
358 * to get the ordering right.
359 */
360
361static int gfs2_write_cache_jdata(struct address_space *mapping,
362 struct writeback_control *wbc)
363{
364 struct backing_dev_info *bdi = mapping->backing_dev_info;
365 int ret = 0;
366 int done = 0;
367 struct pagevec pvec;
368 int nr_pages;
369 pgoff_t index;
370 pgoff_t end;
371 int scanned = 0;
372 int range_whole = 0;
373
374 if (wbc->nonblocking && bdi_write_congested(bdi)) {
375 wbc->encountered_congestion = 1;
376 return 0;
377 }
378
379 pagevec_init(&pvec, 0);
380 if (wbc->range_cyclic) {
381 index = mapping->writeback_index; /* Start from prev offset */
382 end = -1;
383 } else {
384 index = wbc->range_start >> PAGE_CACHE_SHIFT;
385 end = wbc->range_end >> PAGE_CACHE_SHIFT;
386 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
387 range_whole = 1;
388 scanned = 1;
389 }
390
391retry:
392 while (!done && (index <= end) &&
393 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
394 PAGECACHE_TAG_DIRTY,
395 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
396 scanned = 1;
397 ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, end);
398 if (ret)
399 done = 1;
400 if (ret > 0)
401 ret = 0;
402
403 pagevec_release(&pvec);
404 cond_resched();
405 }
406
407 if (!scanned && !done) {
408 /*
409 * We hit the last page and there is more work to be done: wrap
410 * back to the start of the file
411 */
412 scanned = 1;
413 index = 0;
414 goto retry;
415 }
416
417 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
418 mapping->writeback_index = index;
419 return ret;
420}
421
422
423/**
424 * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
425 * @mapping: The mapping to write
426 * @wbc: The writeback control
427 *
428 */
429
430static int gfs2_jdata_writepages(struct address_space *mapping,
431 struct writeback_control *wbc)
432{
433 struct gfs2_inode *ip = GFS2_I(mapping->host);
434 struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
435 int ret;
436
437 ret = gfs2_write_cache_jdata(mapping, wbc);
438 if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
439 gfs2_log_flush(sdp, ip->i_gl);
440 ret = gfs2_write_cache_jdata(mapping, wbc);
441 }
442 return ret;
443}
444
445/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000446 * stuffed_readpage - Fill in a Linux page with stuffed file data
447 * @ip: the inode
448 * @page: the page
449 *
450 * Returns: errno
451 */
452
453static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
454{
455 struct buffer_head *dibh;
456 void *kaddr;
457 int error;
458
Steven Whitehousebf126ae2007-04-20 09:18:30 +0100459 /*
460 * Due to the order of unstuffing files and ->nopage(), we can be
461 * asked for a zero page in the case of a stuffed file being extended,
462 * so we need to supply one here. It doesn't happen often.
463 */
464 if (unlikely(page->index)) {
Steven Whitehouse28405012007-06-18 16:31:42 +0100465 zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
Steven Whitehousebf126ae2007-04-20 09:18:30 +0100466 return 0;
467 }
Steven Whitehousefd88de562006-05-05 16:59:11 -0400468
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469 error = gfs2_meta_inode_buffer(ip, &dibh);
470 if (error)
471 return error;
472
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000473 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400474 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000475 ip->i_di.di_size);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400476 memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
Russell Cattelanc312c4f2006-10-12 09:23:41 -0400477 kunmap_atomic(kaddr, KM_USER0);
Steven Whitehousebf126ae2007-04-20 09:18:30 +0100478 flush_dcache_page(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 SetPageUptodate(page);
481
482 return 0;
483}
484
David Teiglandb3b94fa2006-01-16 16:50:04 +0000485
486/**
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100487 * __gfs2_readpage - readpage
488 * @file: The file to read a page for
David Teiglandb3b94fa2006-01-16 16:50:04 +0000489 * @page: The page to read
490 *
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100491 * This is the core of gfs2's readpage. Its used by the internal file
492 * reading code as in that case we already hold the glock. Also its
493 * called by gfs2_readpage() once the required lock has been granted.
494 *
495 */
496
497static int __gfs2_readpage(void *file, struct page *page)
498{
499 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
500 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
501 int error;
502
503 if (gfs2_is_stuffed(ip)) {
504 error = stuffed_readpage(ip, page);
505 unlock_page(page);
506 } else {
507 error = mpage_readpage(page, gfs2_get_block);
508 }
509
510 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
511 return -EIO;
512
513 return error;
514}
515
516/**
517 * gfs2_readpage - read a page of a file
518 * @file: The file to read
519 * @page: The page of the file
520 *
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100521 * This deals with the locking required. We use a trylock in order to
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100522 * avoid the page lock / glock ordering problems returning AOP_TRUNCATED_PAGE
523 * in the event that we are unable to get the lock.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000524 */
525
526static int gfs2_readpage(struct file *file, struct page *page)
527{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400528 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000529 struct gfs2_holder gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000530 int error;
531
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100532 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh);
533 error = gfs2_glock_nq_atime(&gh);
534 if (unlikely(error)) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400535 unlock_page(page);
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100536 goto out;
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000537 }
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100538 error = __gfs2_readpage(file, page);
539 gfs2_glock_dq(&gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000540out:
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100541 gfs2_holder_uninit(&gh);
Josef Whitera13cbe32007-02-23 12:49:51 -0500542 if (error == GLR_TRYFAILED) {
Josef Whitera13cbe32007-02-23 12:49:51 -0500543 yield();
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100544 return AOP_TRUNCATED_PAGE;
Josef Whitera13cbe32007-02-23 12:49:51 -0500545 }
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100546 return error;
547}
548
549/**
550 * gfs2_internal_read - read an internal file
551 * @ip: The gfs2 inode
552 * @ra_state: The readahead state (or NULL for no readahead)
553 * @buf: The buffer to fill
554 * @pos: The file position
555 * @size: The amount to read
556 *
557 */
558
559int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
560 char *buf, loff_t *pos, unsigned size)
561{
562 struct address_space *mapping = ip->i_inode.i_mapping;
563 unsigned long index = *pos / PAGE_CACHE_SIZE;
564 unsigned offset = *pos & (PAGE_CACHE_SIZE - 1);
565 unsigned copied = 0;
566 unsigned amt;
567 struct page *page;
568 void *p;
569
570 do {
571 amt = size - copied;
572 if (offset + size > PAGE_CACHE_SIZE)
573 amt = PAGE_CACHE_SIZE - offset;
574 page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
575 if (IS_ERR(page))
576 return PTR_ERR(page);
577 p = kmap_atomic(page, KM_USER0);
578 memcpy(buf + copied, p + offset, amt);
579 kunmap_atomic(p, KM_USER0);
580 mark_page_accessed(page);
581 page_cache_release(page);
582 copied += amt;
583 index++;
584 offset = 0;
585 } while(copied < size);
586 (*pos) += size;
587 return size;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400588}
589
Steven Whitehousefd88de562006-05-05 16:59:11 -0400590/**
591 * gfs2_readpages - Read a bunch of pages at once
592 *
593 * Some notes:
594 * 1. This is only for readahead, so we can simply ignore any things
595 * which are slightly inconvenient (such as locking conflicts between
596 * the page lock and the glock) and return having done no I/O. Its
597 * obviously not something we'd want to do on too regular a basis.
598 * Any I/O we ignore at this time will be done via readpage later.
Steven Whitehousee1d5b182006-12-15 16:49:51 -0500599 * 2. We don't handle stuffed files here we let readpage do the honours.
Steven Whitehousefd88de562006-05-05 16:59:11 -0400600 * 3. mpage_readpages() does most of the heavy lifting in the common case.
601 * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
Steven Whitehousefd88de562006-05-05 16:59:11 -0400602 */
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100603
Steven Whitehousefd88de562006-05-05 16:59:11 -0400604static int gfs2_readpages(struct file *file, struct address_space *mapping,
605 struct list_head *pages, unsigned nr_pages)
606{
607 struct inode *inode = mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400608 struct gfs2_inode *ip = GFS2_I(inode);
609 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400610 struct gfs2_holder gh;
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100611 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400612
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100613 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100614 ret = gfs2_glock_nq_atime(&gh);
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100615 if (unlikely(ret))
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100616 goto out_uninit;
Steven Whitehousee1d5b182006-12-15 16:49:51 -0500617 if (!gfs2_is_stuffed(ip))
Steven Whitehousefd88de562006-05-05 16:59:11 -0400618 ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100619 gfs2_glock_dq(&gh);
620out_uninit:
621 gfs2_holder_uninit(&gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400622 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
623 ret = -EIO;
624 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625}
626
627/**
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700628 * gfs2_write_begin - Begin to write to a file
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629 * @file: The file to write to
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700630 * @mapping: The mapping in which to write
631 * @pos: The file offset at which to start writing
632 * @len: Length of the write
633 * @flags: Various flags
634 * @pagep: Pointer to return the page
635 * @fsdata: Pointer to return fs data (unused by GFS2)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000636 *
637 * Returns: errno
638 */
639
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700640static int gfs2_write_begin(struct file *file, struct address_space *mapping,
641 loff_t pos, unsigned len, unsigned flags,
642 struct page **pagep, void **fsdata)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000643{
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700644 struct gfs2_inode *ip = GFS2_I(mapping->host);
645 struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000646 unsigned int data_blocks, ind_blocks, rblocks;
647 int alloc_required;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000648 int error = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000649 struct gfs2_alloc *al;
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700650 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
651 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
652 unsigned to = from + len;
653 struct page *page;
Russell Cattelan52ae7b72006-10-09 12:11:54 -0500654
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700655 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
Steven Whitehousedcd24792006-11-16 11:08:16 -0500656 error = gfs2_glock_nq_atime(&ip->i_gh);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700657 if (unlikely(error))
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000658 goto out_uninit;
659
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700660 gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700661 error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
662 if (error)
Steven Whitehousec41d4f02007-10-17 14:05:41 +0100663 goto out_unlock;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000664
Steven Whitehousef5c54802006-10-10 13:45:15 -0400665 ip->i_alloc.al_requested = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000666 if (alloc_required) {
667 al = gfs2_alloc_get(ip);
668
669 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
670 if (error)
671 goto out_alloc_put;
672
Steven Whitehouse2933f922006-11-01 13:23:29 -0500673 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000674 if (error)
675 goto out_qunlock;
676
677 al->al_requested = data_blocks + ind_blocks;
678 error = gfs2_inplace_reserve(ip);
679 if (error)
680 goto out_qunlock;
681 }
682
683 rblocks = RES_DINODE + ind_blocks;
684 if (gfs2_is_jdata(ip))
685 rblocks += data_blocks ? data_blocks : 1;
686 if (ind_blocks || data_blocks)
687 rblocks += RES_STATFS + RES_QUOTA;
688
Steven Whitehouse16615be2007-09-17 10:59:52 +0100689 error = gfs2_trans_begin(sdp, rblocks,
690 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000691 if (error)
Steven Whitehousea867bb22007-07-17 10:29:02 +0100692 goto out_trans_fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000693
Steven Whitehousec41d4f02007-10-17 14:05:41 +0100694 error = -ENOMEM;
695 page = __grab_cache_page(mapping, index);
696 *pagep = page;
697 if (unlikely(!page))
698 goto out_endtrans;
699
David Teiglandb3b94fa2006-01-16 16:50:04 +0000700 if (gfs2_is_stuffed(ip)) {
Steven Whitehousec41d4f02007-10-17 14:05:41 +0100701 error = 0;
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700702 if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400703 error = gfs2_unstuff_dinode(ip, page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000704 if (error == 0)
705 goto prepare_write;
Steven Whitehousec41d4f02007-10-17 14:05:41 +0100706 } else if (!PageUptodate(page)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000707 error = stuffed_readpage(ip, page);
Steven Whitehousec41d4f02007-10-17 14:05:41 +0100708 }
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000709 goto out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000710 }
711
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000712prepare_write:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000713 error = block_prepare_write(page, from, to, gfs2_get_block);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000714out:
Steven Whitehousec41d4f02007-10-17 14:05:41 +0100715 if (error == 0)
716 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000717
Steven Whitehousec41d4f02007-10-17 14:05:41 +0100718 page_cache_release(page);
719 if (pos + len > ip->i_inode.i_size)
720 vmtruncate(&ip->i_inode, ip->i_inode.i_size);
721out_endtrans:
722 gfs2_trans_end(sdp);
723out_trans_fail:
724 if (alloc_required) {
725 gfs2_inplace_release(ip);
726out_qunlock:
727 gfs2_quota_unlock(ip);
728out_alloc_put:
729 gfs2_alloc_put(ip);
730 }
731out_unlock:
732 gfs2_glock_dq(&ip->i_gh);
733out_uninit:
734 gfs2_holder_uninit(&ip->i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000735 return error;
736}
737
738/**
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500739 * adjust_fs_space - Adjusts the free space available due to gfs2_grow
740 * @inode: the rindex inode
741 */
742static void adjust_fs_space(struct inode *inode)
743{
744 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
745 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
746 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
747 u64 fs_total, new_free;
748
749 /* Total up the file system space, according to the latest rindex. */
750 fs_total = gfs2_ri_total(sdp);
751
752 spin_lock(&sdp->sd_statfs_spin);
753 if (fs_total > (m_sc->sc_total + l_sc->sc_total))
754 new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
755 else
756 new_free = 0;
757 spin_unlock(&sdp->sd_statfs_spin);
Robert Peterson6c532672007-05-10 16:54:38 -0500758 fs_warn(sdp, "File system extended by %llu blocks.\n",
759 (unsigned long long)new_free);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500760 gfs2_statfs_change(sdp, new_free, new_free, 0);
761}
762
763/**
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700764 * gfs2_stuffed_write_end - Write end for stuffed files
765 * @inode: The inode
766 * @dibh: The buffer_head containing the on-disk inode
767 * @pos: The file position
768 * @len: The length of the write
769 * @copied: How much was actually copied by the VFS
770 * @page: The page
771 *
772 * This copies the data from the page into the inode block after
773 * the inode data structure itself.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 *
775 * Returns: errno
776 */
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700777static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
778 loff_t pos, unsigned len, unsigned copied,
779 struct page *page)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000780{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400781 struct gfs2_inode *ip = GFS2_I(inode);
782 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700783 u64 to = pos + copied;
784 void *kaddr;
785 unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
786 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000787
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700788 BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
789 kaddr = kmap_atomic(page, KM_USER0);
790 memcpy(buf + pos, kaddr + pos, copied);
791 memset(kaddr + pos + copied, 0, len - copied);
792 flush_dcache_page(page);
793 kunmap_atomic(kaddr, KM_USER0);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000794
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700795 if (!PageUptodate(page))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000796 SetPageUptodate(page);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700797 unlock_page(page);
798 page_cache_release(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000799
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700800 if (inode->i_size < to) {
801 i_size_write(inode, to);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000802 ip->i_di.di_size = inode->i_size;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400803 di->di_size = cpu_to_be64(inode->i_size);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700804 mark_inode_dirty(inode);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400805 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000806
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500807 if (inode == sdp->sd_rindex)
808 adjust_fs_space(inode);
809
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000810 brelse(dibh);
811 gfs2_trans_end(sdp);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700812 gfs2_glock_dq(&ip->i_gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000813 gfs2_holder_uninit(&ip->i_gh);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700814 return copied;
815}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000816
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700817/**
818 * gfs2_write_end
819 * @file: The file to write to
820 * @mapping: The address space to write to
821 * @pos: The file position
822 * @len: The length of the data
823 * @copied:
824 * @page: The page that has been written
825 * @fsdata: The fsdata (unused in GFS2)
826 *
827 * The main write_end function for GFS2. We have a separate one for
828 * stuffed files as they are slightly different, otherwise we just
829 * put our locking around the VFS provided functions.
830 *
831 * Returns: errno
832 */
833
834static int gfs2_write_end(struct file *file, struct address_space *mapping,
835 loff_t pos, unsigned len, unsigned copied,
836 struct page *page, void *fsdata)
837{
838 struct inode *inode = page->mapping->host;
839 struct gfs2_inode *ip = GFS2_I(inode);
840 struct gfs2_sbd *sdp = GFS2_SB(inode);
841 struct buffer_head *dibh;
842 struct gfs2_alloc *al = &ip->i_alloc;
843 struct gfs2_dinode *di;
844 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
845 unsigned int to = from + len;
846 int ret;
847
848 BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == 0);
849
850 ret = gfs2_meta_inode_buffer(ip, &dibh);
851 if (unlikely(ret)) {
852 unlock_page(page);
853 page_cache_release(page);
854 goto failed;
855 }
856
857 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
858
859 if (gfs2_is_stuffed(ip))
860 return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
861
Steven Whitehousebf36a712007-10-17 08:35:19 +0100862 if (!gfs2_is_writeback(ip))
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700863 gfs2_page_add_databufs(ip, page, from, to);
864
865 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
866
867 if (likely(ret >= 0)) {
868 copied = ret;
869 if ((pos + copied) > inode->i_size) {
870 di = (struct gfs2_dinode *)dibh->b_data;
871 ip->i_di.di_size = inode->i_size;
872 di->di_size = cpu_to_be64(inode->i_size);
873 mark_inode_dirty(inode);
874 }
875 }
876
877 if (inode == sdp->sd_rindex)
878 adjust_fs_space(inode);
879
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000880 brelse(dibh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000881 gfs2_trans_end(sdp);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700882failed:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000883 if (al->al_requested) {
884 gfs2_inplace_release(ip);
885 gfs2_quota_unlock(ip);
886 gfs2_alloc_put(ip);
887 }
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700888 gfs2_glock_dq(&ip->i_gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000889 gfs2_holder_uninit(&ip->i_gh);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700890 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000891}
892
893/**
Robert Peterson8fb68592007-06-12 11:24:36 -0500894 * gfs2_set_page_dirty - Page dirtying function
895 * @page: The page to dirty
896 *
897 * Returns: 1 if it dirtyed the page, or 0 otherwise
898 */
899
900static int gfs2_set_page_dirty(struct page *page)
901{
Steven Whitehouse55610932007-10-17 08:47:38 +0100902 SetPageChecked(page);
Robert Peterson8fb68592007-06-12 11:24:36 -0500903 return __set_page_dirty_buffers(page);
904}
905
906/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907 * gfs2_bmap - Block map function
908 * @mapping: Address space info
909 * @lblock: The block to map
910 *
911 * Returns: The disk address for the block or 0 on hole or error
912 */
913
914static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
915{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400916 struct gfs2_inode *ip = GFS2_I(mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 struct gfs2_holder i_gh;
918 sector_t dblock = 0;
919 int error;
920
David Teiglandb3b94fa2006-01-16 16:50:04 +0000921 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
922 if (error)
923 return 0;
924
925 if (!gfs2_is_stuffed(ip))
Steven Whitehouse4ff14672006-01-30 09:39:10 +0000926 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000927
928 gfs2_glock_dq_uninit(&i_gh);
929
930 return dblock;
931}
932
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100933static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
934{
935 struct gfs2_bufdata *bd;
936
937 lock_buffer(bh);
938 gfs2_log_lock(sdp);
939 clear_buffer_dirty(bh);
940 bd = bh->b_private;
941 if (bd) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100942 if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
943 list_del_init(&bd->bd_le.le_list);
944 else
945 gfs2_remove_from_journal(bh, current->journal_info, 0);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100946 }
947 bh->b_bdev = NULL;
948 clear_buffer_mapped(bh);
949 clear_buffer_req(bh);
950 clear_buffer_new(bh);
951 gfs2_log_unlock(sdp);
952 unlock_buffer(bh);
953}
954
Steven Whitehouse8628de02006-03-31 16:48:41 -0500955static void gfs2_invalidatepage(struct page *page, unsigned long offset)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956{
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100957 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
958 struct buffer_head *bh, *head;
959 unsigned long pos = 0;
960
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961 BUG_ON(!PageLocked(page));
Robert Peterson8fb68592007-06-12 11:24:36 -0500962 if (offset == 0)
963 ClearPageChecked(page);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100964 if (!page_has_buffers(page))
965 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100967 bh = head = page_buffers(page);
968 do {
969 if (offset <= pos)
970 gfs2_discard(sdp, bh);
971 pos += bh->b_size;
972 bh = bh->b_this_page;
973 } while (bh != head);
974out:
975 if (offset == 0)
976 try_to_release_page(page, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977}
978
Steven Whitehousec7b33832006-12-14 18:24:26 +0000979/**
980 * gfs2_ok_for_dio - check that dio is valid on this file
981 * @ip: The inode
982 * @rw: READ or WRITE
983 * @offset: The offset at which we are reading or writing
984 *
985 * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
986 * 1 (to accept the i/o request)
987 */
988static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
989{
990 /*
991 * Should we return an error here? I can't see that O_DIRECT for
Steven Whitehouse55610932007-10-17 08:47:38 +0100992 * a stuffed file makes any sense. For now we'll silently fall
993 * back to buffered I/O
Steven Whitehousec7b33832006-12-14 18:24:26 +0000994 */
Steven Whitehousec7b33832006-12-14 18:24:26 +0000995 if (gfs2_is_stuffed(ip))
996 return 0;
997
998 if (offset > i_size_read(&ip->i_inode))
999 return 0;
1000 return 1;
1001}
1002
1003
1004
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -04001005static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
1006 const struct iovec *iov, loff_t offset,
1007 unsigned long nr_segs)
Steven Whitehoused1665e42006-02-14 11:54:42 +00001008{
1009 struct file *file = iocb->ki_filp;
1010 struct inode *inode = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001011 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehoused1665e42006-02-14 11:54:42 +00001012 struct gfs2_holder gh;
1013 int rv;
1014
1015 /*
Steven Whitehousec7b33832006-12-14 18:24:26 +00001016 * Deferred lock, even if its a write, since we do no allocation
1017 * on this path. All we need change is atime, and this lock mode
1018 * ensures that other nodes have flushed their buffered read caches
1019 * (i.e. their page cache entries for this inode). We do not,
1020 * unfortunately have the option of only flushing a range like
1021 * the VFS does.
Steven Whitehoused1665e42006-02-14 11:54:42 +00001022 */
Steven Whitehousec7b33832006-12-14 18:24:26 +00001023 gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh);
Steven Whitehousedcd24792006-11-16 11:08:16 -05001024 rv = gfs2_glock_nq_atime(&gh);
Steven Whitehoused1665e42006-02-14 11:54:42 +00001025 if (rv)
Steven Whitehousec7b33832006-12-14 18:24:26 +00001026 return rv;
1027 rv = gfs2_ok_for_dio(ip, rw, offset);
1028 if (rv != 1)
1029 goto out; /* dio not valid, fall back to buffered i/o */
Steven Whitehoused1665e42006-02-14 11:54:42 +00001030
Steven Whitehousec7b33832006-12-14 18:24:26 +00001031 rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
1032 iov, offset, nr_segs,
1033 gfs2_get_block_direct, NULL);
Steven Whitehoused1665e42006-02-14 11:54:42 +00001034out:
1035 gfs2_glock_dq_m(1, &gh);
1036 gfs2_holder_uninit(&gh);
Steven Whitehoused1665e42006-02-14 11:54:42 +00001037 return rv;
1038}
1039
1040/**
Steven Whitehouse623d9352006-08-31 12:14:44 -04001041 * gfs2_releasepage - free the metadata associated with a page
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001042 * @page: the page that's being released
1043 * @gfp_mask: passed from Linux VFS, ignored by us
1044 *
1045 * Call try_to_free_buffers() if the buffers in this page can be
1046 * released.
1047 *
1048 * Returns: 0
1049 */
1050
1051int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
1052{
1053 struct inode *aspace = page->mapping->host;
1054 struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
1055 struct buffer_head *bh, *head;
1056 struct gfs2_bufdata *bd;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001057
1058 if (!page_has_buffers(page))
Steven Whitehouse891ba6d2007-09-20 15:26:33 +01001059 return 0;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001060
Steven Whitehousebb3b0e32007-08-16 16:03:57 +01001061 gfs2_log_lock(sdp);
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001062 head = bh = page_buffers(page);
1063 do {
Steven Whitehousebb3b0e32007-08-16 16:03:57 +01001064 if (atomic_read(&bh->b_count))
1065 goto cannot_release;
1066 bd = bh->b_private;
1067 if (bd && bd->bd_ail)
1068 goto cannot_release;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001069 gfs2_assert_warn(sdp, !buffer_pinned(bh));
Steven Whitehouse623d9352006-08-31 12:14:44 -04001070 gfs2_assert_warn(sdp, !buffer_dirty(bh));
Steven Whitehousebb3b0e32007-08-16 16:03:57 +01001071 bh = bh->b_this_page;
1072 } while(bh != head);
1073 gfs2_log_unlock(sdp);
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001074
Steven Whitehousebb3b0e32007-08-16 16:03:57 +01001075 head = bh = page_buffers(page);
1076 do {
Steven Whitehouse623d9352006-08-31 12:14:44 -04001077 gfs2_log_lock(sdp);
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001078 bd = bh->b_private;
1079 if (bd) {
1080 gfs2_assert_warn(sdp, bd->bd_bh == bh);
1081 gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
Steven Whitehoused7b616e2007-09-02 10:48:13 +01001082 if (!list_empty(&bd->bd_le.le_list)) {
1083 if (!buffer_pinned(bh))
1084 list_del_init(&bd->bd_le.le_list);
1085 else
1086 bd = NULL;
1087 }
1088 if (bd)
1089 bd->bd_bh = NULL;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001090 bh->b_private = NULL;
1091 }
Steven Whitehouse623d9352006-08-31 12:14:44 -04001092 gfs2_log_unlock(sdp);
1093 if (bd)
1094 kmem_cache_free(gfs2_bufdata_cachep, bd);
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001095
1096 bh = bh->b_this_page;
Steven Whitehouse166afcc2006-08-24 15:59:40 -04001097 } while (bh != head);
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001098
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001099 return try_to_free_buffers(page);
Steven Whitehousebb3b0e32007-08-16 16:03:57 +01001100cannot_release:
1101 gfs2_log_unlock(sdp);
1102 return 0;
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001103}
1104
Steven Whitehouse55610932007-10-17 08:47:38 +01001105static const struct address_space_operations gfs2_writeback_aops = {
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +01001106 .writepage = gfs2_writeback_writepage,
Steven Whitehouse55610932007-10-17 08:47:38 +01001107 .writepages = gfs2_writeback_writepages,
1108 .readpage = gfs2_readpage,
1109 .readpages = gfs2_readpages,
1110 .sync_page = block_sync_page,
1111 .write_begin = gfs2_write_begin,
1112 .write_end = gfs2_write_end,
1113 .bmap = gfs2_bmap,
1114 .invalidatepage = gfs2_invalidatepage,
1115 .releasepage = gfs2_releasepage,
1116 .direct_IO = gfs2_direct_IO,
1117};
1118
1119static const struct address_space_operations gfs2_ordered_aops = {
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +01001120 .writepage = gfs2_ordered_writepage,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001121 .readpage = gfs2_readpage,
Steven Whitehousefd88de562006-05-05 16:59:11 -04001122 .readpages = gfs2_readpages,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123 .sync_page = block_sync_page,
Steven Whitehouse7765ec22007-10-16 01:25:07 -07001124 .write_begin = gfs2_write_begin,
1125 .write_end = gfs2_write_end,
Robert Peterson8fb68592007-06-12 11:24:36 -05001126 .set_page_dirty = gfs2_set_page_dirty,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001127 .bmap = gfs2_bmap,
1128 .invalidatepage = gfs2_invalidatepage,
Steven Whitehouse4340fe62006-07-11 09:46:33 -04001129 .releasepage = gfs2_releasepage,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001130 .direct_IO = gfs2_direct_IO,
1131};
1132
Steven Whitehouse55610932007-10-17 08:47:38 +01001133static const struct address_space_operations gfs2_jdata_aops = {
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +01001134 .writepage = gfs2_jdata_writepage,
Steven Whitehouseb8e7cbb2007-10-17 09:04:24 +01001135 .writepages = gfs2_jdata_writepages,
Steven Whitehouse55610932007-10-17 08:47:38 +01001136 .readpage = gfs2_readpage,
1137 .readpages = gfs2_readpages,
1138 .sync_page = block_sync_page,
1139 .write_begin = gfs2_write_begin,
1140 .write_end = gfs2_write_end,
1141 .set_page_dirty = gfs2_set_page_dirty,
1142 .bmap = gfs2_bmap,
1143 .invalidatepage = gfs2_invalidatepage,
1144 .releasepage = gfs2_releasepage,
1145};
1146
1147void gfs2_set_aops(struct inode *inode)
1148{
1149 struct gfs2_inode *ip = GFS2_I(inode);
1150
1151 if (gfs2_is_writeback(ip))
1152 inode->i_mapping->a_ops = &gfs2_writeback_aops;
1153 else if (gfs2_is_ordered(ip))
1154 inode->i_mapping->a_ops = &gfs2_ordered_aops;
1155 else if (gfs2_is_jdata(ip))
1156 inode->i_mapping->a_ops = &gfs2_jdata_aops;
1157 else
1158 BUG();
1159}
1160