blob: 3fd8c6ec256cc4b692a88e4a1db01d69c57d8bb5 [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 Whitehouse9b124fb2006-01-30 11:55:32 +000016#include <linux/mpage.h>
Steven Whitehoused1665e42006-02-14 11:54:42 +000017#include <linux/fs.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000019#include <asm/semaphore.h>
20
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"
David Teiglandb3b94fa2006-01-16 16:50:04 +000036
37/**
Steven Whitehouse4ff14672006-01-30 09:39:10 +000038 * gfs2_get_block - Fills in a buffer head with details about a block
David Teiglandb3b94fa2006-01-16 16:50:04 +000039 * @inode: The inode
40 * @lblock: The block number to look up
41 * @bh_result: The buffer head to return the result in
42 * @create: Non-zero if we may add block to the file
43 *
44 * Returns: errno
45 */
46
Steven Whitehouse4ff14672006-01-30 09:39:10 +000047int gfs2_get_block(struct inode *inode, sector_t lblock,
48 struct buffer_head *bh_result, int create)
David Teiglandb3b94fa2006-01-16 16:50:04 +000049{
Steven Whitehouse5c676f62006-02-27 17:23:27 -050050 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 int new = create;
52 uint64_t dblock;
53 int error;
54
55 error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
56 if (error)
57 return error;
58
59 if (!dblock)
60 return 0;
61
62 map_bh(bh_result, inode->i_sb, dblock);
63 if (new)
64 set_buffer_new(bh_result);
65
66 return 0;
67}
68
69/**
70 * get_block_noalloc - Fills in a buffer head with details about a block
71 * @inode: The inode
72 * @lblock: The block number to look up
73 * @bh_result: The buffer head to return the result in
74 * @create: Non-zero if we may add block to the file
75 *
76 * Returns: errno
77 */
78
79static int get_block_noalloc(struct inode *inode, sector_t lblock,
80 struct buffer_head *bh_result, int create)
81{
Steven Whitehouse5c676f62006-02-27 17:23:27 -050082 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +000083 int new = 0;
84 uint64_t dblock;
85 int error;
86
87 error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
88 if (error)
89 return error;
90
91 if (dblock)
92 map_bh(bh_result, inode->i_sb, dblock);
93 else if (gfs2_assert_withdraw(ip->i_sbd, !create))
94 error = -EIO;
95
96 return error;
97}
98
David Teiglandb3b94fa2006-01-16 16:50:04 +000099/**
100 * gfs2_writepage - Write complete page
101 * @page: Page to write
102 *
103 * Returns: errno
104 *
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000105 * Some of this is copied from block_write_full_page() although we still
106 * call it to do most of the work.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000107 */
108
109static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
110{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000111 struct inode *inode = page->mapping->host;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500112 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113 struct gfs2_sbd *sdp = ip->i_sbd;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000114 loff_t i_size = i_size_read(inode);
115 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
116 unsigned offset;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117 int error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000118 int done_trans = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
121 unlock_page(page);
122 return -EIO;
123 }
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500124 if (current->journal_info)
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000125 goto out_ignore;
126
127 /* Is the page fully outside i_size? (truncate in progress) */
128 offset = i_size & (PAGE_CACHE_SIZE-1);
129 if (page->index >= end_index+1 || !offset) {
130 page->mapping->a_ops->invalidatepage(page, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000131 unlock_page(page);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000132 return 0; /* don't care */
133 }
134
135 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
136 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
137 if (error)
138 goto out_ignore;
139 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
140 done_trans = 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000142 error = block_write_full_page(page, get_block_noalloc, wbc);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000143 if (done_trans)
144 gfs2_trans_end(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 gfs2_meta_cache_flush(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000147
148out_ignore:
149 redirty_page_for_writepage(wbc, page);
150 unlock_page(page);
151 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152}
153
154/**
155 * stuffed_readpage - Fill in a Linux page with stuffed file data
156 * @ip: the inode
157 * @page: the page
158 *
159 * Returns: errno
160 */
161
162static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
163{
164 struct buffer_head *dibh;
165 void *kaddr;
166 int error;
167
168 error = gfs2_meta_inode_buffer(ip, &dibh);
169 if (error)
170 return error;
171
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000172 kaddr = kmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173 memcpy((char *)kaddr,
174 dibh->b_data + sizeof(struct gfs2_dinode),
175 ip->i_di.di_size);
176 memset((char *)kaddr + ip->i_di.di_size,
177 0,
178 PAGE_CACHE_SIZE - ip->i_di.di_size);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000179 kunmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180
181 brelse(dibh);
182
183 SetPageUptodate(page);
184
185 return 0;
186}
187
188static int zero_readpage(struct page *page)
189{
190 void *kaddr;
191
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000192 kaddr = kmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000193 memset(kaddr, 0, PAGE_CACHE_SIZE);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000194 kunmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195
196 SetPageUptodate(page);
197 unlock_page(page);
198
199 return 0;
200}
201
202/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203 * gfs2_readpage - readpage with locking
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000204 * @file: The file to read a page for. N.B. This may be NULL if we are
205 * reading an internal file.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000206 * @page: The page to read
207 *
208 * Returns: errno
209 */
210
211static int gfs2_readpage(struct file *file, struct page *page)
212{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500213 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000214 struct gfs2_sbd *sdp = ip->i_sbd;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000215 struct gfs2_holder gh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216 int error;
217
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000218 if (file != &gfs2_internal_file_sentinal) {
219 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
220 error = gfs2_glock_nq_m_atime(1, &gh);
221 if (error)
222 goto out_unlock;
223 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000225 if (gfs2_is_stuffed(ip)) {
226 if (!page->index) {
227 error = stuffed_readpage(ip, page);
228 unlock_page(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000229 } else
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000230 error = zero_readpage(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000231 } else
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000232 error = mpage_readpage(page, gfs2_get_block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233
234 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
235 error = -EIO;
236
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000237 if (file != &gfs2_internal_file_sentinal) {
238 gfs2_glock_dq_m(1, &gh);
239 gfs2_holder_uninit(&gh);
240 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000241out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000242 return error;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000243out_unlock:
244 unlock_page(page);
245 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246}
247
248/**
249 * gfs2_prepare_write - Prepare to write a page to a file
250 * @file: The file to write to
251 * @page: The page which is to be prepared for writing
252 * @from: From (byte range within page)
253 * @to: To (byte range within page)
254 *
255 * Returns: errno
256 */
257
258static int gfs2_prepare_write(struct file *file, struct page *page,
259 unsigned from, unsigned to)
260{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500261 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262 struct gfs2_sbd *sdp = ip->i_sbd;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000263 unsigned int data_blocks, ind_blocks, rblocks;
264 int alloc_required;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000265 int error = 0;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000266 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
267 loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
268 struct gfs2_alloc *al;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000270 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
271 error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
272 if (error)
273 goto out_uninit;
274
275 gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
276
277 error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
278 if (error)
279 goto out_unlock;
280
281
282 if (alloc_required) {
283 al = gfs2_alloc_get(ip);
284
285 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
286 if (error)
287 goto out_alloc_put;
288
289 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
290 if (error)
291 goto out_qunlock;
292
293 al->al_requested = data_blocks + ind_blocks;
294 error = gfs2_inplace_reserve(ip);
295 if (error)
296 goto out_qunlock;
297 }
298
299 rblocks = RES_DINODE + ind_blocks;
300 if (gfs2_is_jdata(ip))
301 rblocks += data_blocks ? data_blocks : 1;
302 if (ind_blocks || data_blocks)
303 rblocks += RES_STATFS + RES_QUOTA;
304
305 error = gfs2_trans_begin(sdp, rblocks, 0);
306 if (error)
307 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000308
309 if (gfs2_is_stuffed(ip)) {
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000310 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500311 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
312 page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000313 if (error == 0)
314 goto prepare_write;
315 } else if (!PageUptodate(page))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000316 error = stuffed_readpage(ip, page);
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000317 goto out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000318 }
319
Steven Whitehouse5c4e9e02006-02-15 12:26:19 +0000320prepare_write:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000321 error = block_prepare_write(page, from, to, gfs2_get_block);
322
323out:
324 if (error) {
325 gfs2_trans_end(sdp);
326 if (alloc_required) {
327 gfs2_inplace_release(ip);
328out_qunlock:
329 gfs2_quota_unlock(ip);
330out_alloc_put:
331 gfs2_alloc_put(ip);
332 }
333out_unlock:
334 gfs2_glock_dq_m(1, &ip->i_gh);
335out_uninit:
336 gfs2_holder_uninit(&ip->i_gh);
337 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338
339 return error;
340}
341
342/**
343 * gfs2_commit_write - Commit write to a file
344 * @file: The file to write to
345 * @page: The page containing the data
346 * @from: From (byte range within page)
347 * @to: To (byte range within page)
348 *
349 * Returns: errno
350 */
351
352static int gfs2_commit_write(struct file *file, struct page *page,
353 unsigned from, unsigned to)
354{
355 struct inode *inode = page->mapping->host;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500356 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357 struct gfs2_sbd *sdp = ip->i_sbd;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000358 int error = -EOPNOTSUPP;
359 struct buffer_head *dibh;
360 struct gfs2_alloc *al = &ip->i_alloc;;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000362 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
363 goto fail_nounlock;
364
365 error = gfs2_meta_inode_buffer(ip, &dibh);
366 if (error)
367 goto fail_endtrans;
368
369 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
370
David Teiglandb3b94fa2006-01-16 16:50:04 +0000371 if (gfs2_is_stuffed(ip)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000372 uint64_t file_size;
373 void *kaddr;
374
375 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
376
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000377 kaddr = kmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000378 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000379 (char *)kaddr + from, to - from);
380 kunmap_atomic(page, KM_USER0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381
382 SetPageUptodate(page);
383
384 if (inode->i_size < file_size)
385 i_size_write(inode, file_size);
386 } else {
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500387 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
388 gfs2_is_jdata(ip))
Steven Whitehouse257f9b42006-01-31 10:00:25 +0000389 gfs2_page_add_databufs(ip, page, from, to);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390 error = generic_commit_write(file, page, from, to);
391 if (error)
392 goto fail;
393 }
394
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000395 if (ip->i_di.di_size < inode->i_size)
396 ip->i_di.di_size = inode->i_size;
397
398 gfs2_dinode_out(&ip->i_di, dibh->b_data);
399 brelse(dibh);
400 gfs2_trans_end(sdp);
401 if (al->al_requested) {
402 gfs2_inplace_release(ip);
403 gfs2_quota_unlock(ip);
404 gfs2_alloc_put(ip);
405 }
406 gfs2_glock_dq_m(1, &ip->i_gh);
407 gfs2_holder_uninit(&ip->i_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408 return 0;
409
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000410fail:
411 brelse(dibh);
412fail_endtrans:
413 gfs2_trans_end(sdp);
414 if (al->al_requested) {
415 gfs2_inplace_release(ip);
416 gfs2_quota_unlock(ip);
417 gfs2_alloc_put(ip);
418 }
419 gfs2_glock_dq_m(1, &ip->i_gh);
420 gfs2_holder_uninit(&ip->i_gh);
421fail_nounlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 ClearPageUptodate(page);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000423 return error;
424}
425
426/**
427 * gfs2_bmap - Block map function
428 * @mapping: Address space info
429 * @lblock: The block to map
430 *
431 * Returns: The disk address for the block or 0 on hole or error
432 */
433
434static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
435{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500436 struct gfs2_inode *ip = mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000437 struct gfs2_holder i_gh;
438 sector_t dblock = 0;
439 int error;
440
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
442 if (error)
443 return 0;
444
445 if (!gfs2_is_stuffed(ip))
Steven Whitehouse4ff14672006-01-30 09:39:10 +0000446 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447
448 gfs2_glock_dq_uninit(&i_gh);
449
450 return dblock;
451}
452
453static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
454{
Steven Whitehouse64fb4eb2006-01-18 13:14:40 +0000455 struct gfs2_bufdata *bd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456
457 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500458 bd = bh->b_private;
Steven Whitehouse64fb4eb2006-01-18 13:14:40 +0000459 if (bd) {
460 bd->bd_bh = NULL;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500461 bh->b_private = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000462 gfs2_log_unlock(sdp);
463 brelse(bh);
464 } else
465 gfs2_log_unlock(sdp);
466
467 lock_buffer(bh);
468 clear_buffer_dirty(bh);
469 bh->b_bdev = NULL;
470 clear_buffer_mapped(bh);
471 clear_buffer_req(bh);
472 clear_buffer_new(bh);
473 clear_buffer_delay(bh);
474 unlock_buffer(bh);
475}
476
Steven Whitehouse8628de02006-03-31 16:48:41 -0500477static void gfs2_invalidatepage(struct page *page, unsigned long offset)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000478{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500479 struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000480 struct buffer_head *head, *bh, *next;
481 unsigned int curr_off = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000482
483 BUG_ON(!PageLocked(page));
484 if (!page_has_buffers(page))
Steven Whitehouse8628de02006-03-31 16:48:41 -0500485 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000486
487 bh = head = page_buffers(page);
488 do {
489 unsigned int next_off = curr_off + bh->b_size;
490 next = bh->b_this_page;
491
492 if (offset <= curr_off)
493 discard_buffer(sdp, bh);
494
495 curr_off = next_off;
496 bh = next;
497 } while (bh != head);
498
499 if (!offset)
Steven Whitehouse8628de02006-03-31 16:48:41 -0500500 try_to_release_page(page, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000501
Steven Whitehouse8628de02006-03-31 16:48:41 -0500502 return;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503}
504
Steven Whitehoused1665e42006-02-14 11:54:42 +0000505static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
506 loff_t offset, unsigned long nr_segs)
507{
508 struct file *file = iocb->ki_filp;
509 struct inode *inode = file->f_mapping->host;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500510 struct gfs2_inode *ip = inode->u.generic_ip;
Steven Whitehoused1665e42006-02-14 11:54:42 +0000511 struct gfs2_holder gh;
512 int rv;
513
514 /*
515 * Shared lock, even though its write, since we do no allocation
516 * on this path. All we need change is atime.
517 */
518 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
519 rv = gfs2_glock_nq_m_atime(1, &gh);
520 if (rv)
521 goto out;
522
523 /*
524 * Should we return an error here? I can't see that O_DIRECT for
525 * a journaled file makes any sense. For now we'll silently fall
526 * back to buffered I/O, likewise we do the same for stuffed
527 * files since they are (a) small and (b) unaligned.
528 */
529 if (gfs2_is_jdata(ip))
530 goto out;
531
532 if (gfs2_is_stuffed(ip))
533 goto out;
534
535 rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
Steven Whitehouse8628de02006-03-31 16:48:41 -0500536 iov, offset, nr_segs, gfs2_get_block,
Steven Whitehoused1665e42006-02-14 11:54:42 +0000537 NULL, DIO_OWN_LOCKING);
538out:
539 gfs2_glock_dq_m(1, &gh);
540 gfs2_holder_uninit(&gh);
541
542 return rv;
543}
544
545/**
546 * gfs2_direct_IO
547 *
548 * This is called with a shared lock already held for the read path.
549 * Currently, no locks are held when the write path is called.
550 */
551static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
552 const struct iovec *iov, loff_t offset,
553 unsigned long nr_segs)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000554{
555 struct file *file = iocb->ki_filp;
556 struct inode *inode = file->f_mapping->host;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500557 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000558 struct gfs2_sbd *sdp = ip->i_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000559
Steven Whitehoused1665e42006-02-14 11:54:42 +0000560 if (rw == WRITE)
561 return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
562
563 if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
564 gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000565 return -EINVAL;
566
Steven Whitehoused1665e42006-02-14 11:54:42 +0000567 return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
Steven Whitehouse8628de02006-03-31 16:48:41 -0500568 offset, nr_segs, gfs2_get_block, NULL,
Steven Whitehoused1665e42006-02-14 11:54:42 +0000569 DIO_OWN_LOCKING);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000570}
571
572struct address_space_operations gfs2_file_aops = {
573 .writepage = gfs2_writepage,
574 .readpage = gfs2_readpage,
575 .sync_page = block_sync_page,
576 .prepare_write = gfs2_prepare_write,
577 .commit_write = gfs2_commit_write,
578 .bmap = gfs2_bmap,
579 .invalidatepage = gfs2_invalidatepage,
580 .direct_IO = gfs2_direct_IO,
581};
582