blob: 93e00a8af8cf274235faf2f03f69044c34939523 [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
7 * of the GNU General Public License v.2.
8 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/pagemap.h>
Steven 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"
30#include "page.h"
31#include "quota.h"
32#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000033#include "rgrp.h"
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000034#include "ops_file.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050035#include "util.h"
Steven Whitehouse4340fe62006-07-11 09:46:33 -040036#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000037
38/**
Steven Whitehouse4ff14672006-01-30 09:39:10 +000039 * gfs2_get_block - Fills in a buffer head with details about a block
David Teiglandb3b94fa2006-01-16 16:50:04 +000040 * @inode: The inode
41 * @lblock: The block number to look up
42 * @bh_result: The buffer head to return the result in
43 * @create: Non-zero if we may add block to the file
44 *
45 * Returns: errno
46 */
47
Steven Whitehouse4ff14672006-01-30 09:39:10 +000048int gfs2_get_block(struct inode *inode, sector_t lblock,
49 struct buffer_head *bh_result, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +000050{
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 int new = create;
52 uint64_t dblock;
53 int error;
Steven Whitehousefd88de562006-05-05 16:59:11 -040054 int boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +000055
Steven Whitehousefd88de562006-05-05 16:59:11 -040056 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
David Teiglandb3b94fa2006-01-16 16:50:04 +000057 if (error)
58 return error;
59
60 if (!dblock)
61 return 0;
62
63 map_bh(bh_result, inode->i_sb, dblock);
64 if (new)
65 set_buffer_new(bh_result);
Steven Whitehousefd88de562006-05-05 16:59:11 -040066 if (boundary)
67 set_buffer_boundary(bh_result);
David Teiglandb3b94fa2006-01-16 16:50:04 +000068
69 return 0;
70}
71
72/**
73 * get_block_noalloc - Fills in a buffer head with details about a block
74 * @inode: The inode
75 * @lblock: The block number to look up
76 * @bh_result: The buffer head to return the result in
77 * @create: Non-zero if we may add block to the file
78 *
79 * Returns: errno
80 */
81
82static int get_block_noalloc(struct inode *inode, sector_t lblock,
83 struct buffer_head *bh_result, int create)
84{
David Teiglandb3b94fa2006-01-16 16:50:04 +000085 int new = 0;
86 uint64_t dblock;
87 int error;
Steven Whitehousefd88de562006-05-05 16:59:11 -040088 int boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +000089
Steven Whitehousefd88de562006-05-05 16:59:11 -040090 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
David Teiglandb3b94fa2006-01-16 16:50:04 +000091 if (error)
92 return error;
93
94 if (dblock)
95 map_bh(bh_result, inode->i_sb, dblock);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -040096 else if (gfs2_assert_withdraw(GFS2_SB(inode), !create))
David Teiglandb3b94fa2006-01-16 16:50:04 +000097 error = -EIO;
Steven Whitehousefd88de562006-05-05 16:59:11 -040098 if (boundary)
99 set_buffer_boundary(bh_result);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000100
101 return error;
102}
103
David Teiglandb3b94fa2006-01-16 16:50:04 +0000104/**
105 * gfs2_writepage - Write complete page
106 * @page: Page to write
107 *
108 * Returns: errno
109 *
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000110 * Some of this is copied from block_write_full_page() although we still
111 * call it to do most of the work.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112 */
113
114static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
115{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000116 struct inode *inode = page->mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400117 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
118 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
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;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 int error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000123 int done_trans = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000124
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
126 unlock_page(page);
127 return -EIO;
128 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500129 if (current->journal_info)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000130 goto out_ignore;
131
132 /* Is the page fully outside i_size? (truncate in progress) */
133 offset = i_size & (PAGE_CACHE_SIZE-1);
Steven Whitehoused2d7b8a2006-05-02 12:09:42 -0400134 if (page->index > end_index || (page->index == end_index && !offset)) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000135 page->mapping->a_ops->invalidatepage(page, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136 unlock_page(page);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000137 return 0; /* don't care */
138 }
139
140 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
141 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
142 if (error)
143 goto out_ignore;
144 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
145 done_trans = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 error = block_write_full_page(page, get_block_noalloc, wbc);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000148 if (done_trans)
149 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 gfs2_meta_cache_flush(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000152
153out_ignore:
154 redirty_page_for_writepage(wbc, page);
155 unlock_page(page);
156 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157}
158
Steven Whitehousefd88de562006-05-05 16:59:11 -0400159static int zero_readpage(struct page *page)
160{
161 void *kaddr;
162
163 kaddr = kmap_atomic(page, KM_USER0);
164 memset(kaddr, 0, PAGE_CACHE_SIZE);
165 kunmap_atomic(page, KM_USER0);
166
167 SetPageUptodate(page);
168
169 return 0;
170}
171
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172/**
173 * stuffed_readpage - Fill in a Linux page with stuffed file data
174 * @ip: the inode
175 * @page: the page
176 *
177 * Returns: errno
178 */
179
180static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
181{
182 struct buffer_head *dibh;
183 void *kaddr;
184 int error;
185
Steven Whitehousefd88de562006-05-05 16:59:11 -0400186 /* Only the first page of a stuffed file might contain data */
187 if (unlikely(page->index))
188 return zero_readpage(page);
189
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190 error = gfs2_meta_inode_buffer(ip, &dibh);
191 if (error)
192 return error;
193
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000194 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400195 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196 ip->i_di.di_size);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400197 memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000198 kunmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199
200 brelse(dibh);
201
202 SetPageUptodate(page);
203
204 return 0;
205}
206
David Teiglandb3b94fa2006-01-16 16:50:04 +0000207
208/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000209 * gfs2_readpage - readpage with locking
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000210 * @file: The file to read a page for. N.B. This may be NULL if we are
211 * reading an internal file.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000212 * @page: The page to read
213 *
214 * Returns: errno
215 */
216
217static int gfs2_readpage(struct file *file, struct page *page)
218{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400219 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
220 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000221 struct gfs2_holder gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000222 int error;
223
Steven Whitehousefd88de562006-05-05 16:59:11 -0400224 if (likely(file != &gfs2_internal_file_sentinal)) {
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400225 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh);
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000226 error = gfs2_glock_nq_m_atime(1, &gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400227 if (unlikely(error))
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000228 goto out_unlock;
229 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000230
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000231 if (gfs2_is_stuffed(ip)) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400232 error = stuffed_readpage(ip, page);
233 unlock_page(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000234 } else
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000235 error = mpage_readpage(page, gfs2_get_block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236
237 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
238 error = -EIO;
239
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000240 if (file != &gfs2_internal_file_sentinal) {
241 gfs2_glock_dq_m(1, &gh);
242 gfs2_holder_uninit(&gh);
243 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000244out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000246out_unlock:
247 unlock_page(page);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400248 if (file != &gfs2_internal_file_sentinal)
249 gfs2_holder_uninit(&gh);
250 goto out;
251}
252
Steven Whitehousefd88de562006-05-05 16:59:11 -0400253/**
254 * gfs2_readpages - Read a bunch of pages at once
255 *
256 * Some notes:
257 * 1. This is only for readahead, so we can simply ignore any things
258 * which are slightly inconvenient (such as locking conflicts between
259 * the page lock and the glock) and return having done no I/O. Its
260 * obviously not something we'd want to do on too regular a basis.
261 * Any I/O we ignore at this time will be done via readpage later.
262 * 2. We have to handle stuffed files here too.
263 * 3. mpage_readpages() does most of the heavy lifting in the common case.
264 * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
265 * 5. We use LM_FLAG_TRY_1CB here, effectively we then have lock-ahead as
266 * well as read-ahead.
267 */
268static int gfs2_readpages(struct file *file, struct address_space *mapping,
269 struct list_head *pages, unsigned nr_pages)
270{
271 struct inode *inode = mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400272 struct gfs2_inode *ip = GFS2_I(inode);
273 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400274 struct gfs2_holder gh;
275 unsigned page_idx;
276 int ret;
277
278 if (likely(file != &gfs2_internal_file_sentinal)) {
279 gfs2_holder_init(ip->i_gl, LM_ST_SHARED,
280 LM_FLAG_TRY_1CB|GL_ATIME|GL_AOP, &gh);
281 ret = gfs2_glock_nq_m_atime(1, &gh);
282 if (ret == GLR_TRYFAILED)
283 goto out_noerror;
284 if (unlikely(ret))
285 goto out_unlock;
286 }
287
288 if (gfs2_is_stuffed(ip)) {
289 struct pagevec lru_pvec;
290 pagevec_init(&lru_pvec, 0);
291 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Steven Whitehouseffeb8742006-07-10 15:47:01 -0400292 struct page *page = list_entry(pages->prev, struct page, lru);
293 prefetchw(&page->flags);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400294 list_del(&page->lru);
295 if (!add_to_page_cache(page, mapping,
296 page->index, GFP_KERNEL)) {
297 ret = stuffed_readpage(ip, page);
298 unlock_page(page);
299 if (!pagevec_add(&lru_pvec, page))
300 __pagevec_lru_add(&lru_pvec);
Steven Whitehouseffeb8742006-07-10 15:47:01 -0400301 } else {
302 page_cache_release(page);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400303 }
Steven Whitehousefd88de562006-05-05 16:59:11 -0400304 }
305 pagevec_lru_add(&lru_pvec);
306 ret = 0;
307 } else {
308 /* What we really want to do .... */
309 ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
310 }
311
312 if (likely(file != &gfs2_internal_file_sentinal)) {
313 gfs2_glock_dq_m(1, &gh);
314 gfs2_holder_uninit(&gh);
315 }
316out:
317 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
318 ret = -EIO;
319 return ret;
320out_noerror:
321 ret = 0;
322out_unlock:
323 /* unlock all pages, we can't do any I/O right now */
324 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Steven Whitehouseffeb8742006-07-10 15:47:01 -0400325 struct page *page = list_entry(pages->prev, struct page, lru);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400326 list_del(&page->lru);
327 unlock_page(page);
328 page_cache_release(page);
329 }
330 if (likely(file != &gfs2_internal_file_sentinal))
331 gfs2_holder_uninit(&gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000332 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333}
334
335/**
336 * gfs2_prepare_write - Prepare to write a page to a file
337 * @file: The file to write to
338 * @page: The page which is to be prepared for writing
339 * @from: From (byte range within page)
340 * @to: To (byte range within page)
341 *
342 * Returns: errno
343 */
344
345static int gfs2_prepare_write(struct file *file, struct page *page,
346 unsigned from, unsigned to)
347{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400348 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
349 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000350 unsigned int data_blocks, ind_blocks, rblocks;
351 int alloc_required;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000352 int error = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000353 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
354 loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
355 struct gfs2_alloc *al;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000356
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400357 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000358 error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
359 if (error)
360 goto out_uninit;
361
362 gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
363
364 error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
365 if (error)
366 goto out_unlock;
367
368
369 if (alloc_required) {
370 al = gfs2_alloc_get(ip);
371
372 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
373 if (error)
374 goto out_alloc_put;
375
376 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
377 if (error)
378 goto out_qunlock;
379
380 al->al_requested = data_blocks + ind_blocks;
381 error = gfs2_inplace_reserve(ip);
382 if (error)
383 goto out_qunlock;
384 }
385
386 rblocks = RES_DINODE + ind_blocks;
387 if (gfs2_is_jdata(ip))
388 rblocks += data_blocks ? data_blocks : 1;
389 if (ind_blocks || data_blocks)
390 rblocks += RES_STATFS + RES_QUOTA;
391
392 error = gfs2_trans_begin(sdp, rblocks, 0);
393 if (error)
394 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000395
396 if (gfs2_is_stuffed(ip)) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000397 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
Steven Whitehousef25ef0c2006-07-26 10:51:20 -0400398 error = gfs2_unstuff_dinode(ip, page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000399 if (error == 0)
400 goto prepare_write;
401 } else if (!PageUptodate(page))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402 error = stuffed_readpage(ip, page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000403 goto out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000404 }
405
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000406prepare_write:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000407 error = block_prepare_write(page, from, to, gfs2_get_block);
408
409out:
410 if (error) {
411 gfs2_trans_end(sdp);
412 if (alloc_required) {
413 gfs2_inplace_release(ip);
414out_qunlock:
415 gfs2_quota_unlock(ip);
416out_alloc_put:
417 gfs2_alloc_put(ip);
418 }
419out_unlock:
420 gfs2_glock_dq_m(1, &ip->i_gh);
421out_uninit:
422 gfs2_holder_uninit(&ip->i_gh);
423 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424
425 return error;
426}
427
428/**
429 * gfs2_commit_write - Commit write to a file
430 * @file: The file to write to
431 * @page: The page containing the data
432 * @from: From (byte range within page)
433 * @to: To (byte range within page)
434 *
435 * Returns: errno
436 */
437
438static int gfs2_commit_write(struct file *file, struct page *page,
439 unsigned from, unsigned to)
440{
441 struct inode *inode = page->mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400442 struct gfs2_inode *ip = GFS2_I(inode);
443 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000444 int error = -EOPNOTSUPP;
445 struct buffer_head *dibh;
446 struct gfs2_alloc *al = &ip->i_alloc;;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000448 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
449 goto fail_nounlock;
450
451 error = gfs2_meta_inode_buffer(ip, &dibh);
452 if (error)
453 goto fail_endtrans;
454
455 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
456
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457 if (gfs2_is_stuffed(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000458 uint64_t file_size;
459 void *kaddr;
460
461 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
462
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000463 kaddr = kmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000464 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000465 (char *)kaddr + from, to - from);
466 kunmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000467
468 SetPageUptodate(page);
469
470 if (inode->i_size < file_size)
471 i_size_write(inode, file_size);
472 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500473 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
474 gfs2_is_jdata(ip))
Steven Whitehouse257f9b42006-01-31 10:00:25 +0000475 gfs2_page_add_databufs(ip, page, from, to);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000476 error = generic_commit_write(file, page, from, to);
477 if (error)
478 goto fail;
479 }
480
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000481 if (ip->i_di.di_size < inode->i_size)
482 ip->i_di.di_size = inode->i_size;
483
484 gfs2_dinode_out(&ip->i_di, dibh->b_data);
485 brelse(dibh);
486 gfs2_trans_end(sdp);
487 if (al->al_requested) {
488 gfs2_inplace_release(ip);
489 gfs2_quota_unlock(ip);
490 gfs2_alloc_put(ip);
491 }
492 gfs2_glock_dq_m(1, &ip->i_gh);
493 gfs2_holder_uninit(&ip->i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000494 return 0;
495
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000496fail:
497 brelse(dibh);
498fail_endtrans:
499 gfs2_trans_end(sdp);
500 if (al->al_requested) {
501 gfs2_inplace_release(ip);
502 gfs2_quota_unlock(ip);
503 gfs2_alloc_put(ip);
504 }
505 gfs2_glock_dq_m(1, &ip->i_gh);
506 gfs2_holder_uninit(&ip->i_gh);
507fail_nounlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000508 ClearPageUptodate(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000509 return error;
510}
511
512/**
513 * gfs2_bmap - Block map function
514 * @mapping: Address space info
515 * @lblock: The block to map
516 *
517 * Returns: The disk address for the block or 0 on hole or error
518 */
519
520static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
521{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400522 struct gfs2_inode *ip = GFS2_I(mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000523 struct gfs2_holder i_gh;
524 sector_t dblock = 0;
525 int error;
526
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
528 if (error)
529 return 0;
530
531 if (!gfs2_is_stuffed(ip))
Steven Whitehouse4ff14672006-01-30 09:39:10 +0000532 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000533
534 gfs2_glock_dq_uninit(&i_gh);
535
536 return dblock;
537}
538
539static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
540{
Steven Whitehouse64fb4eb2006-01-18 13:14:40 +0000541 struct gfs2_bufdata *bd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000542
543 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500544 bd = bh->b_private;
Steven Whitehouse64fb4eb2006-01-18 13:14:40 +0000545 if (bd) {
546 bd->bd_bh = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500547 bh->b_private = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000548 gfs2_log_unlock(sdp);
549 brelse(bh);
550 } else
551 gfs2_log_unlock(sdp);
552
553 lock_buffer(bh);
554 clear_buffer_dirty(bh);
555 bh->b_bdev = NULL;
556 clear_buffer_mapped(bh);
557 clear_buffer_req(bh);
558 clear_buffer_new(bh);
559 clear_buffer_delay(bh);
560 unlock_buffer(bh);
561}
562
Steven Whitehouse8628de02006-03-31 16:48:41 -0500563static void gfs2_invalidatepage(struct page *page, unsigned long offset)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000564{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500565 struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 struct buffer_head *head, *bh, *next;
567 unsigned int curr_off = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568
569 BUG_ON(!PageLocked(page));
570 if (!page_has_buffers(page))
Steven Whitehouse8628de02006-03-31 16:48:41 -0500571 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000572
573 bh = head = page_buffers(page);
574 do {
575 unsigned int next_off = curr_off + bh->b_size;
576 next = bh->b_this_page;
577
578 if (offset <= curr_off)
579 discard_buffer(sdp, bh);
580
581 curr_off = next_off;
582 bh = next;
583 } while (bh != head);
584
585 if (!offset)
Steven Whitehouse8628de02006-03-31 16:48:41 -0500586 try_to_release_page(page, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587
Steven Whitehouse8628de02006-03-31 16:48:41 -0500588 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589}
590
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400591static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
592 const struct iovec *iov, loff_t offset,
593 unsigned long nr_segs)
Steven Whitehoused1665e42006-02-14 11:54:42 +0000594{
595 struct file *file = iocb->ki_filp;
596 struct inode *inode = file->f_mapping->host;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400597 struct gfs2_inode *ip = GFS2_I(inode);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000598 struct gfs2_holder gh;
599 int rv;
600
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400601 if (rw == READ)
602 mutex_lock(&inode->i_mutex);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000603 /*
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400604 * Shared lock, even if its a write, since we do no allocation
Steven Whitehoused1665e42006-02-14 11:54:42 +0000605 * on this path. All we need change is atime.
606 */
607 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
608 rv = gfs2_glock_nq_m_atime(1, &gh);
609 if (rv)
610 goto out;
611
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400612 if (offset > i_size_read(inode))
613 goto out;
614
Steven Whitehoused1665e42006-02-14 11:54:42 +0000615 /*
616 * Should we return an error here? I can't see that O_DIRECT for
617 * a journaled file makes any sense. For now we'll silently fall
618 * back to buffered I/O, likewise we do the same for stuffed
619 * files since they are (a) small and (b) unaligned.
620 */
621 if (gfs2_is_jdata(ip))
622 goto out;
623
624 if (gfs2_is_stuffed(ip))
625 goto out;
626
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400627 rv = blockdev_direct_IO_own_locking(rw, iocb, inode,
628 inode->i_sb->s_bdev,
629 iov, offset, nr_segs,
630 gfs2_get_block, NULL);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000631out:
632 gfs2_glock_dq_m(1, &gh);
633 gfs2_holder_uninit(&gh);
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400634 if (rw == READ)
635 mutex_unlock(&inode->i_mutex);
Steven Whitehoused1665e42006-02-14 11:54:42 +0000636
637 return rv;
638}
639
640/**
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400641 * stuck_releasepage - We're stuck in gfs2_releasepage(). Print stuff out.
642 * @bh: the buffer we're stuck on
643 *
644 */
645
646static void stuck_releasepage(struct buffer_head *bh)
647{
648 struct inode *inode = bh->b_page->mapping->host;
649 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
650 struct gfs2_bufdata *bd = bh->b_private;
651 struct gfs2_glock *gl;
652
653 fs_warn(sdp, "stuck in gfs2_releasepage() %p\n", inode);
654 fs_warn(sdp, "blkno = %llu, bh->b_count = %d\n",
655 (unsigned long long)bh->b_blocknr, atomic_read(&bh->b_count));
656 fs_warn(sdp, "pinned = %u\n", buffer_pinned(bh));
657 fs_warn(sdp, "bh->b_private = %s\n", (bd) ? "!NULL" : "NULL");
658
659 if (!bd)
660 return;
661
662 gl = bd->bd_gl;
663
664 fs_warn(sdp, "gl = (%u, %llu)\n",
665 gl->gl_name.ln_type, (unsigned long long)gl->gl_name.ln_number);
666
667 fs_warn(sdp, "bd_list_tr = %s, bd_le.le_list = %s\n",
668 (list_empty(&bd->bd_list_tr)) ? "no" : "yes",
669 (list_empty(&bd->bd_le.le_list)) ? "no" : "yes");
670
671 if (gl->gl_ops == &gfs2_inode_glops) {
672 struct gfs2_inode *ip = gl->gl_object;
673 unsigned int x;
674
675 if (!ip)
676 return;
677
678 fs_warn(sdp, "ip = %llu %llu\n",
679 (unsigned long long)ip->i_num.no_formal_ino,
680 (unsigned long long)ip->i_num.no_addr);
681
682 for (x = 0; x < GFS2_MAX_META_HEIGHT; x++)
683 fs_warn(sdp, "ip->i_cache[%u] = %s\n",
684 x, (ip->i_cache[x]) ? "!NULL" : "NULL");
685 }
686}
687
688/**
689 * gfs2_aspace_releasepage - free the metadata associated with a page
690 * @page: the page that's being released
691 * @gfp_mask: passed from Linux VFS, ignored by us
692 *
693 * Call try_to_free_buffers() if the buffers in this page can be
694 * released.
695 *
696 * Returns: 0
697 */
698
699int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
700{
701 struct inode *aspace = page->mapping->host;
702 struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
703 struct buffer_head *bh, *head;
704 struct gfs2_bufdata *bd;
705 unsigned long t;
706
707 if (!page_has_buffers(page))
708 goto out;
709
710 head = bh = page_buffers(page);
711 do {
712 t = jiffies;
713
714 while (atomic_read(&bh->b_count)) {
715 if (atomic_read(&aspace->i_writecount)) {
716 if (time_after_eq(jiffies, t +
717 gfs2_tune_get(sdp, gt_stall_secs) * HZ)) {
718 stuck_releasepage(bh);
719 t = jiffies;
720 }
721
722 yield();
723 continue;
724 }
725
726 return 0;
727 }
728
729 gfs2_assert_warn(sdp, !buffer_pinned(bh));
730
731 bd = bh->b_private;
732 if (bd) {
733 gfs2_assert_warn(sdp, bd->bd_bh == bh);
734 gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
735 gfs2_assert_warn(sdp, list_empty(&bd->bd_le.le_list));
736 gfs2_assert_warn(sdp, !bd->bd_ail);
737 kmem_cache_free(gfs2_bufdata_cachep, bd);
738 bh->b_private = NULL;
739 }
740
741 bh = bh->b_this_page;
742 }
743 while (bh != head);
744
Steven Whitehousea9e5f4d2006-07-25 17:24:12 -0400745out:
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400746 return try_to_free_buffers(page);
747}
748
Steven Whitehouse66de0452006-07-03 13:37:30 -0400749const struct address_space_operations gfs2_file_aops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000750 .writepage = gfs2_writepage,
751 .readpage = gfs2_readpage,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400752 .readpages = gfs2_readpages,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000753 .sync_page = block_sync_page,
754 .prepare_write = gfs2_prepare_write,
755 .commit_write = gfs2_commit_write,
756 .bmap = gfs2_bmap,
757 .invalidatepage = gfs2_invalidatepage,
Steven Whitehouse4340fe62006-07-11 09:46:33 -0400758 .releasepage = gfs2_releasepage,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000759 .direct_IO = gfs2_direct_IO,
760};
761