blob: 4bf73ed945ae0e8242fe8c16af1797039b756e60 [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 Whitehouse51ff87b2007-10-15 14:42:35 +010023#include <linux/swap.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000024
25#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050026#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000027#include "bmap.h"
28#include "glock.h"
29#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030#include "log.h"
31#include "meta_io.h"
32#include "ops_address.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000033#include "quota.h"
34#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000035#include "rgrp.h"
Robert Petersoncd81a4b2007-05-14 12:42:18 -050036#include "super.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050037#include "util.h"
Steven Whitehouse4340fe62006-07-11 09:46:33 -040038#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000039
Steven Whitehouseba7f7292006-07-26 11:27:10 -040040
41static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
42 unsigned int from, unsigned int to)
43{
44 struct buffer_head *head = page_buffers(page);
45 unsigned int bsize = head->b_size;
46 struct buffer_head *bh;
47 unsigned int start, end;
48
49 for (bh = head, start = 0; bh != head || !start;
50 bh = bh->b_this_page, start = end) {
51 end = start + bsize;
52 if (end <= from || start >= to)
53 continue;
Benjamin Marzinskiddf4b422007-06-01 14:21:38 -050054 if (gfs2_is_jdata(ip))
55 set_buffer_uptodate(bh);
Steven Whitehouseba7f7292006-07-26 11:27:10 -040056 gfs2_trans_add_bh(ip->i_gl, bh, 0);
57 }
58}
59
David Teiglandb3b94fa2006-01-16 16:50:04 +000060/**
Steven Whitehouse4ff14672006-01-30 09:39:10 +000061 * gfs2_get_block - Fills in a buffer head with details about a block
David Teiglandb3b94fa2006-01-16 16:50:04 +000062 * @inode: The inode
63 * @lblock: The block number to look up
64 * @bh_result: The buffer head to return the result in
65 * @create: Non-zero if we may add block to the file
66 *
67 * Returns: errno
68 */
69
Steven Whitehouse4ff14672006-01-30 09:39:10 +000070int gfs2_get_block(struct inode *inode, sector_t lblock,
71 struct buffer_head *bh_result, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +000072{
Steven Whitehouse23591252006-10-13 17:25:45 -040073 return gfs2_block_map(inode, lblock, create, bh_result);
David Teiglandb3b94fa2006-01-16 16:50:04 +000074}
75
76/**
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -040077 * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
David Teiglandb3b94fa2006-01-16 16:50:04 +000078 * @inode: The inode
79 * @lblock: The block number to look up
80 * @bh_result: The buffer head to return the result in
81 * @create: Non-zero if we may add block to the file
82 *
83 * Returns: errno
84 */
85
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -040086static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
87 struct buffer_head *bh_result, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +000088{
David Teiglandb3b94fa2006-01-16 16:50:04 +000089 int error;
90
Steven Whitehouse23591252006-10-13 17:25:45 -040091 error = gfs2_block_map(inode, lblock, 0, bh_result);
David Teiglandb3b94fa2006-01-16 16:50:04 +000092 if (error)
93 return error;
Wendy Chengde986e82007-09-18 09:19:13 -040094 if (!buffer_mapped(bh_result))
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -040095 return -EIO;
Steven Whitehouse623d9352006-08-31 12:14:44 -040096 return 0;
97}
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -040098
99static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
100 struct buffer_head *bh_result, int create)
101{
Steven Whitehouse23591252006-10-13 17:25:45 -0400102 return gfs2_block_map(inode, lblock, 0, bh_result);
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400103}
104
David Teiglandb3b94fa2006-01-16 16:50:04 +0000105/**
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100106 * gfs2_writepage_common - Common bits of writepage
107 * @page: The page to be written
108 * @wbc: The writeback control
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109 *
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100110 * Returns: 1 if writepage is ok, otherwise an error code or zero if no error.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111 */
112
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100113static int gfs2_writepage_common(struct page *page,
114 struct writeback_control *wbc)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000116 struct inode *inode = page->mapping->host;
Steven Whitehousef4387142006-08-08 13:23:19 -0400117 struct gfs2_inode *ip = GFS2_I(inode);
118 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000119 loff_t i_size = i_size_read(inode);
120 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
121 unsigned offset;
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100122 int ret = -EIO;
123
124 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
125 goto out;
126 ret = 0;
127 if (current->journal_info)
128 goto redirty;
129 /* Is the page fully outside i_size? (truncate in progress) */
130 offset = i_size & (PAGE_CACHE_SIZE-1);
131 if (page->index > end_index || (page->index == end_index && !offset)) {
132 page->mapping->a_ops->invalidatepage(page, 0);
133 goto out;
134 }
135 return 1;
136redirty:
137 redirty_page_for_writepage(wbc, page);
138out:
139 unlock_page(page);
140 return 0;
141}
142
143/**
144 * gfs2_writeback_writepage - Write page for writeback mappings
145 * @page: The page
146 * @wbc: The writeback control
147 *
148 */
149
150static int gfs2_writeback_writepage(struct page *page,
151 struct writeback_control *wbc)
152{
153 int ret;
154
155 ret = gfs2_writepage_common(page, wbc);
156 if (ret <= 0)
157 return ret;
158
159 ret = mpage_writepage(page, gfs2_get_block_noalloc, wbc);
160 if (ret == -EAGAIN)
161 ret = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
162 return ret;
163}
164
165/**
166 * gfs2_ordered_writepage - Write page for ordered data files
167 * @page: The page to write
168 * @wbc: The writeback control
169 *
170 */
171
172static int gfs2_ordered_writepage(struct page *page,
173 struct writeback_control *wbc)
174{
175 struct inode *inode = page->mapping->host;
176 struct gfs2_inode *ip = GFS2_I(inode);
177 int ret;
178
179 ret = gfs2_writepage_common(page, wbc);
180 if (ret <= 0)
181 return ret;
182
183 if (!page_has_buffers(page)) {
184 create_empty_buffers(page, inode->i_sb->s_blocksize,
185 (1 << BH_Dirty)|(1 << BH_Uptodate));
186 }
187 gfs2_page_add_databufs(ip, page, 0, inode->i_sb->s_blocksize-1);
188 return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
189}
190
191/**
192 * gfs2_jdata_writepage - Write complete page
193 * @page: Page to write
194 *
195 * Returns: errno
196 *
197 */
198
199static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
200{
201 struct inode *inode = page->mapping->host;
202 struct gfs2_inode *ip = GFS2_I(inode);
203 struct gfs2_sbd *sdp = GFS2_SB(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000204 int error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000205 int done_trans = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100207 error = gfs2_writepage_common(page, wbc);
208 if (error <= 0)
209 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000210
Steven Whitehousebf36a712007-10-17 08:35:19 +0100211 if (PageChecked(page)) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000212 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
213 if (error)
214 goto out_ignore;
Steven Whitehousebf36a712007-10-17 08:35:19 +0100215 ClearPageChecked(page);
Steven Whitehousef4387142006-08-08 13:23:19 -0400216 if (!page_has_buffers(page)) {
217 create_empty_buffers(page, inode->i_sb->s_blocksize,
218 (1 << BH_Dirty)|(1 << BH_Uptodate));
219 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000220 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
221 done_trans = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222 }
Steven Whitehouse7a6bbac2006-09-18 17:18:23 -0400223 error = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000224 if (done_trans)
225 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000226 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000227
228out_ignore:
229 redirty_page_for_writepage(wbc, page);
230 unlock_page(page);
231 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232}
233
234/**
Steven Whitehouse55610932007-10-17 08:47:38 +0100235 * gfs2_writeback_writepages - Write a bunch of dirty pages back to disk
Steven Whitehousea8d638e2007-01-15 13:52:17 +0000236 * @mapping: The mapping to write
237 * @wbc: Write-back control
238 *
Steven Whitehouse55610932007-10-17 08:47:38 +0100239 * For the data=writeback case we can already ignore buffer heads
Steven Whitehousea8d638e2007-01-15 13:52:17 +0000240 * and write whole extents at once. This is a big reduction in the
241 * number of I/O requests we send and the bmap calls we make in this case.
242 */
Steven Whitehouse55610932007-10-17 08:47:38 +0100243static int gfs2_writeback_writepages(struct address_space *mapping,
244 struct writeback_control *wbc)
Steven Whitehousea8d638e2007-01-15 13:52:17 +0000245{
Steven Whitehouse55610932007-10-17 08:47:38 +0100246 return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
Steven Whitehousea8d638e2007-01-15 13:52:17 +0000247}
248
249/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000250 * stuffed_readpage - Fill in a Linux page with stuffed file data
251 * @ip: the inode
252 * @page: the page
253 *
254 * Returns: errno
255 */
256
257static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
258{
259 struct buffer_head *dibh;
260 void *kaddr;
261 int error;
262
Steven Whitehousebf126ae2007-04-20 09:18:30 +0100263 /*
264 * Due to the order of unstuffing files and ->nopage(), we can be
265 * asked for a zero page in the case of a stuffed file being extended,
266 * so we need to supply one here. It doesn't happen often.
267 */
268 if (unlikely(page->index)) {
Steven Whitehouse28405012007-06-18 16:31:42 +0100269 zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
Steven Whitehousebf126ae2007-04-20 09:18:30 +0100270 return 0;
271 }
Steven Whitehousefd88de562006-05-05 16:59:11 -0400272
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273 error = gfs2_meta_inode_buffer(ip, &dibh);
274 if (error)
275 return error;
276
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000277 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400278 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279 ip->i_di.di_size);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400280 memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
Russell Cattelanc312c4f2006-10-12 09:23:41 -0400281 kunmap_atomic(kaddr, KM_USER0);
Steven Whitehousebf126ae2007-04-20 09:18:30 +0100282 flush_dcache_page(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283 brelse(dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000284 SetPageUptodate(page);
285
286 return 0;
287}
288
David Teiglandb3b94fa2006-01-16 16:50:04 +0000289
290/**
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100291 * __gfs2_readpage - readpage
292 * @file: The file to read a page for
David Teiglandb3b94fa2006-01-16 16:50:04 +0000293 * @page: The page to read
294 *
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100295 * This is the core of gfs2's readpage. Its used by the internal file
296 * reading code as in that case we already hold the glock. Also its
297 * called by gfs2_readpage() once the required lock has been granted.
298 *
299 */
300
301static int __gfs2_readpage(void *file, struct page *page)
302{
303 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
304 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
305 int error;
306
307 if (gfs2_is_stuffed(ip)) {
308 error = stuffed_readpage(ip, page);
309 unlock_page(page);
310 } else {
311 error = mpage_readpage(page, gfs2_get_block);
312 }
313
314 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
315 return -EIO;
316
317 return error;
318}
319
320/**
321 * gfs2_readpage - read a page of a file
322 * @file: The file to read
323 * @page: The page of the file
324 *
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100325 * This deals with the locking required. We use a trylock in order to
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100326 * avoid the page lock / glock ordering problems returning AOP_TRUNCATED_PAGE
327 * in the event that we are unable to get the lock.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000328 */
329
330static int gfs2_readpage(struct file *file, struct page *page)
331{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400332 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000333 struct gfs2_holder gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334 int error;
335
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100336 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh);
337 error = gfs2_glock_nq_atime(&gh);
338 if (unlikely(error)) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400339 unlock_page(page);
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100340 goto out;
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000341 }
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100342 error = __gfs2_readpage(file, page);
343 gfs2_glock_dq(&gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000344out:
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100345 gfs2_holder_uninit(&gh);
Josef Whitera13cbe32007-02-23 12:49:51 -0500346 if (error == GLR_TRYFAILED) {
Josef Whitera13cbe32007-02-23 12:49:51 -0500347 yield();
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100348 return AOP_TRUNCATED_PAGE;
Josef Whitera13cbe32007-02-23 12:49:51 -0500349 }
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100350 return error;
351}
352
353/**
354 * gfs2_internal_read - read an internal file
355 * @ip: The gfs2 inode
356 * @ra_state: The readahead state (or NULL for no readahead)
357 * @buf: The buffer to fill
358 * @pos: The file position
359 * @size: The amount to read
360 *
361 */
362
363int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
364 char *buf, loff_t *pos, unsigned size)
365{
366 struct address_space *mapping = ip->i_inode.i_mapping;
367 unsigned long index = *pos / PAGE_CACHE_SIZE;
368 unsigned offset = *pos & (PAGE_CACHE_SIZE - 1);
369 unsigned copied = 0;
370 unsigned amt;
371 struct page *page;
372 void *p;
373
374 do {
375 amt = size - copied;
376 if (offset + size > PAGE_CACHE_SIZE)
377 amt = PAGE_CACHE_SIZE - offset;
378 page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
379 if (IS_ERR(page))
380 return PTR_ERR(page);
381 p = kmap_atomic(page, KM_USER0);
382 memcpy(buf + copied, p + offset, amt);
383 kunmap_atomic(p, KM_USER0);
384 mark_page_accessed(page);
385 page_cache_release(page);
386 copied += amt;
387 index++;
388 offset = 0;
389 } while(copied < size);
390 (*pos) += size;
391 return size;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400392}
393
Steven Whitehousefd88de562006-05-05 16:59:11 -0400394/**
395 * gfs2_readpages - Read a bunch of pages at once
396 *
397 * Some notes:
398 * 1. This is only for readahead, so we can simply ignore any things
399 * which are slightly inconvenient (such as locking conflicts between
400 * the page lock and the glock) and return having done no I/O. Its
401 * obviously not something we'd want to do on too regular a basis.
402 * Any I/O we ignore at this time will be done via readpage later.
Steven Whitehousee1d5b182006-12-15 16:49:51 -0500403 * 2. We don't handle stuffed files here we let readpage do the honours.
Steven Whitehousefd88de562006-05-05 16:59:11 -0400404 * 3. mpage_readpages() does most of the heavy lifting in the common case.
405 * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
Steven Whitehousefd88de562006-05-05 16:59:11 -0400406 */
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100407
Steven Whitehousefd88de562006-05-05 16:59:11 -0400408static int gfs2_readpages(struct file *file, struct address_space *mapping,
409 struct list_head *pages, unsigned nr_pages)
410{
411 struct inode *inode = mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400412 struct gfs2_inode *ip = GFS2_I(inode);
413 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400414 struct gfs2_holder gh;
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100415 int ret;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400416
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100417 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100418 ret = gfs2_glock_nq_atime(&gh);
Steven Whitehouse51ff87b2007-10-15 14:42:35 +0100419 if (unlikely(ret))
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100420 goto out_uninit;
Steven Whitehousee1d5b182006-12-15 16:49:51 -0500421 if (!gfs2_is_stuffed(ip))
Steven Whitehousefd88de562006-05-05 16:59:11 -0400422 ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100423 gfs2_glock_dq(&gh);
424out_uninit:
425 gfs2_holder_uninit(&gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400426 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
427 ret = -EIO;
428 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000429}
430
431/**
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700432 * gfs2_write_begin - Begin to write to a file
David Teiglandb3b94fa2006-01-16 16:50:04 +0000433 * @file: The file to write to
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700434 * @mapping: The mapping in which to write
435 * @pos: The file offset at which to start writing
436 * @len: Length of the write
437 * @flags: Various flags
438 * @pagep: Pointer to return the page
439 * @fsdata: Pointer to return fs data (unused by GFS2)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000440 *
441 * Returns: errno
442 */
443
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700444static int gfs2_write_begin(struct file *file, struct address_space *mapping,
445 loff_t pos, unsigned len, unsigned flags,
446 struct page **pagep, void **fsdata)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447{
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700448 struct gfs2_inode *ip = GFS2_I(mapping->host);
449 struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000450 unsigned int data_blocks, ind_blocks, rblocks;
451 int alloc_required;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000452 int error = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000453 struct gfs2_alloc *al;
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700454 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
455 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
456 unsigned to = from + len;
457 struct page *page;
Russell Cattelan52ae7b72006-10-09 12:11:54 -0500458
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700459 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
Steven Whitehousedcd24792006-11-16 11:08:16 -0500460 error = gfs2_glock_nq_atime(&ip->i_gh);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700461 if (unlikely(error))
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000462 goto out_uninit;
463
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700464 error = -ENOMEM;
465 page = __grab_cache_page(mapping, index);
466 *pagep = page;
467 if (!page)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000468 goto out_unlock;
469
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700470 gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
471
472 error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
473 if (error)
474 goto out_putpage;
475
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000476
Steven Whitehousef5c54802006-10-10 13:45:15 -0400477 ip->i_alloc.al_requested = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000478 if (alloc_required) {
479 al = gfs2_alloc_get(ip);
480
481 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
482 if (error)
483 goto out_alloc_put;
484
Steven Whitehouse2933f922006-11-01 13:23:29 -0500485 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000486 if (error)
487 goto out_qunlock;
488
489 al->al_requested = data_blocks + ind_blocks;
490 error = gfs2_inplace_reserve(ip);
491 if (error)
492 goto out_qunlock;
493 }
494
495 rblocks = RES_DINODE + ind_blocks;
496 if (gfs2_is_jdata(ip))
497 rblocks += data_blocks ? data_blocks : 1;
498 if (ind_blocks || data_blocks)
499 rblocks += RES_STATFS + RES_QUOTA;
500
Steven Whitehouse16615be2007-09-17 10:59:52 +0100501 error = gfs2_trans_begin(sdp, rblocks,
502 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000503 if (error)
Steven Whitehousea867bb22007-07-17 10:29:02 +0100504 goto out_trans_fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505
506 if (gfs2_is_stuffed(ip)) {
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700507 if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400508 error = gfs2_unstuff_dinode(ip, page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000509 if (error == 0)
510 goto prepare_write;
511 } else if (!PageUptodate(page))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 error = stuffed_readpage(ip, page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000513 goto out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000514 }
515
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000516prepare_write:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000517 error = block_prepare_write(page, from, to, gfs2_get_block);
518
519out:
520 if (error) {
521 gfs2_trans_end(sdp);
Steven Whitehousea867bb22007-07-17 10:29:02 +0100522out_trans_fail:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000523 if (alloc_required) {
524 gfs2_inplace_release(ip);
525out_qunlock:
526 gfs2_quota_unlock(ip);
527out_alloc_put:
528 gfs2_alloc_put(ip);
529 }
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700530out_putpage:
531 page_cache_release(page);
532 if (pos + len > ip->i_inode.i_size)
533 vmtruncate(&ip->i_inode, ip->i_inode.i_size);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000534out_unlock:
535 gfs2_glock_dq_m(1, &ip->i_gh);
536out_uninit:
537 gfs2_holder_uninit(&ip->i_gh);
538 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000539
540 return error;
541}
542
543/**
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500544 * adjust_fs_space - Adjusts the free space available due to gfs2_grow
545 * @inode: the rindex inode
546 */
547static void adjust_fs_space(struct inode *inode)
548{
549 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
550 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
551 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
552 u64 fs_total, new_free;
553
554 /* Total up the file system space, according to the latest rindex. */
555 fs_total = gfs2_ri_total(sdp);
556
557 spin_lock(&sdp->sd_statfs_spin);
558 if (fs_total > (m_sc->sc_total + l_sc->sc_total))
559 new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
560 else
561 new_free = 0;
562 spin_unlock(&sdp->sd_statfs_spin);
Robert Peterson6c532672007-05-10 16:54:38 -0500563 fs_warn(sdp, "File system extended by %llu blocks.\n",
564 (unsigned long long)new_free);
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500565 gfs2_statfs_change(sdp, new_free, new_free, 0);
566}
567
568/**
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700569 * gfs2_stuffed_write_end - Write end for stuffed files
570 * @inode: The inode
571 * @dibh: The buffer_head containing the on-disk inode
572 * @pos: The file position
573 * @len: The length of the write
574 * @copied: How much was actually copied by the VFS
575 * @page: The page
576 *
577 * This copies the data from the page into the inode block after
578 * the inode data structure itself.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000579 *
580 * Returns: errno
581 */
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700582static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
583 loff_t pos, unsigned len, unsigned copied,
584 struct page *page)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000585{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400586 struct gfs2_inode *ip = GFS2_I(inode);
587 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700588 u64 to = pos + copied;
589 void *kaddr;
590 unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
591 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000592
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700593 BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
594 kaddr = kmap_atomic(page, KM_USER0);
595 memcpy(buf + pos, kaddr + pos, copied);
596 memset(kaddr + pos + copied, 0, len - copied);
597 flush_dcache_page(page);
598 kunmap_atomic(kaddr, KM_USER0);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000599
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700600 if (!PageUptodate(page))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000601 SetPageUptodate(page);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700602 unlock_page(page);
603 page_cache_release(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000604
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700605 if (inode->i_size < to) {
606 i_size_write(inode, to);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000607 ip->i_di.di_size = inode->i_size;
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400608 di->di_size = cpu_to_be64(inode->i_size);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700609 mark_inode_dirty(inode);
Steven Whitehouse48516ce2006-10-02 12:39:19 -0400610 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000611
Robert Peterson7ae8fa82007-05-09 09:37:57 -0500612 if (inode == sdp->sd_rindex)
613 adjust_fs_space(inode);
614
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000615 brelse(dibh);
616 gfs2_trans_end(sdp);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700617 gfs2_glock_dq(&ip->i_gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000618 gfs2_holder_uninit(&ip->i_gh);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700619 return copied;
620}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700622/**
623 * gfs2_write_end
624 * @file: The file to write to
625 * @mapping: The address space to write to
626 * @pos: The file position
627 * @len: The length of the data
628 * @copied:
629 * @page: The page that has been written
630 * @fsdata: The fsdata (unused in GFS2)
631 *
632 * The main write_end function for GFS2. We have a separate one for
633 * stuffed files as they are slightly different, otherwise we just
634 * put our locking around the VFS provided functions.
635 *
636 * Returns: errno
637 */
638
639static int gfs2_write_end(struct file *file, struct address_space *mapping,
640 loff_t pos, unsigned len, unsigned copied,
641 struct page *page, void *fsdata)
642{
643 struct inode *inode = page->mapping->host;
644 struct gfs2_inode *ip = GFS2_I(inode);
645 struct gfs2_sbd *sdp = GFS2_SB(inode);
646 struct buffer_head *dibh;
647 struct gfs2_alloc *al = &ip->i_alloc;
648 struct gfs2_dinode *di;
649 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
650 unsigned int to = from + len;
651 int ret;
652
653 BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == 0);
654
655 ret = gfs2_meta_inode_buffer(ip, &dibh);
656 if (unlikely(ret)) {
657 unlock_page(page);
658 page_cache_release(page);
659 goto failed;
660 }
661
662 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
663
664 if (gfs2_is_stuffed(ip))
665 return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
666
Steven Whitehousebf36a712007-10-17 08:35:19 +0100667 if (!gfs2_is_writeback(ip))
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700668 gfs2_page_add_databufs(ip, page, from, to);
669
670 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
671
672 if (likely(ret >= 0)) {
673 copied = ret;
674 if ((pos + copied) > inode->i_size) {
675 di = (struct gfs2_dinode *)dibh->b_data;
676 ip->i_di.di_size = inode->i_size;
677 di->di_size = cpu_to_be64(inode->i_size);
678 mark_inode_dirty(inode);
679 }
680 }
681
682 if (inode == sdp->sd_rindex)
683 adjust_fs_space(inode);
684
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000685 brelse(dibh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000686 gfs2_trans_end(sdp);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700687failed:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000688 if (al->al_requested) {
689 gfs2_inplace_release(ip);
690 gfs2_quota_unlock(ip);
691 gfs2_alloc_put(ip);
692 }
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700693 gfs2_glock_dq(&ip->i_gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000694 gfs2_holder_uninit(&ip->i_gh);
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700695 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000696}
697
698/**
Robert Peterson8fb68592007-06-12 11:24:36 -0500699 * gfs2_set_page_dirty - Page dirtying function
700 * @page: The page to dirty
701 *
702 * Returns: 1 if it dirtyed the page, or 0 otherwise
703 */
704
705static int gfs2_set_page_dirty(struct page *page)
706{
Steven Whitehouse55610932007-10-17 08:47:38 +0100707 SetPageChecked(page);
Robert Peterson8fb68592007-06-12 11:24:36 -0500708 return __set_page_dirty_buffers(page);
709}
710
711/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000712 * gfs2_bmap - Block map function
713 * @mapping: Address space info
714 * @lblock: The block to map
715 *
716 * Returns: The disk address for the block or 0 on hole or error
717 */
718
719static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
720{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400721 struct gfs2_inode *ip = GFS2_I(mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000722 struct gfs2_holder i_gh;
723 sector_t dblock = 0;
724 int error;
725
David Teiglandb3b94fa2006-01-16 16:50:04 +0000726 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
727 if (error)
728 return 0;
729
730 if (!gfs2_is_stuffed(ip))
Steven Whitehouse4ff14672006-01-30 09:39:10 +0000731 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732
733 gfs2_glock_dq_uninit(&i_gh);
734
735 return dblock;
736}
737
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100738static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
739{
740 struct gfs2_bufdata *bd;
741
742 lock_buffer(bh);
743 gfs2_log_lock(sdp);
744 clear_buffer_dirty(bh);
745 bd = bh->b_private;
746 if (bd) {
Steven Whitehouse16615be2007-09-17 10:59:52 +0100747 if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
748 list_del_init(&bd->bd_le.le_list);
749 else
750 gfs2_remove_from_journal(bh, current->journal_info, 0);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100751 }
752 bh->b_bdev = NULL;
753 clear_buffer_mapped(bh);
754 clear_buffer_req(bh);
755 clear_buffer_new(bh);
756 gfs2_log_unlock(sdp);
757 unlock_buffer(bh);
758}
759
Steven Whitehouse8628de02006-03-31 16:48:41 -0500760static void gfs2_invalidatepage(struct page *page, unsigned long offset)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000761{
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100762 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
763 struct buffer_head *bh, *head;
764 unsigned long pos = 0;
765
David Teiglandb3b94fa2006-01-16 16:50:04 +0000766 BUG_ON(!PageLocked(page));
Robert Peterson8fb68592007-06-12 11:24:36 -0500767 if (offset == 0)
768 ClearPageChecked(page);
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100769 if (!page_has_buffers(page))
770 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000771
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100772 bh = head = page_buffers(page);
773 do {
774 if (offset <= pos)
775 gfs2_discard(sdp, bh);
776 pos += bh->b_size;
777 bh = bh->b_this_page;
778 } while (bh != head);
779out:
780 if (offset == 0)
781 try_to_release_page(page, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000782}
783
Steven Whitehousec7b33832006-12-14 18:24:26 +0000784/**
785 * gfs2_ok_for_dio - check that dio is valid on this file
786 * @ip: The inode
787 * @rw: READ or WRITE
788 * @offset: The offset at which we are reading or writing
789 *
790 * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
791 * 1 (to accept the i/o request)
792 */
793static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
794{
795 /*
796 * Should we return an error here? I can't see that O_DIRECT for
Steven Whitehouse55610932007-10-17 08:47:38 +0100797 * a stuffed file makes any sense. For now we'll silently fall
798 * back to buffered I/O
Steven Whitehousec7b33832006-12-14 18:24:26 +0000799 */
Steven Whitehousec7b33832006-12-14 18:24:26 +0000800 if (gfs2_is_stuffed(ip))
801 return 0;
802
803 if (offset > i_size_read(&ip->i_inode))
804 return 0;
805 return 1;
806}
807
808
809
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400810static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
811 const struct iovec *iov, loff_t offset,
812 unsigned long nr_segs)
Steven Whitehoused1665e42006-02-14 11:54:42 +0000813{
814 struct file *file = iocb->ki_filp;
815 struct inode *inode = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400816 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000817 struct gfs2_holder gh;
818 int rv;
819
820 /*
Steven Whitehousec7b33832006-12-14 18:24:26 +0000821 * Deferred lock, even if its a write, since we do no allocation
822 * on this path. All we need change is atime, and this lock mode
823 * ensures that other nodes have flushed their buffered read caches
824 * (i.e. their page cache entries for this inode). We do not,
825 * unfortunately have the option of only flushing a range like
826 * the VFS does.
Steven Whitehoused1665e42006-02-14 11:54:42 +0000827 */
Steven Whitehousec7b33832006-12-14 18:24:26 +0000828 gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh);
Steven Whitehousedcd24792006-11-16 11:08:16 -0500829 rv = gfs2_glock_nq_atime(&gh);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000830 if (rv)
Steven Whitehousec7b33832006-12-14 18:24:26 +0000831 return rv;
832 rv = gfs2_ok_for_dio(ip, rw, offset);
833 if (rv != 1)
834 goto out; /* dio not valid, fall back to buffered i/o */
Steven Whitehoused1665e42006-02-14 11:54:42 +0000835
Steven Whitehousec7b33832006-12-14 18:24:26 +0000836 rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
837 iov, offset, nr_segs,
838 gfs2_get_block_direct, NULL);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000839out:
840 gfs2_glock_dq_m(1, &gh);
841 gfs2_holder_uninit(&gh);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000842 return rv;
843}
844
845/**
Steven Whitehouse623d9352006-08-31 12:14:44 -0400846 * gfs2_releasepage - free the metadata associated with a page
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400847 * @page: the page that's being released
848 * @gfp_mask: passed from Linux VFS, ignored by us
849 *
850 * Call try_to_free_buffers() if the buffers in this page can be
851 * released.
852 *
853 * Returns: 0
854 */
855
856int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
857{
858 struct inode *aspace = page->mapping->host;
859 struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
860 struct buffer_head *bh, *head;
861 struct gfs2_bufdata *bd;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400862
863 if (!page_has_buffers(page))
Steven Whitehouse891ba6d2007-09-20 15:26:33 +0100864 return 0;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400865
Steven Whitehousebb3b0e32007-08-16 16:03:57 +0100866 gfs2_log_lock(sdp);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400867 head = bh = page_buffers(page);
868 do {
Steven Whitehousebb3b0e32007-08-16 16:03:57 +0100869 if (atomic_read(&bh->b_count))
870 goto cannot_release;
871 bd = bh->b_private;
872 if (bd && bd->bd_ail)
873 goto cannot_release;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400874 gfs2_assert_warn(sdp, !buffer_pinned(bh));
Steven Whitehouse623d9352006-08-31 12:14:44 -0400875 gfs2_assert_warn(sdp, !buffer_dirty(bh));
Steven Whitehousebb3b0e32007-08-16 16:03:57 +0100876 bh = bh->b_this_page;
877 } while(bh != head);
878 gfs2_log_unlock(sdp);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400879
Steven Whitehousebb3b0e32007-08-16 16:03:57 +0100880 head = bh = page_buffers(page);
881 do {
Steven Whitehouse623d9352006-08-31 12:14:44 -0400882 gfs2_log_lock(sdp);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400883 bd = bh->b_private;
884 if (bd) {
885 gfs2_assert_warn(sdp, bd->bd_bh == bh);
886 gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
Steven Whitehoused7b616e2007-09-02 10:48:13 +0100887 if (!list_empty(&bd->bd_le.le_list)) {
888 if (!buffer_pinned(bh))
889 list_del_init(&bd->bd_le.le_list);
890 else
891 bd = NULL;
892 }
893 if (bd)
894 bd->bd_bh = NULL;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400895 bh->b_private = NULL;
896 }
Steven Whitehouse623d9352006-08-31 12:14:44 -0400897 gfs2_log_unlock(sdp);
898 if (bd)
899 kmem_cache_free(gfs2_bufdata_cachep, bd);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400900
901 bh = bh->b_this_page;
Steven Whitehouse166afcc2006-08-24 15:59:40 -0400902 } while (bh != head);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400903
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400904 return try_to_free_buffers(page);
Steven Whitehousebb3b0e32007-08-16 16:03:57 +0100905cannot_release:
906 gfs2_log_unlock(sdp);
907 return 0;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400908}
909
Steven Whitehouse55610932007-10-17 08:47:38 +0100910static const struct address_space_operations gfs2_writeback_aops = {
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100911 .writepage = gfs2_writeback_writepage,
Steven Whitehouse55610932007-10-17 08:47:38 +0100912 .writepages = gfs2_writeback_writepages,
913 .readpage = gfs2_readpage,
914 .readpages = gfs2_readpages,
915 .sync_page = block_sync_page,
916 .write_begin = gfs2_write_begin,
917 .write_end = gfs2_write_end,
918 .bmap = gfs2_bmap,
919 .invalidatepage = gfs2_invalidatepage,
920 .releasepage = gfs2_releasepage,
921 .direct_IO = gfs2_direct_IO,
922};
923
924static const struct address_space_operations gfs2_ordered_aops = {
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100925 .writepage = gfs2_ordered_writepage,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000926 .readpage = gfs2_readpage,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400927 .readpages = gfs2_readpages,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 .sync_page = block_sync_page,
Steven Whitehouse7765ec22007-10-16 01:25:07 -0700929 .write_begin = gfs2_write_begin,
930 .write_end = gfs2_write_end,
Robert Peterson8fb68592007-06-12 11:24:36 -0500931 .set_page_dirty = gfs2_set_page_dirty,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000932 .bmap = gfs2_bmap,
933 .invalidatepage = gfs2_invalidatepage,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400934 .releasepage = gfs2_releasepage,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000935 .direct_IO = gfs2_direct_IO,
936};
937
Steven Whitehouse55610932007-10-17 08:47:38 +0100938static const struct address_space_operations gfs2_jdata_aops = {
Steven Whitehouse9ff8ec32007-09-28 13:49:05 +0100939 .writepage = gfs2_jdata_writepage,
Steven Whitehouse55610932007-10-17 08:47:38 +0100940 .readpage = gfs2_readpage,
941 .readpages = gfs2_readpages,
942 .sync_page = block_sync_page,
943 .write_begin = gfs2_write_begin,
944 .write_end = gfs2_write_end,
945 .set_page_dirty = gfs2_set_page_dirty,
946 .bmap = gfs2_bmap,
947 .invalidatepage = gfs2_invalidatepage,
948 .releasepage = gfs2_releasepage,
949};
950
951void gfs2_set_aops(struct inode *inode)
952{
953 struct gfs2_inode *ip = GFS2_I(inode);
954
955 if (gfs2_is_writeback(ip))
956 inode->i_mapping->a_ops = &gfs2_writeback_aops;
957 else if (gfs2_is_ordered(ip))
958 inode->i_mapping->a_ops = &gfs2_ordered_aops;
959 else if (gfs2_is_jdata(ip))
960 inode->i_mapping->a_ops = &gfs2_jdata_aops;
961 else
962 BUG();
963}
964