blob: d89179b316fe82161aa5d74b30763a27dde5f231 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
8 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/pagemap.h>
Steven 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#include <asm/semaphore.h>
21
22#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050023#include "lm_interface.h"
24#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "bmap.h"
26#include "glock.h"
27#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000028#include "log.h"
29#include "meta_io.h"
30#include "ops_address.h"
31#include "page.h"
32#include "quota.h"
33#include "trans.h"
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000034#include "rgrp.h"
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000035#include "ops_file.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050036#include "util.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{
Steven Whitehouse5c676f62006-02-27 17:23:27 -050085 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +000086 int new = 0;
87 uint64_t dblock;
88 int error;
Steven Whitehousefd88de562006-05-05 16:59:11 -040089 int boundary;
David Teiglandb3b94fa2006-01-16 16:50:04 +000090
Steven Whitehousefd88de562006-05-05 16:59:11 -040091 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
David Teiglandb3b94fa2006-01-16 16:50:04 +000092 if (error)
93 return error;
94
95 if (dblock)
96 map_bh(bh_result, inode->i_sb, dblock);
97 else if (gfs2_assert_withdraw(ip->i_sbd, !create))
98 error = -EIO;
Steven Whitehousefd88de562006-05-05 16:59:11 -040099 if (boundary)
100 set_buffer_boundary(bh_result);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000101
102 return error;
103}
104
David Teiglandb3b94fa2006-01-16 16:50:04 +0000105/**
106 * gfs2_writepage - Write complete page
107 * @page: Page to write
108 *
109 * Returns: errno
110 *
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000111 * Some of this is copied from block_write_full_page() although we still
112 * call it to do most of the work.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 */
114
115static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
116{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000117 struct inode *inode = page->mapping->host;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500118 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119 struct gfs2_sbd *sdp = ip->i_sbd;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000120 loff_t i_size = i_size_read(inode);
121 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
122 unsigned offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000123 int error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000124 int done_trans = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
127 unlock_page(page);
128 return -EIO;
129 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500130 if (current->journal_info)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000131 goto out_ignore;
132
133 /* Is the page fully outside i_size? (truncate in progress) */
134 offset = i_size & (PAGE_CACHE_SIZE-1);
Steven Whitehoused2d7b8a2006-05-02 12:09:42 -0400135 if (page->index > end_index || (page->index == end_index && !offset)) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000136 page->mapping->a_ops->invalidatepage(page, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137 unlock_page(page);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000138 return 0; /* don't care */
139 }
140
141 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
142 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
143 if (error)
144 goto out_ignore;
145 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
146 done_trans = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000147 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148 error = block_write_full_page(page, get_block_noalloc, wbc);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000149 if (done_trans)
150 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151 gfs2_meta_cache_flush(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000153
154out_ignore:
155 redirty_page_for_writepage(wbc, page);
156 unlock_page(page);
157 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158}
159
Steven Whitehousefd88de562006-05-05 16:59:11 -0400160static int zero_readpage(struct page *page)
161{
162 void *kaddr;
163
164 kaddr = kmap_atomic(page, KM_USER0);
165 memset(kaddr, 0, PAGE_CACHE_SIZE);
166 kunmap_atomic(page, KM_USER0);
167
168 SetPageUptodate(page);
169
170 return 0;
171}
172
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173/**
174 * stuffed_readpage - Fill in a Linux page with stuffed file data
175 * @ip: the inode
176 * @page: the page
177 *
178 * Returns: errno
179 */
180
181static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
182{
183 struct buffer_head *dibh;
184 void *kaddr;
185 int error;
186
Steven Whitehousefd88de562006-05-05 16:59:11 -0400187 /* Only the first page of a stuffed file might contain data */
188 if (unlikely(page->index))
189 return zero_readpage(page);
190
David Teiglandb3b94fa2006-01-16 16:50:04 +0000191 error = gfs2_meta_inode_buffer(ip, &dibh);
192 if (error)
193 return error;
194
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000195 kaddr = kmap_atomic(page, KM_USER0);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400196 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
David Teiglandb3b94fa2006-01-16 16:50:04 +0000197 ip->i_di.di_size);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400198 memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000199 kunmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000200
201 brelse(dibh);
202
203 SetPageUptodate(page);
204
205 return 0;
206}
207
David Teiglandb3b94fa2006-01-16 16:50:04 +0000208
209/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210 * gfs2_readpage - readpage with locking
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000211 * @file: The file to read a page for. N.B. This may be NULL if we are
212 * reading an internal file.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000213 * @page: The page to read
214 *
215 * Returns: errno
216 */
217
218static int gfs2_readpage(struct file *file, struct page *page)
219{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500220 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000221 struct gfs2_sbd *sdp = ip->i_sbd;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000222 struct gfs2_holder gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000223 int error;
224
Steven Whitehousefd88de562006-05-05 16:59:11 -0400225 if (likely(file != &gfs2_internal_file_sentinal)) {
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400226 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh);
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000227 error = gfs2_glock_nq_m_atime(1, &gh);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400228 if (unlikely(error))
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000229 goto out_unlock;
230 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000232 if (gfs2_is_stuffed(ip)) {
Steven Whitehousefd88de562006-05-05 16:59:11 -0400233 error = stuffed_readpage(ip, page);
234 unlock_page(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235 } else
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000236 error = mpage_readpage(page, gfs2_get_block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000237
238 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
239 error = -EIO;
240
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000241 if (file != &gfs2_internal_file_sentinal) {
242 gfs2_glock_dq_m(1, &gh);
243 gfs2_holder_uninit(&gh);
244 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000245out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000247out_unlock:
248 unlock_page(page);
Steven Whitehousefd88de562006-05-05 16:59:11 -0400249 if (file != &gfs2_internal_file_sentinal)
250 gfs2_holder_uninit(&gh);
251 goto out;
252}
253
254#define list_to_page(head) (list_entry((head)->prev, struct page, lru))
255
256/**
257 * gfs2_readpages - Read a bunch of pages at once
258 *
259 * Some notes:
260 * 1. This is only for readahead, so we can simply ignore any things
261 * which are slightly inconvenient (such as locking conflicts between
262 * the page lock and the glock) and return having done no I/O. Its
263 * obviously not something we'd want to do on too regular a basis.
264 * Any I/O we ignore at this time will be done via readpage later.
265 * 2. We have to handle stuffed files here too.
266 * 3. mpage_readpages() does most of the heavy lifting in the common case.
267 * 4. gfs2_get_block() is relied upon to set BH_Boundary in the right places.
268 * 5. We use LM_FLAG_TRY_1CB here, effectively we then have lock-ahead as
269 * well as read-ahead.
270 */
271static int gfs2_readpages(struct file *file, struct address_space *mapping,
272 struct list_head *pages, unsigned nr_pages)
273{
274 struct inode *inode = mapping->host;
275 struct gfs2_inode *ip = inode->u.generic_ip;
276 struct gfs2_sbd *sdp = ip->i_sbd;
277 struct gfs2_holder gh;
278 unsigned page_idx;
279 int ret;
280
281 if (likely(file != &gfs2_internal_file_sentinal)) {
282 gfs2_holder_init(ip->i_gl, LM_ST_SHARED,
283 LM_FLAG_TRY_1CB|GL_ATIME|GL_AOP, &gh);
284 ret = gfs2_glock_nq_m_atime(1, &gh);
285 if (ret == GLR_TRYFAILED)
286 goto out_noerror;
287 if (unlikely(ret))
288 goto out_unlock;
289 }
290
291 if (gfs2_is_stuffed(ip)) {
292 struct pagevec lru_pvec;
293 pagevec_init(&lru_pvec, 0);
294 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
295 struct page *page = list_to_page(pages);
296 list_del(&page->lru);
297 if (!add_to_page_cache(page, mapping,
298 page->index, GFP_KERNEL)) {
299 ret = stuffed_readpage(ip, page);
300 unlock_page(page);
301 if (!pagevec_add(&lru_pvec, page))
302 __pagevec_lru_add(&lru_pvec);
303 }
304 page_cache_release(page);
305 }
306 pagevec_lru_add(&lru_pvec);
307 ret = 0;
308 } else {
309 /* What we really want to do .... */
310 ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
311 }
312
313 if (likely(file != &gfs2_internal_file_sentinal)) {
314 gfs2_glock_dq_m(1, &gh);
315 gfs2_holder_uninit(&gh);
316 }
317out:
318 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
319 ret = -EIO;
320 return ret;
321out_noerror:
322 ret = 0;
323out_unlock:
324 /* unlock all pages, we can't do any I/O right now */
325 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
326 struct page *page = list_to_page(pages);
327 list_del(&page->lru);
328 unlock_page(page);
329 page_cache_release(page);
330 }
331 if (likely(file != &gfs2_internal_file_sentinal))
332 gfs2_holder_uninit(&gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000333 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334}
335
336/**
337 * gfs2_prepare_write - Prepare to write a page to a file
338 * @file: The file to write to
339 * @page: The page which is to be prepared for writing
340 * @from: From (byte range within page)
341 * @to: To (byte range within page)
342 *
343 * Returns: errno
344 */
345
346static int gfs2_prepare_write(struct file *file, struct page *page,
347 unsigned from, unsigned to)
348{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500349 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 struct gfs2_sbd *sdp = ip->i_sbd;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000351 unsigned int data_blocks, ind_blocks, rblocks;
352 int alloc_required;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353 int error = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000354 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
355 loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
356 struct gfs2_alloc *al;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357
Steven Whitehousefe1bded2006-04-18 10:09:15 -0400358 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000359 error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
360 if (error)
361 goto out_uninit;
362
363 gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
364
365 error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
366 if (error)
367 goto out_unlock;
368
369
370 if (alloc_required) {
371 al = gfs2_alloc_get(ip);
372
373 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
374 if (error)
375 goto out_alloc_put;
376
377 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
378 if (error)
379 goto out_qunlock;
380
381 al->al_requested = data_blocks + ind_blocks;
382 error = gfs2_inplace_reserve(ip);
383 if (error)
384 goto out_qunlock;
385 }
386
387 rblocks = RES_DINODE + ind_blocks;
388 if (gfs2_is_jdata(ip))
389 rblocks += data_blocks ? data_blocks : 1;
390 if (ind_blocks || data_blocks)
391 rblocks += RES_STATFS + RES_QUOTA;
392
393 error = gfs2_trans_begin(sdp, rblocks, 0);
394 if (error)
395 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000396
397 if (gfs2_is_stuffed(ip)) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000398 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500399 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
400 page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000401 if (error == 0)
402 goto prepare_write;
403 } else if (!PageUptodate(page))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404 error = stuffed_readpage(ip, page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000405 goto out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000406 }
407
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000408prepare_write:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000409 error = block_prepare_write(page, from, to, gfs2_get_block);
410
411out:
412 if (error) {
413 gfs2_trans_end(sdp);
414 if (alloc_required) {
415 gfs2_inplace_release(ip);
416out_qunlock:
417 gfs2_quota_unlock(ip);
418out_alloc_put:
419 gfs2_alloc_put(ip);
420 }
421out_unlock:
422 gfs2_glock_dq_m(1, &ip->i_gh);
423out_uninit:
424 gfs2_holder_uninit(&ip->i_gh);
425 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000426
427 return error;
428}
429
430/**
431 * gfs2_commit_write - Commit write to a file
432 * @file: The file to write to
433 * @page: The page containing the data
434 * @from: From (byte range within page)
435 * @to: To (byte range within page)
436 *
437 * Returns: errno
438 */
439
440static int gfs2_commit_write(struct file *file, struct page *page,
441 unsigned from, unsigned to)
442{
443 struct inode *inode = page->mapping->host;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500444 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000445 struct gfs2_sbd *sdp = ip->i_sbd;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000446 int error = -EOPNOTSUPP;
447 struct buffer_head *dibh;
448 struct gfs2_alloc *al = &ip->i_alloc;;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000450 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
451 goto fail_nounlock;
452
453 error = gfs2_meta_inode_buffer(ip, &dibh);
454 if (error)
455 goto fail_endtrans;
456
457 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
458
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 if (gfs2_is_stuffed(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000460 uint64_t file_size;
461 void *kaddr;
462
463 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
464
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000465 kaddr = kmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000466 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000467 (char *)kaddr + from, to - from);
468 kunmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000469
470 SetPageUptodate(page);
471
472 if (inode->i_size < file_size)
473 i_size_write(inode, file_size);
474 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500475 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
476 gfs2_is_jdata(ip))
Steven Whitehouse257f9b42006-01-31 10:00:25 +0000477 gfs2_page_add_databufs(ip, page, from, to);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478 error = generic_commit_write(file, page, from, to);
479 if (error)
480 goto fail;
481 }
482
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000483 if (ip->i_di.di_size < inode->i_size)
484 ip->i_di.di_size = inode->i_size;
485
486 gfs2_dinode_out(&ip->i_di, dibh->b_data);
487 brelse(dibh);
488 gfs2_trans_end(sdp);
489 if (al->al_requested) {
490 gfs2_inplace_release(ip);
491 gfs2_quota_unlock(ip);
492 gfs2_alloc_put(ip);
493 }
494 gfs2_glock_dq_m(1, &ip->i_gh);
495 gfs2_holder_uninit(&ip->i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000496 return 0;
497
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000498fail:
499 brelse(dibh);
500fail_endtrans:
501 gfs2_trans_end(sdp);
502 if (al->al_requested) {
503 gfs2_inplace_release(ip);
504 gfs2_quota_unlock(ip);
505 gfs2_alloc_put(ip);
506 }
507 gfs2_glock_dq_m(1, &ip->i_gh);
508 gfs2_holder_uninit(&ip->i_gh);
509fail_nounlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000510 ClearPageUptodate(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000511 return error;
512}
513
514/**
515 * gfs2_bmap - Block map function
516 * @mapping: Address space info
517 * @lblock: The block to map
518 *
519 * Returns: The disk address for the block or 0 on hole or error
520 */
521
522static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
523{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500524 struct gfs2_inode *ip = mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000525 struct gfs2_holder i_gh;
526 sector_t dblock = 0;
527 int error;
528
David Teiglandb3b94fa2006-01-16 16:50:04 +0000529 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
530 if (error)
531 return 0;
532
533 if (!gfs2_is_stuffed(ip))
Steven Whitehouse4ff14672006-01-30 09:39:10 +0000534 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535
536 gfs2_glock_dq_uninit(&i_gh);
537
538 return dblock;
539}
540
541static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
542{
Steven Whitehouse64fb4eb2006-01-18 13:14:40 +0000543 struct gfs2_bufdata *bd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544
545 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500546 bd = bh->b_private;
Steven Whitehouse64fb4eb2006-01-18 13:14:40 +0000547 if (bd) {
548 bd->bd_bh = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500549 bh->b_private = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000550 gfs2_log_unlock(sdp);
551 brelse(bh);
552 } else
553 gfs2_log_unlock(sdp);
554
555 lock_buffer(bh);
556 clear_buffer_dirty(bh);
557 bh->b_bdev = NULL;
558 clear_buffer_mapped(bh);
559 clear_buffer_req(bh);
560 clear_buffer_new(bh);
561 clear_buffer_delay(bh);
562 unlock_buffer(bh);
563}
564
Steven Whitehouse8628de02006-03-31 16:48:41 -0500565static void gfs2_invalidatepage(struct page *page, unsigned long offset)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500567 struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000568 struct buffer_head *head, *bh, *next;
569 unsigned int curr_off = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570
571 BUG_ON(!PageLocked(page));
572 if (!page_has_buffers(page))
Steven Whitehouse8628de02006-03-31 16:48:41 -0500573 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000574
575 bh = head = page_buffers(page);
576 do {
577 unsigned int next_off = curr_off + bh->b_size;
578 next = bh->b_this_page;
579
580 if (offset <= curr_off)
581 discard_buffer(sdp, bh);
582
583 curr_off = next_off;
584 bh = next;
585 } while (bh != head);
586
587 if (!offset)
Steven Whitehouse8628de02006-03-31 16:48:41 -0500588 try_to_release_page(page, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000589
Steven Whitehouse8628de02006-03-31 16:48:41 -0500590 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591}
592
Steven Whitehoused1665e42006-02-14 11:54:42 +0000593static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
594 loff_t offset, unsigned long nr_segs)
595{
596 struct file *file = iocb->ki_filp;
597 struct inode *inode = file->f_mapping->host;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500598 struct gfs2_inode *ip = inode->u.generic_ip;
Steven Whitehoused1665e42006-02-14 11:54:42 +0000599 struct gfs2_holder gh;
600 int rv;
601
602 /*
603 * Shared lock, even though its write, since we do no allocation
604 * on this path. All we need change is atime.
605 */
606 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
607 rv = gfs2_glock_nq_m_atime(1, &gh);
608 if (rv)
609 goto out;
610
611 /*
612 * Should we return an error here? I can't see that O_DIRECT for
613 * a journaled file makes any sense. For now we'll silently fall
614 * back to buffered I/O, likewise we do the same for stuffed
615 * files since they are (a) small and (b) unaligned.
616 */
617 if (gfs2_is_jdata(ip))
618 goto out;
619
620 if (gfs2_is_stuffed(ip))
621 goto out;
622
623 rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
Steven Whitehouse8628de02006-03-31 16:48:41 -0500624 iov, offset, nr_segs, gfs2_get_block,
Steven Whitehoused1665e42006-02-14 11:54:42 +0000625 NULL, DIO_OWN_LOCKING);
626out:
627 gfs2_glock_dq_m(1, &gh);
628 gfs2_holder_uninit(&gh);
629
630 return rv;
631}
632
633/**
634 * gfs2_direct_IO
635 *
636 * This is called with a shared lock already held for the read path.
637 * Currently, no locks are held when the write path is called.
638 */
639static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
640 const struct iovec *iov, loff_t offset,
641 unsigned long nr_segs)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000642{
643 struct file *file = iocb->ki_filp;
644 struct inode *inode = file->f_mapping->host;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500645 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000646 struct gfs2_sbd *sdp = ip->i_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000647
Steven Whitehoused1665e42006-02-14 11:54:42 +0000648 if (rw == WRITE)
649 return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
650
651 if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
652 gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000653 return -EINVAL;
654
Steven Whitehoused1665e42006-02-14 11:54:42 +0000655 return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
Steven Whitehouse8628de02006-03-31 16:48:41 -0500656 offset, nr_segs, gfs2_get_block, NULL,
Steven Whitehoused1665e42006-02-14 11:54:42 +0000657 DIO_OWN_LOCKING);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000658}
659
660struct address_space_operations gfs2_file_aops = {
661 .writepage = gfs2_writepage,
662 .readpage = gfs2_readpage,
Steven Whitehousefd88de562006-05-05 16:59:11 -0400663 .readpages = gfs2_readpages,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000664 .sync_page = block_sync_page,
665 .prepare_write = gfs2_prepare_write,
666 .commit_write = gfs2_commit_write,
667 .bmap = gfs2_bmap,
668 .invalidatepage = gfs2_invalidatepage,
669 .direct_IO = gfs2_direct_IO,
670};
671