blob: d44d42fb4163b89d24363c8ff7bcfdf9984fd0a3 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 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 Whitehouse5c676f62006-02-27 17:23:27 -050019#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000020
21#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050022#include "lm_interface.h"
23#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "bmap.h"
25#include "glock.h"
26#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000027#include "log.h"
28#include "meta_io.h"
29#include "ops_address.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030#include "quota.h"
31#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000032#include "rgrp.h"
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000033#include "ops_file.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050034#include "util.h"
Steven Whitehouse4340fe62006-07-11 09:46:33 -040035#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000036
Steven Whitehouseba7f7292006-07-26 11:27:10 -040037
38static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
39 unsigned int from, unsigned int to)
40{
41 struct buffer_head *head = page_buffers(page);
42 unsigned int bsize = head->b_size;
43 struct buffer_head *bh;
44 unsigned int start, end;
45
46 for (bh = head, start = 0; bh != head || !start;
47 bh = bh->b_this_page, start = end) {
48 end = start + bsize;
49 if (end <= from || start >= to)
50 continue;
51 gfs2_trans_add_bh(ip->i_gl, bh, 0);
52 }
53}
54
David Teiglandb3b94fa2006-01-16 16:50:04 +000055/**
Steven Whitehouse4ff14672006-01-30 09:39:10 +000056 * gfs2_get_block - Fills in a buffer head with details about a block
David Teiglandb3b94fa2006-01-16 16:50:04 +000057 * @inode: The inode
58 * @lblock: The block number to look up
59 * @bh_result: The buffer head to return the result in
60 * @create: Non-zero if we may add block to the file
61 *
62 * Returns: errno
63 */
64
Steven Whitehouse4ff14672006-01-30 09:39:10 +000065int gfs2_get_block(struct inode *inode, sector_t lblock,
66 struct buffer_head *bh_result, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +000067{
David Teiglandb3b94fa2006-01-16 16:50:04 +000068 int new = create;
Steven Whitehousecd915492006-09-04 12:49:07 -040069 u64 dblock;
David Teiglandb3b94fa2006-01-16 16:50:04 +000070 int error;
Steven Whitehousefd88de562006-05-05 16:59:11 -040071 int boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +000072
Steven Whitehousefd88de562006-05-05 16:59:11 -040073 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
David Teiglandb3b94fa2006-01-16 16:50:04 +000074 if (error)
75 return error;
76
77 if (!dblock)
78 return 0;
79
80 map_bh(bh_result, inode->i_sb, dblock);
81 if (new)
82 set_buffer_new(bh_result);
Steven Whitehousefd88de562006-05-05 16:59:11 -040083 if (boundary)
84 set_buffer_boundary(bh_result);
David Teiglandb3b94fa2006-01-16 16:50:04 +000085
86 return 0;
87}
88
89/**
90 * get_block_noalloc - Fills in a buffer head with details about a block
91 * @inode: The inode
92 * @lblock: The block number to look up
93 * @bh_result: The buffer head to return the result in
94 * @create: Non-zero if we may add block to the file
95 *
96 * Returns: errno
97 */
98
99static int get_block_noalloc(struct inode *inode, sector_t lblock,
100 struct buffer_head *bh_result, int create)
101{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000102 int new = 0;
Steven Whitehousecd915492006-09-04 12:49:07 -0400103 u64 dblock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000104 int error;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400105 int boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000106
Steven Whitehousefd88de562006-05-05 16:59:11 -0400107 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108 if (error)
109 return error;
110
111 if (dblock)
112 map_bh(bh_result, inode->i_sb, dblock);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400113 else if (gfs2_assert_withdraw(GFS2_SB(inode), !create))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114 error = -EIO;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400115 if (boundary)
116 set_buffer_boundary(bh_result);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117
118 return error;
119}
120
Steven Whitehouse623d9352006-08-31 12:14:44 -0400121static int get_block_direct(struct inode *inode, sector_t lblock,
122 struct buffer_head *bh_result, int create)
123{
124 int new = 0;
125 u64 dblock;
126 int error, boundary;
127
128 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
129 if (error)
130 return error;
131
132 if (dblock) {
133 map_bh(bh_result, inode->i_sb, dblock);
134 if (boundary)
135 set_buffer_boundary(bh_result);
136 }
137
138 return 0;
139}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140/**
141 * gfs2_writepage - Write complete page
142 * @page: Page to write
143 *
144 * Returns: errno
145 *
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000146 * Some of this is copied from block_write_full_page() although we still
147 * call it to do most of the work.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148 */
149
150static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
151{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000152 struct inode *inode = page->mapping->host;
Steven Whitehousef4387142006-08-08 13:23:19 -0400153 struct gfs2_inode *ip = GFS2_I(inode);
154 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000155 loff_t i_size = i_size_read(inode);
156 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
157 unsigned offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 int error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000159 int done_trans = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
162 unlock_page(page);
163 return -EIO;
164 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500165 if (current->journal_info)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000166 goto out_ignore;
167
168 /* Is the page fully outside i_size? (truncate in progress) */
169 offset = i_size & (PAGE_CACHE_SIZE-1);
Steven Whitehoused2d7b8a2006-05-02 12:09:42 -0400170 if (page->index > end_index || (page->index == end_index && !offset)) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000171 page->mapping->a_ops->invalidatepage(page, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 unlock_page(page);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000173 return 0; /* don't care */
174 }
175
176 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
177 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
178 if (error)
179 goto out_ignore;
Steven Whitehousef4387142006-08-08 13:23:19 -0400180 if (!page_has_buffers(page)) {
181 create_empty_buffers(page, inode->i_sb->s_blocksize,
182 (1 << BH_Dirty)|(1 << BH_Uptodate));
183 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000184 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
185 done_trans = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000186 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000187 error = block_write_full_page(page, get_block_noalloc, wbc);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000188 if (done_trans)
189 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190 gfs2_meta_cache_flush(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000192
193out_ignore:
194 redirty_page_for_writepage(wbc, page);
195 unlock_page(page);
196 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197}
198
Steven Whitehousefd88de562006-05-05 16:59:11 -0400199static int zero_readpage(struct page *page)
200{
201 void *kaddr;
202
203 kaddr = kmap_atomic(page, KM_USER0);
204 memset(kaddr, 0, PAGE_CACHE_SIZE);
205 kunmap_atomic(page, KM_USER0);
206
207 SetPageUptodate(page);
208
209 return 0;
210}
211
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212/**
213 * stuffed_readpage - Fill in a Linux page with stuffed file data
214 * @ip: the inode
215 * @page: the page
216 *
217 * Returns: errno
218 */
219
220static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
221{
222 struct buffer_head *dibh;
223 void *kaddr;
224 int error;
225
Steven Whitehousefd88de562006-05-05 16:59:11 -0400226 /* Only the first page of a stuffed file might contain data */
227 if (unlikely(page->index))
228 return zero_readpage(page);
229
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230 error = gfs2_meta_inode_buffer(ip, &dibh);
231 if (error)
232 return error;
233
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000234 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400235 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236 ip->i_di.di_size);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400237 memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000238 kunmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239
240 brelse(dibh);
241
242 SetPageUptodate(page);
243
244 return 0;
245}
246
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247
248/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249 * gfs2_readpage - readpage with locking
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000250 * @file: The file to read a page for. N.B. This may be NULL if we are
251 * reading an internal file.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252 * @page: The page to read
253 *
254 * Returns: errno
255 */
256
257static int gfs2_readpage(struct file *file, struct page *page)
258{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400259 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
260 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000261 struct gfs2_holder gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262 int error;
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400263 int do_unlock = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000264
Steven Whitehousedd538c82006-09-04 14:53:30 -0400265 if (likely(file != &gfs2_internal_file_sentinel)) {
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400266 if (file) {
267 struct gfs2_file *gf = file->private_data;
268 if (test_bit(GFF_EXLOCK, &gf->f_flags))
269 goto skip_lock;
270 }
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400271 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh);
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400272 do_unlock = 1;
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000273 error = gfs2_glock_nq_m_atime(1, &gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400274 if (unlikely(error))
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000275 goto out_unlock;
276 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400278skip_lock:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000279 if (gfs2_is_stuffed(ip)) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400280 error = stuffed_readpage(ip, page);
281 unlock_page(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 } else
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000283 error = mpage_readpage(page, gfs2_get_block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000284
285 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
286 error = -EIO;
287
Steven Whitehousedd538c82006-09-04 14:53:30 -0400288 if (file != &gfs2_internal_file_sentinel) {
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000289 gfs2_glock_dq_m(1, &gh);
290 gfs2_holder_uninit(&gh);
291 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000292out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000293 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000294out_unlock:
295 unlock_page(page);
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400296 if (do_unlock)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400297 gfs2_holder_uninit(&gh);
298 goto out;
299}
300
Steven Whitehousefd88de562006-05-05 16:59:11 -0400301/**
302 * gfs2_readpages - Read a bunch of pages at once
303 *
304 * Some notes:
305 * 1. This is only for readahead, so we can simply ignore any things
306 * which are slightly inconvenient (such as locking conflicts between
307 * the page lock and the glock) and return having done no I/O. Its
308 * obviously not something we'd want to do on too regular a basis.
309 * Any I/O we ignore at this time will be done via readpage later.
310 * 2. We have to handle stuffed files here too.
311 * 3. mpage_readpages() does most of the heavy lifting in the common case.
312 * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
313 * 5. We use LM_FLAG_TRY_1CB here, effectively we then have lock-ahead as
314 * well as read-ahead.
315 */
316static int gfs2_readpages(struct file *file, struct address_space *mapping,
317 struct list_head *pages, unsigned nr_pages)
318{
319 struct inode *inode = mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400320 struct gfs2_inode *ip = GFS2_I(inode);
321 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400322 struct gfs2_holder gh;
323 unsigned page_idx;
324 int ret;
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400325 int do_unlock = 0;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400326
Steven Whitehousedd538c82006-09-04 14:53:30 -0400327 if (likely(file != &gfs2_internal_file_sentinel)) {
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400328 if (file) {
329 struct gfs2_file *gf = file->private_data;
330 if (test_bit(GFF_EXLOCK, &gf->f_flags))
331 goto skip_lock;
332 }
Steven Whitehousefd88de562006-05-05 16:59:11 -0400333 gfs2_holder_init(ip->i_gl, LM_ST_SHARED,
334 LM_FLAG_TRY_1CB|GL_ATIME|GL_AOP, &gh);
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400335 do_unlock = 1;
Steven Whitehousefd88de562006-05-05 16:59:11 -0400336 ret = gfs2_glock_nq_m_atime(1, &gh);
337 if (ret == GLR_TRYFAILED)
338 goto out_noerror;
339 if (unlikely(ret))
340 goto out_unlock;
341 }
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400342skip_lock:
Steven Whitehousefd88de562006-05-05 16:59:11 -0400343 if (gfs2_is_stuffed(ip)) {
344 struct pagevec lru_pvec;
345 pagevec_init(&lru_pvec, 0);
346 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Steven Whitehouseffeb8742006-07-10 15:47:01 -0400347 struct page *page = list_entry(pages->prev, struct page, lru);
348 prefetchw(&page->flags);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400349 list_del(&page->lru);
350 if (!add_to_page_cache(page, mapping,
351 page->index, GFP_KERNEL)) {
352 ret = stuffed_readpage(ip, page);
353 unlock_page(page);
354 if (!pagevec_add(&lru_pvec, page))
355 __pagevec_lru_add(&lru_pvec);
Steven Whitehouseffeb8742006-07-10 15:47:01 -0400356 } else {
357 page_cache_release(page);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400358 }
Steven Whitehousefd88de562006-05-05 16:59:11 -0400359 }
360 pagevec_lru_add(&lru_pvec);
361 ret = 0;
362 } else {
363 /* What we really want to do .... */
364 ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
365 }
366
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400367 if (do_unlock) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400368 gfs2_glock_dq_m(1, &gh);
369 gfs2_holder_uninit(&gh);
370 }
371out:
372 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
373 ret = -EIO;
374 return ret;
375out_noerror:
376 ret = 0;
377out_unlock:
378 /* unlock all pages, we can't do any I/O right now */
379 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Steven Whitehouseffeb8742006-07-10 15:47:01 -0400380 struct page *page = list_entry(pages->prev, struct page, lru);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400381 list_del(&page->lru);
382 unlock_page(page);
383 page_cache_release(page);
384 }
Steven Whitehouse59a1cc62006-08-04 15:41:22 -0400385 if (do_unlock)
Steven Whitehousefd88de562006-05-05 16:59:11 -0400386 gfs2_holder_uninit(&gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000387 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000388}
389
390/**
391 * gfs2_prepare_write - Prepare to write a page to a file
392 * @file: The file to write to
393 * @page: The page which is to be prepared for writing
394 * @from: From (byte range within page)
395 * @to: To (byte range within page)
396 *
397 * Returns: errno
398 */
399
400static int gfs2_prepare_write(struct file *file, struct page *page,
401 unsigned from, unsigned to)
402{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400403 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
404 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000405 unsigned int data_blocks, ind_blocks, rblocks;
406 int alloc_required;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000407 int error = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000408 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
409 loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
410 struct gfs2_alloc *al;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000411
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400412 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000413 error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
414 if (error)
415 goto out_uninit;
416
417 gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
418
419 error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
420 if (error)
421 goto out_unlock;
422
423
424 if (alloc_required) {
425 al = gfs2_alloc_get(ip);
426
427 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
428 if (error)
429 goto out_alloc_put;
430
431 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
432 if (error)
433 goto out_qunlock;
434
435 al->al_requested = data_blocks + ind_blocks;
436 error = gfs2_inplace_reserve(ip);
437 if (error)
438 goto out_qunlock;
439 }
440
441 rblocks = RES_DINODE + ind_blocks;
442 if (gfs2_is_jdata(ip))
443 rblocks += data_blocks ? data_blocks : 1;
444 if (ind_blocks || data_blocks)
445 rblocks += RES_STATFS + RES_QUOTA;
446
447 error = gfs2_trans_begin(sdp, rblocks, 0);
448 if (error)
449 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000450
451 if (gfs2_is_stuffed(ip)) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000452 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400453 error = gfs2_unstuff_dinode(ip, page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000454 if (error == 0)
455 goto prepare_write;
456 } else if (!PageUptodate(page))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457 error = stuffed_readpage(ip, page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000458 goto out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000459 }
460
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000461prepare_write:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000462 error = block_prepare_write(page, from, to, gfs2_get_block);
463
464out:
465 if (error) {
466 gfs2_trans_end(sdp);
467 if (alloc_required) {
468 gfs2_inplace_release(ip);
469out_qunlock:
470 gfs2_quota_unlock(ip);
471out_alloc_put:
472 gfs2_alloc_put(ip);
473 }
474out_unlock:
475 gfs2_glock_dq_m(1, &ip->i_gh);
476out_uninit:
477 gfs2_holder_uninit(&ip->i_gh);
478 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000479
480 return error;
481}
482
483/**
484 * gfs2_commit_write - Commit write to a file
485 * @file: The file to write to
486 * @page: The page containing the data
487 * @from: From (byte range within page)
488 * @to: To (byte range within page)
489 *
490 * Returns: errno
491 */
492
493static int gfs2_commit_write(struct file *file, struct page *page,
494 unsigned from, unsigned to)
495{
496 struct inode *inode = page->mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400497 struct gfs2_inode *ip = GFS2_I(inode);
498 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000499 int error = -EOPNOTSUPP;
500 struct buffer_head *dibh;
501 struct gfs2_alloc *al = &ip->i_alloc;;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000502
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000503 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
504 goto fail_nounlock;
505
506 error = gfs2_meta_inode_buffer(ip, &dibh);
507 if (error)
508 goto fail_endtrans;
509
510 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
511
David Teiglandb3b94fa2006-01-16 16:50:04 +0000512 if (gfs2_is_stuffed(ip)) {
Steven Whitehousecd915492006-09-04 12:49:07 -0400513 u64 file_size;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000514 void *kaddr;
515
Steven Whitehousecd915492006-09-04 12:49:07 -0400516 file_size = ((u64)page->index << PAGE_CACHE_SHIFT) + to;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000517
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000518 kaddr = kmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000519 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
Steven Whitehouse0bd59962006-09-04 14:59:35 -0400520 kaddr + from, to - from);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000521 kunmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000522
523 SetPageUptodate(page);
524
525 if (inode->i_size < file_size)
526 i_size_write(inode, file_size);
527 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500528 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
529 gfs2_is_jdata(ip))
Steven Whitehouse257f9b42006-01-31 10:00:25 +0000530 gfs2_page_add_databufs(ip, page, from, to);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531 error = generic_commit_write(file, page, from, to);
532 if (error)
533 goto fail;
534 }
535
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000536 if (ip->i_di.di_size < inode->i_size)
537 ip->i_di.di_size = inode->i_size;
538
539 gfs2_dinode_out(&ip->i_di, dibh->b_data);
540 brelse(dibh);
541 gfs2_trans_end(sdp);
542 if (al->al_requested) {
543 gfs2_inplace_release(ip);
544 gfs2_quota_unlock(ip);
545 gfs2_alloc_put(ip);
546 }
547 gfs2_glock_dq_m(1, &ip->i_gh);
548 gfs2_holder_uninit(&ip->i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549 return 0;
550
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000551fail:
552 brelse(dibh);
553fail_endtrans:
554 gfs2_trans_end(sdp);
555 if (al->al_requested) {
556 gfs2_inplace_release(ip);
557 gfs2_quota_unlock(ip);
558 gfs2_alloc_put(ip);
559 }
560 gfs2_glock_dq_m(1, &ip->i_gh);
561 gfs2_holder_uninit(&ip->i_gh);
562fail_nounlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000563 ClearPageUptodate(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000564 return error;
565}
566
567/**
568 * gfs2_bmap - Block map function
569 * @mapping: Address space info
570 * @lblock: The block to map
571 *
572 * Returns: The disk address for the block or 0 on hole or error
573 */
574
575static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
576{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400577 struct gfs2_inode *ip = GFS2_I(mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000578 struct gfs2_holder i_gh;
579 sector_t dblock = 0;
580 int error;
581
David Teiglandb3b94fa2006-01-16 16:50:04 +0000582 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
583 if (error)
584 return 0;
585
586 if (!gfs2_is_stuffed(ip))
Steven Whitehouse4ff14672006-01-30 09:39:10 +0000587 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000588
589 gfs2_glock_dq_uninit(&i_gh);
590
591 return dblock;
592}
593
594static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
595{
Steven Whitehouse64fb4eb2006-01-18 13:14:40 +0000596 struct gfs2_bufdata *bd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597
598 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500599 bd = bh->b_private;
Steven Whitehouse64fb4eb2006-01-18 13:14:40 +0000600 if (bd) {
601 bd->bd_bh = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500602 bh->b_private = NULL;
Steven Whitehouse15d00c02006-08-18 15:51:09 -0400603 }
604 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000605
606 lock_buffer(bh);
607 clear_buffer_dirty(bh);
608 bh->b_bdev = NULL;
609 clear_buffer_mapped(bh);
610 clear_buffer_req(bh);
611 clear_buffer_new(bh);
612 clear_buffer_delay(bh);
613 unlock_buffer(bh);
614}
615
Steven Whitehouse8628de02006-03-31 16:48:41 -0500616static void gfs2_invalidatepage(struct page *page, unsigned long offset)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617{
Steven Whitehouse15d00c02006-08-18 15:51:09 -0400618 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000619 struct buffer_head *head, *bh, *next;
620 unsigned int curr_off = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621
622 BUG_ON(!PageLocked(page));
623 if (!page_has_buffers(page))
Steven Whitehouse8628de02006-03-31 16:48:41 -0500624 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625
626 bh = head = page_buffers(page);
627 do {
628 unsigned int next_off = curr_off + bh->b_size;
629 next = bh->b_this_page;
630
631 if (offset <= curr_off)
632 discard_buffer(sdp, bh);
633
634 curr_off = next_off;
635 bh = next;
636 } while (bh != head);
637
638 if (!offset)
Steven Whitehouse8628de02006-03-31 16:48:41 -0500639 try_to_release_page(page, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000640
Steven Whitehouse8628de02006-03-31 16:48:41 -0500641 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642}
643
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400644static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
645 const struct iovec *iov, loff_t offset,
646 unsigned long nr_segs)
Steven Whitehoused1665e42006-02-14 11:54:42 +0000647{
648 struct file *file = iocb->ki_filp;
649 struct inode *inode = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400650 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000651 struct gfs2_holder gh;
652 int rv;
653
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400654 if (rw == READ)
655 mutex_lock(&inode->i_mutex);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000656 /*
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400657 * Shared lock, even if its a write, since we do no allocation
Steven Whitehoused1665e42006-02-14 11:54:42 +0000658 * on this path. All we need change is atime.
659 */
660 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
661 rv = gfs2_glock_nq_m_atime(1, &gh);
662 if (rv)
663 goto out;
664
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400665 if (offset > i_size_read(inode))
666 goto out;
667
Steven Whitehoused1665e42006-02-14 11:54:42 +0000668 /*
669 * Should we return an error here? I can't see that O_DIRECT for
670 * a journaled file makes any sense. For now we'll silently fall
671 * back to buffered I/O, likewise we do the same for stuffed
672 * files since they are (a) small and (b) unaligned.
673 */
674 if (gfs2_is_jdata(ip))
675 goto out;
676
677 if (gfs2_is_stuffed(ip))
678 goto out;
679
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400680 rv = blockdev_direct_IO_own_locking(rw, iocb, inode,
681 inode->i_sb->s_bdev,
682 iov, offset, nr_segs,
Steven Whitehouse623d9352006-08-31 12:14:44 -0400683 get_block_direct, NULL);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000684out:
685 gfs2_glock_dq_m(1, &gh);
686 gfs2_holder_uninit(&gh);
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400687 if (rw == READ)
688 mutex_unlock(&inode->i_mutex);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000689
690 return rv;
691}
692
693/**
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400694 * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out.
695 * @bh: the buffer we're stuck on
696 *
697 */
698
699static void stuck_releasepage(struct buffer_head *bh)
700{
701 struct inode *inode = bh->b_page->mapping->host;
702 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
703 struct gfs2_bufdata *bd = bh->b_private;
704 struct gfs2_glock *gl;
Steven Whitehouse166afcc2006-08-24 15:59:40 -0400705static unsigned limit = 0;
706
Steven Whitehouse0bd59962006-09-04 14:59:35 -0400707 if (limit > 3)
Steven Whitehouse166afcc2006-08-24 15:59:40 -0400708 return;
Steven Whitehouse0bd59962006-09-04 14:59:35 -0400709 limit++;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400710
711 fs_warn(sdp, "stuck in gfs2_releasepage() %p\n", inode);
712 fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
713 (unsigned long long)bh->b_blocknr, atomic_read(&bh->b_count));
714 fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
715 fs_warn(sdp, "bh->b_private = %s\n", (bd) ? "!NULL" : "NULL");
716
717 if (!bd)
718 return;
719
720 gl = bd->bd_gl;
721
722 fs_warn(sdp, "gl = (%u, %llu)\n",
723 gl->gl_name.ln_type, (unsigned long long)gl->gl_name.ln_number);
724
725 fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
726 (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
727 (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
728
729 if (gl->gl_ops == &gfs2_inode_glops) {
730 struct gfs2_inode *ip = gl->gl_object;
731 unsigned int x;
732
733 if (!ip)
734 return;
735
736 fs_warn(sdp, "ip = %llu %llu\n",
737 (unsigned long long)ip->i_num.no_formal_ino,
738 (unsigned long long)ip->i_num.no_addr);
739
740 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
741 fs_warn(sdp, "ip->i_cache[%u] = %s\n",
742 x, (ip->i_cache[x]) ? "!NULL" : "NULL");
743 }
744}
745
746/**
Steven Whitehouse623d9352006-08-31 12:14:44 -0400747 * gfs2_releasepage - free the metadata associated with a page
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400748 * @page: the page that's being released
749 * @gfp_mask: passed from Linux VFS, ignored by us
750 *
751 * Call try_to_free_buffers() if the buffers in this page can be
752 * released.
753 *
754 * Returns: 0
755 */
756
757int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
758{
759 struct inode *aspace = page->mapping->host;
760 struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
761 struct buffer_head *bh, *head;
762 struct gfs2_bufdata *bd;
Steven Whitehouse166afcc2006-08-24 15:59:40 -0400763 unsigned long t = jiffies + gfs2_tune_get(sdp, gt_stall_secs) * HZ;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400764
765 if (!page_has_buffers(page))
766 goto out;
767
768 head = bh = page_buffers(page);
769 do {
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400770 while (atomic_read(&bh->b_count)) {
Steven Whitehouse166afcc2006-08-24 15:59:40 -0400771 if (!atomic_read(&aspace->i_writecount))
772 return 0;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400773
Steven Whitehouse166afcc2006-08-24 15:59:40 -0400774 if (time_after_eq(jiffies, t)) {
775 stuck_releasepage(bh);
776 /* should we withdraw here? */
777 return 0;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400778 }
779
Steven Whitehouse166afcc2006-08-24 15:59:40 -0400780 yield();
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400781 }
782
783 gfs2_assert_warn(sdp, !buffer_pinned(bh));
Steven Whitehouse623d9352006-08-31 12:14:44 -0400784 gfs2_assert_warn(sdp, !buffer_dirty(bh));
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400785
Steven Whitehouse623d9352006-08-31 12:14:44 -0400786 gfs2_log_lock(sdp);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400787 bd = bh->b_private;
788 if (bd) {
789 gfs2_assert_warn(sdp, bd->bd_bh == bh);
790 gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400791 gfs2_assert_warn(sdp, !bd->bd_ail);
Steven Whitehouse623d9352006-08-31 12:14:44 -0400792 bd->bd_bh = NULL;
793 if (!list_empty(&bd->bd_le.le_list))
794 bd = NULL;
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400795 bh->b_private = NULL;
796 }
Steven Whitehouse623d9352006-08-31 12:14:44 -0400797 gfs2_log_unlock(sdp);
798 if (bd)
799 kmem_cache_free(gfs2_bufdata_cachep, bd);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400800
801 bh = bh->b_this_page;
Steven Whitehouse166afcc2006-08-24 15:59:40 -0400802 } while (bh != head);
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400803
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400804out:
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400805 return try_to_free_buffers(page);
806}
807
Steven Whitehouse66de0452006-07-03 13:37:30 -0400808const struct address_space_operations gfs2_file_aops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000809 .writepage = gfs2_writepage,
810 .readpage = gfs2_readpage,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400811 .readpages = gfs2_readpages,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000812 .sync_page = block_sync_page,
813 .prepare_write = gfs2_prepare_write,
814 .commit_write = gfs2_commit_write,
815 .bmap = gfs2_bmap,
816 .invalidatepage = gfs2_invalidatepage,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400817 .releasepage = gfs2_releasepage,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000818 .direct_IO = gfs2_direct_IO,
819};
820