2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
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.
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>
16 #include <linux/mpage.h>
18 #include <linux/gfs2_ondisk.h>
19 #include <asm/semaphore.h>
22 #include "lm_interface.h"
29 #include "ops_address.h"
38 * gfs2_get_block - Fills in a buffer head with details about a block
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
47 int gfs2_get_block(struct inode *inode, sector_t lblock,
48 struct buffer_head *bh_result, int create)
50 struct gfs2_inode *ip = inode->u.generic_ip;
55 error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
62 map_bh(bh_result, inode->i_sb, dblock);
64 set_buffer_new(bh_result);
70 * get_block_noalloc - Fills in a buffer head with details about a block
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
79 static int get_block_noalloc(struct inode *inode, sector_t lblock,
80 struct buffer_head *bh_result, int create)
82 struct gfs2_inode *ip = inode->u.generic_ip;
87 error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
92 map_bh(bh_result, inode->i_sb, dblock);
93 else if (gfs2_assert_withdraw(ip->i_sbd, !create))
100 * gfs2_writepage - Write complete page
101 * @page: Page to write
105 * Some of this is copied from block_write_full_page() although we still
106 * call it to do most of the work.
109 static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
111 struct inode *inode = page->mapping->host;
112 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
113 struct gfs2_sbd *sdp = ip->i_sbd;
114 loff_t i_size = i_size_read(inode);
115 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
120 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
124 if (current->journal_info)
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);
132 return 0; /* don't care */
135 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
136 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
139 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
142 error = block_write_full_page(page, get_block_noalloc, wbc);
145 gfs2_meta_cache_flush(ip);
149 redirty_page_for_writepage(wbc, page);
155 * stuffed_readpage - Fill in a Linux page with stuffed file data
162 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
164 struct buffer_head *dibh;
168 error = gfs2_meta_inode_buffer(ip, &dibh);
172 kaddr = kmap_atomic(page, KM_USER0);
173 memcpy((char *)kaddr,
174 dibh->b_data + sizeof(struct gfs2_dinode),
176 memset((char *)kaddr + ip->i_di.di_size,
178 PAGE_CACHE_SIZE - ip->i_di.di_size);
179 kunmap_atomic(page, KM_USER0);
183 SetPageUptodate(page);
188 static int zero_readpage(struct page *page)
192 kaddr = kmap_atomic(page, KM_USER0);
193 memset(kaddr, 0, PAGE_CACHE_SIZE);
194 kunmap_atomic(page, KM_USER0);
196 SetPageUptodate(page);
203 * gfs2_readpage - readpage with locking
204 * @file: The file to read a page for. N.B. This may be NULL if we are
205 * reading an internal file.
206 * @page: The page to read
211 static int gfs2_readpage(struct file *file, struct page *page)
213 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
214 struct gfs2_sbd *sdp = ip->i_sbd;
215 struct gfs2_holder gh;
218 if (file != &gfs2_internal_file_sentinal) {
219 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh);
220 error = gfs2_glock_nq_m_atime(1, &gh);
225 if (gfs2_is_stuffed(ip)) {
227 error = stuffed_readpage(ip, page);
230 error = zero_readpage(page);
232 error = mpage_readpage(page, gfs2_get_block);
234 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
237 if (file != &gfs2_internal_file_sentinal) {
238 gfs2_glock_dq_m(1, &gh);
239 gfs2_holder_uninit(&gh);
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)
258 static int gfs2_prepare_write(struct file *file, struct page *page,
259 unsigned from, unsigned to)
261 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
262 struct gfs2_sbd *sdp = ip->i_sbd;
263 unsigned int data_blocks, ind_blocks, rblocks;
266 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;
270 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh);
271 error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
275 gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
277 error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
282 if (alloc_required) {
283 al = gfs2_alloc_get(ip);
285 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
289 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
293 al->al_requested = data_blocks + ind_blocks;
294 error = gfs2_inplace_reserve(ip);
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;
305 error = gfs2_trans_begin(sdp, rblocks, 0);
309 if (gfs2_is_stuffed(ip)) {
310 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
311 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
315 } else if (!PageUptodate(page))
316 error = stuffed_readpage(ip, page);
321 error = block_prepare_write(page, from, to, gfs2_get_block);
326 if (alloc_required) {
327 gfs2_inplace_release(ip);
329 gfs2_quota_unlock(ip);
334 gfs2_glock_dq_m(1, &ip->i_gh);
336 gfs2_holder_uninit(&ip->i_gh);
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)
352 static int gfs2_commit_write(struct file *file, struct page *page,
353 unsigned from, unsigned to)
355 struct inode *inode = page->mapping->host;
356 struct gfs2_inode *ip = inode->u.generic_ip;
357 struct gfs2_sbd *sdp = ip->i_sbd;
358 int error = -EOPNOTSUPP;
359 struct buffer_head *dibh;
360 struct gfs2_alloc *al = &ip->i_alloc;;
362 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
365 error = gfs2_meta_inode_buffer(ip, &dibh);
369 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
371 if (gfs2_is_stuffed(ip)) {
375 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
377 kaddr = kmap_atomic(page, KM_USER0);
378 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
379 (char *)kaddr + from, to - from);
380 kunmap_atomic(page, KM_USER0);
382 SetPageUptodate(page);
384 if (inode->i_size < file_size)
385 i_size_write(inode, file_size);
387 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
389 gfs2_page_add_databufs(ip, page, from, to);
390 error = generic_commit_write(file, page, from, to);
395 if (ip->i_di.di_size < inode->i_size)
396 ip->i_di.di_size = inode->i_size;
398 gfs2_dinode_out(&ip->i_di, dibh->b_data);
401 if (al->al_requested) {
402 gfs2_inplace_release(ip);
403 gfs2_quota_unlock(ip);
406 gfs2_glock_dq_m(1, &ip->i_gh);
407 gfs2_holder_uninit(&ip->i_gh);
414 if (al->al_requested) {
415 gfs2_inplace_release(ip);
416 gfs2_quota_unlock(ip);
419 gfs2_glock_dq_m(1, &ip->i_gh);
420 gfs2_holder_uninit(&ip->i_gh);
422 ClearPageUptodate(page);
427 * gfs2_bmap - Block map function
428 * @mapping: Address space info
429 * @lblock: The block to map
431 * Returns: The disk address for the block or 0 on hole or error
434 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
436 struct gfs2_inode *ip = mapping->host->u.generic_ip;
437 struct gfs2_holder i_gh;
441 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
445 if (!gfs2_is_stuffed(ip))
446 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
448 gfs2_glock_dq_uninit(&i_gh);
453 static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
455 struct gfs2_bufdata *bd;
461 bh->b_private = NULL;
462 gfs2_log_unlock(sdp);
465 gfs2_log_unlock(sdp);
468 clear_buffer_dirty(bh);
470 clear_buffer_mapped(bh);
471 clear_buffer_req(bh);
472 clear_buffer_new(bh);
473 clear_buffer_delay(bh);
477 static void gfs2_invalidatepage(struct page *page, unsigned long offset)
479 struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
480 struct buffer_head *head, *bh, *next;
481 unsigned int curr_off = 0;
483 BUG_ON(!PageLocked(page));
484 if (!page_has_buffers(page))
487 bh = head = page_buffers(page);
489 unsigned int next_off = curr_off + bh->b_size;
490 next = bh->b_this_page;
492 if (offset <= curr_off)
493 discard_buffer(sdp, bh);
497 } while (bh != head);
500 try_to_release_page(page, 0);
505 static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
506 loff_t offset, unsigned long nr_segs)
508 struct file *file = iocb->ki_filp;
509 struct inode *inode = file->f_mapping->host;
510 struct gfs2_inode *ip = inode->u.generic_ip;
511 struct gfs2_holder gh;
515 * Shared lock, even though its write, since we do no allocation
516 * on this path. All we need change is atime.
518 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
519 rv = gfs2_glock_nq_m_atime(1, &gh);
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.
529 if (gfs2_is_jdata(ip))
532 if (gfs2_is_stuffed(ip))
535 rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
536 iov, offset, nr_segs, gfs2_get_block,
537 NULL, DIO_OWN_LOCKING);
539 gfs2_glock_dq_m(1, &gh);
540 gfs2_holder_uninit(&gh);
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.
551 static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
552 const struct iovec *iov, loff_t offset,
553 unsigned long nr_segs)
555 struct file *file = iocb->ki_filp;
556 struct inode *inode = file->f_mapping->host;
557 struct gfs2_inode *ip = inode->u.generic_ip;
558 struct gfs2_sbd *sdp = ip->i_sbd;
561 return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
563 if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
564 gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
567 return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
568 offset, nr_segs, gfs2_get_block, NULL,
572 struct 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,
579 .invalidatepage = gfs2_invalidatepage,
580 .direct_IO = gfs2_direct_IO,