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/pagevec.h>
17 #include <linux/mpage.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <asm/semaphore.h>
23 #include "lm_interface.h"
30 #include "ops_address.h"
39 * gfs2_get_block - Fills in a buffer head with details about a block
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
48 int gfs2_get_block(struct inode *inode, sector_t lblock,
49 struct buffer_head *bh_result, int create)
56 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
63 map_bh(bh_result, inode->i_sb, dblock);
65 set_buffer_new(bh_result);
67 set_buffer_boundary(bh_result);
73 * get_block_noalloc - Fills in a buffer head with details about a block
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
82 static int get_block_noalloc(struct inode *inode, sector_t lblock,
83 struct buffer_head *bh_result, int create)
85 struct gfs2_inode *ip = inode->u.generic_ip;
91 error = gfs2_block_map(inode, lblock, &new, &dblock, &boundary);
96 map_bh(bh_result, inode->i_sb, dblock);
97 else if (gfs2_assert_withdraw(ip->i_sbd, !create))
100 set_buffer_boundary(bh_result);
106 * gfs2_writepage - Write complete page
107 * @page: Page to write
111 * Some of this is copied from block_write_full_page() although we still
112 * call it to do most of the work.
115 static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
117 struct inode *inode = page->mapping->host;
118 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
119 struct gfs2_sbd *sdp = ip->i_sbd;
120 loff_t i_size = i_size_read(inode);
121 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
126 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
130 if (current->journal_info)
133 /* Is the page fully outside i_size? (truncate in progress) */
134 offset = i_size & (PAGE_CACHE_SIZE-1);
135 if (page->index > end_index || (page->index == end_index && !offset)) {
136 page->mapping->a_ops->invalidatepage(page, 0);
138 return 0; /* don't care */
141 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
142 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
145 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
148 error = block_write_full_page(page, get_block_noalloc, wbc);
151 gfs2_meta_cache_flush(ip);
155 redirty_page_for_writepage(wbc, page);
160 static int zero_readpage(struct page *page)
164 kaddr = kmap_atomic(page, KM_USER0);
165 memset(kaddr, 0, PAGE_CACHE_SIZE);
166 kunmap_atomic(page, KM_USER0);
168 SetPageUptodate(page);
174 * stuffed_readpage - Fill in a Linux page with stuffed file data
181 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
183 struct buffer_head *dibh;
187 /* Only the first page of a stuffed file might contain data */
188 if (unlikely(page->index))
189 return zero_readpage(page);
191 error = gfs2_meta_inode_buffer(ip, &dibh);
195 kaddr = kmap_atomic(page, KM_USER0);
196 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
198 memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
199 kunmap_atomic(page, KM_USER0);
203 SetPageUptodate(page);
210 * gfs2_readpage - readpage with locking
211 * @file: The file to read a page for. N.B. This may be NULL if we are
212 * reading an internal file.
213 * @page: The page to read
218 static int gfs2_readpage(struct file *file, struct page *page)
220 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
221 struct gfs2_sbd *sdp = ip->i_sbd;
222 struct gfs2_holder gh;
225 if (likely(file != &gfs2_internal_file_sentinal)) {
226 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh);
227 error = gfs2_glock_nq_m_atime(1, &gh);
232 if (gfs2_is_stuffed(ip)) {
233 error = stuffed_readpage(ip, page);
236 error = mpage_readpage(page, gfs2_get_block);
238 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
241 if (file != &gfs2_internal_file_sentinal) {
242 gfs2_glock_dq_m(1, &gh);
243 gfs2_holder_uninit(&gh);
249 if (file != &gfs2_internal_file_sentinal)
250 gfs2_holder_uninit(&gh);
254 #define list_to_page(head) (list_entry((head)->prev, struct page, lru))
257 * gfs2_readpages - Read a bunch of pages at once
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.
271 static int gfs2_readpages(struct file *file, struct address_space *mapping,
272 struct list_head *pages, unsigned nr_pages)
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;
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)
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);
301 if (!pagevec_add(&lru_pvec, page))
302 __pagevec_lru_add(&lru_pvec);
304 page_cache_release(page);
306 pagevec_lru_add(&lru_pvec);
309 /* What we really want to do .... */
310 ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
313 if (likely(file != &gfs2_internal_file_sentinal)) {
314 gfs2_glock_dq_m(1, &gh);
315 gfs2_holder_uninit(&gh);
318 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
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);
329 page_cache_release(page);
331 if (likely(file != &gfs2_internal_file_sentinal))
332 gfs2_holder_uninit(&gh);
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)
346 static int gfs2_prepare_write(struct file *file, struct page *page,
347 unsigned from, unsigned to)
349 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
350 struct gfs2_sbd *sdp = ip->i_sbd;
351 unsigned int data_blocks, ind_blocks, rblocks;
354 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;
358 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh);
359 error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
363 gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
365 error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
370 if (alloc_required) {
371 al = gfs2_alloc_get(ip);
373 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
377 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
381 al->al_requested = data_blocks + ind_blocks;
382 error = gfs2_inplace_reserve(ip);
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;
393 error = gfs2_trans_begin(sdp, rblocks, 0);
397 if (gfs2_is_stuffed(ip)) {
398 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
399 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
403 } else if (!PageUptodate(page))
404 error = stuffed_readpage(ip, page);
409 error = block_prepare_write(page, from, to, gfs2_get_block);
414 if (alloc_required) {
415 gfs2_inplace_release(ip);
417 gfs2_quota_unlock(ip);
422 gfs2_glock_dq_m(1, &ip->i_gh);
424 gfs2_holder_uninit(&ip->i_gh);
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)
440 static int gfs2_commit_write(struct file *file, struct page *page,
441 unsigned from, unsigned to)
443 struct inode *inode = page->mapping->host;
444 struct gfs2_inode *ip = inode->u.generic_ip;
445 struct gfs2_sbd *sdp = ip->i_sbd;
446 int error = -EOPNOTSUPP;
447 struct buffer_head *dibh;
448 struct gfs2_alloc *al = &ip->i_alloc;;
450 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
453 error = gfs2_meta_inode_buffer(ip, &dibh);
457 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
459 if (gfs2_is_stuffed(ip)) {
463 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
465 kaddr = kmap_atomic(page, KM_USER0);
466 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
467 (char *)kaddr + from, to - from);
468 kunmap_atomic(page, KM_USER0);
470 SetPageUptodate(page);
472 if (inode->i_size < file_size)
473 i_size_write(inode, file_size);
475 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
477 gfs2_page_add_databufs(ip, page, from, to);
478 error = generic_commit_write(file, page, from, to);
483 if (ip->i_di.di_size < inode->i_size)
484 ip->i_di.di_size = inode->i_size;
486 gfs2_dinode_out(&ip->i_di, dibh->b_data);
489 if (al->al_requested) {
490 gfs2_inplace_release(ip);
491 gfs2_quota_unlock(ip);
494 gfs2_glock_dq_m(1, &ip->i_gh);
495 gfs2_holder_uninit(&ip->i_gh);
502 if (al->al_requested) {
503 gfs2_inplace_release(ip);
504 gfs2_quota_unlock(ip);
507 gfs2_glock_dq_m(1, &ip->i_gh);
508 gfs2_holder_uninit(&ip->i_gh);
510 ClearPageUptodate(page);
515 * gfs2_bmap - Block map function
516 * @mapping: Address space info
517 * @lblock: The block to map
519 * Returns: The disk address for the block or 0 on hole or error
522 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
524 struct gfs2_inode *ip = mapping->host->u.generic_ip;
525 struct gfs2_holder i_gh;
529 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
533 if (!gfs2_is_stuffed(ip))
534 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
536 gfs2_glock_dq_uninit(&i_gh);
541 static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
543 struct gfs2_bufdata *bd;
549 bh->b_private = NULL;
550 gfs2_log_unlock(sdp);
553 gfs2_log_unlock(sdp);
556 clear_buffer_dirty(bh);
558 clear_buffer_mapped(bh);
559 clear_buffer_req(bh);
560 clear_buffer_new(bh);
561 clear_buffer_delay(bh);
565 static void gfs2_invalidatepage(struct page *page, unsigned long offset)
567 struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
568 struct buffer_head *head, *bh, *next;
569 unsigned int curr_off = 0;
571 BUG_ON(!PageLocked(page));
572 if (!page_has_buffers(page))
575 bh = head = page_buffers(page);
577 unsigned int next_off = curr_off + bh->b_size;
578 next = bh->b_this_page;
580 if (offset <= curr_off)
581 discard_buffer(sdp, bh);
585 } while (bh != head);
588 try_to_release_page(page, 0);
593 static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
594 loff_t offset, unsigned long nr_segs)
596 struct file *file = iocb->ki_filp;
597 struct inode *inode = file->f_mapping->host;
598 struct gfs2_inode *ip = inode->u.generic_ip;
599 struct gfs2_holder gh;
603 * Shared lock, even though its write, since we do no allocation
604 * on this path. All we need change is atime.
606 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
607 rv = gfs2_glock_nq_m_atime(1, &gh);
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.
617 if (gfs2_is_jdata(ip))
620 if (gfs2_is_stuffed(ip))
623 rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
624 iov, offset, nr_segs, gfs2_get_block,
625 NULL, DIO_OWN_LOCKING);
627 gfs2_glock_dq_m(1, &gh);
628 gfs2_holder_uninit(&gh);
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.
639 static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
640 const struct iovec *iov, loff_t offset,
641 unsigned long nr_segs)
643 struct file *file = iocb->ki_filp;
644 struct inode *inode = file->f_mapping->host;
645 struct gfs2_inode *ip = inode->u.generic_ip;
646 struct gfs2_sbd *sdp = ip->i_sbd;
649 return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
651 if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
652 gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
655 return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
656 offset, nr_segs, gfs2_get_block, NULL,
660 struct address_space_operations gfs2_file_aops = {
661 .writepage = gfs2_writepage,
662 .readpage = gfs2_readpage,
663 .readpages = gfs2_readpages,
664 .sync_page = block_sync_page,
665 .prepare_write = gfs2_prepare_write,
666 .commit_write = gfs2_commit_write,
668 .invalidatepage = gfs2_invalidatepage,
669 .direct_IO = gfs2_direct_IO,