xref: /openbmc/linux/fs/gfs2/aops.c (revision d623f60d)
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2008 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 version 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>
16 #include <linux/pagevec.h>
17 #include <linux/mpage.h>
18 #include <linux/fs.h>
19 #include <linux/writeback.h>
20 #include <linux/swap.h>
21 #include <linux/gfs2_ondisk.h>
22 #include <linux/backing-dev.h>
23 #include <linux/uio.h>
24 #include <trace/events/writeback.h>
25 
26 #include "gfs2.h"
27 #include "incore.h"
28 #include "bmap.h"
29 #include "glock.h"
30 #include "inode.h"
31 #include "log.h"
32 #include "meta_io.h"
33 #include "quota.h"
34 #include "trans.h"
35 #include "rgrp.h"
36 #include "super.h"
37 #include "util.h"
38 #include "glops.h"
39 
40 
41 static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
42 				   unsigned int from, unsigned int len)
43 {
44 	struct buffer_head *head = page_buffers(page);
45 	unsigned int bsize = head->b_size;
46 	struct buffer_head *bh;
47 	unsigned int to = from + len;
48 	unsigned int start, end;
49 
50 	for (bh = head, start = 0; bh != head || !start;
51 	     bh = bh->b_this_page, start = end) {
52 		end = start + bsize;
53 		if (end <= from)
54 			continue;
55 		if (start >= to)
56 			break;
57 		set_buffer_uptodate(bh);
58 		gfs2_trans_add_data(ip->i_gl, bh);
59 	}
60 }
61 
62 /**
63  * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
64  * @inode: The inode
65  * @lblock: The block number to look up
66  * @bh_result: The buffer head to return the result in
67  * @create: Non-zero if we may add block to the file
68  *
69  * Returns: errno
70  */
71 
72 static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
73 				  struct buffer_head *bh_result, int create)
74 {
75 	int error;
76 
77 	error = gfs2_block_map(inode, lblock, bh_result, 0);
78 	if (error)
79 		return error;
80 	if (!buffer_mapped(bh_result))
81 		return -EIO;
82 	return 0;
83 }
84 
85 static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
86 				 struct buffer_head *bh_result, int create)
87 {
88 	return gfs2_block_map(inode, lblock, bh_result, 0);
89 }
90 
91 /**
92  * gfs2_writepage_common - Common bits of writepage
93  * @page: The page to be written
94  * @wbc: The writeback control
95  *
96  * Returns: 1 if writepage is ok, otherwise an error code or zero if no error.
97  */
98 
99 static int gfs2_writepage_common(struct page *page,
100 				 struct writeback_control *wbc)
101 {
102 	struct inode *inode = page->mapping->host;
103 	struct gfs2_inode *ip = GFS2_I(inode);
104 	struct gfs2_sbd *sdp = GFS2_SB(inode);
105 	loff_t i_size = i_size_read(inode);
106 	pgoff_t end_index = i_size >> PAGE_SHIFT;
107 	unsigned offset;
108 
109 	if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
110 		goto out;
111 	if (current->journal_info)
112 		goto redirty;
113 	/* Is the page fully outside i_size? (truncate in progress) */
114 	offset = i_size & (PAGE_SIZE-1);
115 	if (page->index > end_index || (page->index == end_index && !offset)) {
116 		page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
117 		goto out;
118 	}
119 	return 1;
120 redirty:
121 	redirty_page_for_writepage(wbc, page);
122 out:
123 	unlock_page(page);
124 	return 0;
125 }
126 
127 /**
128  * gfs2_writepage - Write page for writeback mappings
129  * @page: The page
130  * @wbc: The writeback control
131  *
132  */
133 
134 static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
135 {
136 	int ret;
137 
138 	ret = gfs2_writepage_common(page, wbc);
139 	if (ret <= 0)
140 		return ret;
141 
142 	return nobh_writepage(page, gfs2_get_block_noalloc, wbc);
143 }
144 
145 /* This is the same as calling block_write_full_page, but it also
146  * writes pages outside of i_size
147  */
148 static int gfs2_write_full_page(struct page *page, get_block_t *get_block,
149 				struct writeback_control *wbc)
150 {
151 	struct inode * const inode = page->mapping->host;
152 	loff_t i_size = i_size_read(inode);
153 	const pgoff_t end_index = i_size >> PAGE_SHIFT;
154 	unsigned offset;
155 
156 	/*
157 	 * The page straddles i_size.  It must be zeroed out on each and every
158 	 * writepage invocation because it may be mmapped.  "A file is mapped
159 	 * in multiples of the page size.  For a file that is not a multiple of
160 	 * the  page size, the remaining memory is zeroed when mapped, and
161 	 * writes to that region are not written out to the file."
162 	 */
163 	offset = i_size & (PAGE_SIZE-1);
164 	if (page->index == end_index && offset)
165 		zero_user_segment(page, offset, PAGE_SIZE);
166 
167 	return __block_write_full_page(inode, page, get_block, wbc,
168 				       end_buffer_async_write);
169 }
170 
171 /**
172  * __gfs2_jdata_writepage - The core of jdata writepage
173  * @page: The page to write
174  * @wbc: The writeback control
175  *
176  * This is shared between writepage and writepages and implements the
177  * core of the writepage operation. If a transaction is required then
178  * PageChecked will have been set and the transaction will have
179  * already been started before this is called.
180  */
181 
182 static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
183 {
184 	struct inode *inode = page->mapping->host;
185 	struct gfs2_inode *ip = GFS2_I(inode);
186 	struct gfs2_sbd *sdp = GFS2_SB(inode);
187 
188 	if (PageChecked(page)) {
189 		ClearPageChecked(page);
190 		if (!page_has_buffers(page)) {
191 			create_empty_buffers(page, inode->i_sb->s_blocksize,
192 					     BIT(BH_Dirty)|BIT(BH_Uptodate));
193 		}
194 		gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize);
195 	}
196 	return gfs2_write_full_page(page, gfs2_get_block_noalloc, wbc);
197 }
198 
199 /**
200  * gfs2_jdata_writepage - Write complete page
201  * @page: Page to write
202  * @wbc: The writeback control
203  *
204  * Returns: errno
205  *
206  */
207 
208 static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
209 {
210 	struct inode *inode = page->mapping->host;
211 	struct gfs2_inode *ip = GFS2_I(inode);
212 	struct gfs2_sbd *sdp = GFS2_SB(inode);
213 	int ret;
214 
215 	if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
216 		goto out;
217 	if (PageChecked(page) || current->journal_info)
218 		goto out_ignore;
219 	ret = __gfs2_jdata_writepage(page, wbc);
220 	return ret;
221 
222 out_ignore:
223 	redirty_page_for_writepage(wbc, page);
224 out:
225 	unlock_page(page);
226 	return 0;
227 }
228 
229 /**
230  * gfs2_writepages - Write a bunch of dirty pages back to disk
231  * @mapping: The mapping to write
232  * @wbc: Write-back control
233  *
234  * Used for both ordered and writeback modes.
235  */
236 static int gfs2_writepages(struct address_space *mapping,
237 			   struct writeback_control *wbc)
238 {
239 	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
240 	int ret = mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
241 
242 	/*
243 	 * Even if we didn't write any pages here, we might still be holding
244 	 * dirty pages in the ail. We forcibly flush the ail because we don't
245 	 * want balance_dirty_pages() to loop indefinitely trying to write out
246 	 * pages held in the ail that it can't find.
247 	 */
248 	if (ret == 0)
249 		set_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
250 
251 	return ret;
252 }
253 
254 /**
255  * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
256  * @mapping: The mapping
257  * @wbc: The writeback control
258  * @pvec: The vector of pages
259  * @nr_pages: The number of pages to write
260  * @done_index: Page index
261  *
262  * Returns: non-zero if loop should terminate, zero otherwise
263  */
264 
265 static int gfs2_write_jdata_pagevec(struct address_space *mapping,
266 				    struct writeback_control *wbc,
267 				    struct pagevec *pvec,
268 				    int nr_pages,
269 				    pgoff_t *done_index)
270 {
271 	struct inode *inode = mapping->host;
272 	struct gfs2_sbd *sdp = GFS2_SB(inode);
273 	unsigned nrblocks = nr_pages * (PAGE_SIZE/inode->i_sb->s_blocksize);
274 	int i;
275 	int ret;
276 
277 	ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
278 	if (ret < 0)
279 		return ret;
280 
281 	for(i = 0; i < nr_pages; i++) {
282 		struct page *page = pvec->pages[i];
283 
284 		*done_index = page->index;
285 
286 		lock_page(page);
287 
288 		if (unlikely(page->mapping != mapping)) {
289 continue_unlock:
290 			unlock_page(page);
291 			continue;
292 		}
293 
294 		if (!PageDirty(page)) {
295 			/* someone wrote it for us */
296 			goto continue_unlock;
297 		}
298 
299 		if (PageWriteback(page)) {
300 			if (wbc->sync_mode != WB_SYNC_NONE)
301 				wait_on_page_writeback(page);
302 			else
303 				goto continue_unlock;
304 		}
305 
306 		BUG_ON(PageWriteback(page));
307 		if (!clear_page_dirty_for_io(page))
308 			goto continue_unlock;
309 
310 		trace_wbc_writepage(wbc, inode_to_bdi(inode));
311 
312 		ret = __gfs2_jdata_writepage(page, wbc);
313 		if (unlikely(ret)) {
314 			if (ret == AOP_WRITEPAGE_ACTIVATE) {
315 				unlock_page(page);
316 				ret = 0;
317 			} else {
318 
319 				/*
320 				 * done_index is set past this page,
321 				 * so media errors will not choke
322 				 * background writeout for the entire
323 				 * file. This has consequences for
324 				 * range_cyclic semantics (ie. it may
325 				 * not be suitable for data integrity
326 				 * writeout).
327 				 */
328 				*done_index = page->index + 1;
329 				ret = 1;
330 				break;
331 			}
332 		}
333 
334 		/*
335 		 * We stop writing back only if we are not doing
336 		 * integrity sync. In case of integrity sync we have to
337 		 * keep going until we have written all the pages
338 		 * we tagged for writeback prior to entering this loop.
339 		 */
340 		if (--wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE) {
341 			ret = 1;
342 			break;
343 		}
344 
345 	}
346 	gfs2_trans_end(sdp);
347 	return ret;
348 }
349 
350 /**
351  * gfs2_write_cache_jdata - Like write_cache_pages but different
352  * @mapping: The mapping to write
353  * @wbc: The writeback control
354  *
355  * The reason that we use our own function here is that we need to
356  * start transactions before we grab page locks. This allows us
357  * to get the ordering right.
358  */
359 
360 static int gfs2_write_cache_jdata(struct address_space *mapping,
361 				  struct writeback_control *wbc)
362 {
363 	int ret = 0;
364 	int done = 0;
365 	struct pagevec pvec;
366 	int nr_pages;
367 	pgoff_t uninitialized_var(writeback_index);
368 	pgoff_t index;
369 	pgoff_t end;
370 	pgoff_t done_index;
371 	int cycled;
372 	int range_whole = 0;
373 	int tag;
374 
375 	pagevec_init(&pvec);
376 	if (wbc->range_cyclic) {
377 		writeback_index = mapping->writeback_index; /* prev offset */
378 		index = writeback_index;
379 		if (index == 0)
380 			cycled = 1;
381 		else
382 			cycled = 0;
383 		end = -1;
384 	} else {
385 		index = wbc->range_start >> PAGE_SHIFT;
386 		end = wbc->range_end >> PAGE_SHIFT;
387 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
388 			range_whole = 1;
389 		cycled = 1; /* ignore range_cyclic tests */
390 	}
391 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
392 		tag = PAGECACHE_TAG_TOWRITE;
393 	else
394 		tag = PAGECACHE_TAG_DIRTY;
395 
396 retry:
397 	if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
398 		tag_pages_for_writeback(mapping, index, end);
399 	done_index = index;
400 	while (!done && (index <= end)) {
401 		nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
402 				tag);
403 		if (nr_pages == 0)
404 			break;
405 
406 		ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, &done_index);
407 		if (ret)
408 			done = 1;
409 		if (ret > 0)
410 			ret = 0;
411 		pagevec_release(&pvec);
412 		cond_resched();
413 	}
414 
415 	if (!cycled && !done) {
416 		/*
417 		 * range_cyclic:
418 		 * We hit the last page and there is more work to be done: wrap
419 		 * back to the start of the file
420 		 */
421 		cycled = 1;
422 		index = 0;
423 		end = writeback_index - 1;
424 		goto retry;
425 	}
426 
427 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
428 		mapping->writeback_index = done_index;
429 
430 	return ret;
431 }
432 
433 
434 /**
435  * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
436  * @mapping: The mapping to write
437  * @wbc: The writeback control
438  *
439  */
440 
441 static int gfs2_jdata_writepages(struct address_space *mapping,
442 				 struct writeback_control *wbc)
443 {
444 	struct gfs2_inode *ip = GFS2_I(mapping->host);
445 	struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
446 	int ret;
447 
448 	ret = gfs2_write_cache_jdata(mapping, wbc);
449 	if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
450 		gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
451 			       GFS2_LFC_JDATA_WPAGES);
452 		ret = gfs2_write_cache_jdata(mapping, wbc);
453 	}
454 	return ret;
455 }
456 
457 /**
458  * stuffed_readpage - Fill in a Linux page with stuffed file data
459  * @ip: the inode
460  * @page: the page
461  *
462  * Returns: errno
463  */
464 
465 static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
466 {
467 	struct buffer_head *dibh;
468 	u64 dsize = i_size_read(&ip->i_inode);
469 	void *kaddr;
470 	int error;
471 
472 	/*
473 	 * Due to the order of unstuffing files and ->fault(), we can be
474 	 * asked for a zero page in the case of a stuffed file being extended,
475 	 * so we need to supply one here. It doesn't happen often.
476 	 */
477 	if (unlikely(page->index)) {
478 		zero_user(page, 0, PAGE_SIZE);
479 		SetPageUptodate(page);
480 		return 0;
481 	}
482 
483 	error = gfs2_meta_inode_buffer(ip, &dibh);
484 	if (error)
485 		return error;
486 
487 	kaddr = kmap_atomic(page);
488 	if (dsize > gfs2_max_stuffed_size(ip))
489 		dsize = gfs2_max_stuffed_size(ip);
490 	memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
491 	memset(kaddr + dsize, 0, PAGE_SIZE - dsize);
492 	kunmap_atomic(kaddr);
493 	flush_dcache_page(page);
494 	brelse(dibh);
495 	SetPageUptodate(page);
496 
497 	return 0;
498 }
499 
500 
501 /**
502  * __gfs2_readpage - readpage
503  * @file: The file to read a page for
504  * @page: The page to read
505  *
506  * This is the core of gfs2's readpage. It's used by the internal file
507  * reading code as in that case we already hold the glock. Also it's
508  * called by gfs2_readpage() once the required lock has been granted.
509  */
510 
511 static int __gfs2_readpage(void *file, struct page *page)
512 {
513 	struct gfs2_inode *ip = GFS2_I(page->mapping->host);
514 	struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
515 	int error;
516 
517 	if (gfs2_is_stuffed(ip)) {
518 		error = stuffed_readpage(ip, page);
519 		unlock_page(page);
520 	} else {
521 		error = mpage_readpage(page, gfs2_block_map);
522 	}
523 
524 	if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
525 		return -EIO;
526 
527 	return error;
528 }
529 
530 /**
531  * gfs2_readpage - read a page of a file
532  * @file: The file to read
533  * @page: The page of the file
534  *
535  * This deals with the locking required. We have to unlock and
536  * relock the page in order to get the locking in the right
537  * order.
538  */
539 
540 static int gfs2_readpage(struct file *file, struct page *page)
541 {
542 	struct address_space *mapping = page->mapping;
543 	struct gfs2_inode *ip = GFS2_I(mapping->host);
544 	struct gfs2_holder gh;
545 	int error;
546 
547 	unlock_page(page);
548 	gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
549 	error = gfs2_glock_nq(&gh);
550 	if (unlikely(error))
551 		goto out;
552 	error = AOP_TRUNCATED_PAGE;
553 	lock_page(page);
554 	if (page->mapping == mapping && !PageUptodate(page))
555 		error = __gfs2_readpage(file, page);
556 	else
557 		unlock_page(page);
558 	gfs2_glock_dq(&gh);
559 out:
560 	gfs2_holder_uninit(&gh);
561 	if (error && error != AOP_TRUNCATED_PAGE)
562 		lock_page(page);
563 	return error;
564 }
565 
566 /**
567  * gfs2_internal_read - read an internal file
568  * @ip: The gfs2 inode
569  * @buf: The buffer to fill
570  * @pos: The file position
571  * @size: The amount to read
572  *
573  */
574 
575 int gfs2_internal_read(struct gfs2_inode *ip, char *buf, loff_t *pos,
576                        unsigned size)
577 {
578 	struct address_space *mapping = ip->i_inode.i_mapping;
579 	unsigned long index = *pos / PAGE_SIZE;
580 	unsigned offset = *pos & (PAGE_SIZE - 1);
581 	unsigned copied = 0;
582 	unsigned amt;
583 	struct page *page;
584 	void *p;
585 
586 	do {
587 		amt = size - copied;
588 		if (offset + size > PAGE_SIZE)
589 			amt = PAGE_SIZE - offset;
590 		page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
591 		if (IS_ERR(page))
592 			return PTR_ERR(page);
593 		p = kmap_atomic(page);
594 		memcpy(buf + copied, p + offset, amt);
595 		kunmap_atomic(p);
596 		put_page(page);
597 		copied += amt;
598 		index++;
599 		offset = 0;
600 	} while(copied < size);
601 	(*pos) += size;
602 	return size;
603 }
604 
605 /**
606  * gfs2_readpages - Read a bunch of pages at once
607  * @file: The file to read from
608  * @mapping: Address space info
609  * @pages: List of pages to read
610  * @nr_pages: Number of pages to read
611  *
612  * Some notes:
613  * 1. This is only for readahead, so we can simply ignore any things
614  *    which are slightly inconvenient (such as locking conflicts between
615  *    the page lock and the glock) and return having done no I/O. Its
616  *    obviously not something we'd want to do on too regular a basis.
617  *    Any I/O we ignore at this time will be done via readpage later.
618  * 2. We don't handle stuffed files here we let readpage do the honours.
619  * 3. mpage_readpages() does most of the heavy lifting in the common case.
620  * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
621  */
622 
623 static int gfs2_readpages(struct file *file, struct address_space *mapping,
624 			  struct list_head *pages, unsigned nr_pages)
625 {
626 	struct inode *inode = mapping->host;
627 	struct gfs2_inode *ip = GFS2_I(inode);
628 	struct gfs2_sbd *sdp = GFS2_SB(inode);
629 	struct gfs2_holder gh;
630 	int ret;
631 
632 	gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
633 	ret = gfs2_glock_nq(&gh);
634 	if (unlikely(ret))
635 		goto out_uninit;
636 	if (!gfs2_is_stuffed(ip))
637 		ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
638 	gfs2_glock_dq(&gh);
639 out_uninit:
640 	gfs2_holder_uninit(&gh);
641 	if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
642 		ret = -EIO;
643 	return ret;
644 }
645 
646 /**
647  * gfs2_write_begin - Begin to write to a file
648  * @file: The file to write to
649  * @mapping: The mapping in which to write
650  * @pos: The file offset at which to start writing
651  * @len: Length of the write
652  * @flags: Various flags
653  * @pagep: Pointer to return the page
654  * @fsdata: Pointer to return fs data (unused by GFS2)
655  *
656  * Returns: errno
657  */
658 
659 static int gfs2_write_begin(struct file *file, struct address_space *mapping,
660 			    loff_t pos, unsigned len, unsigned flags,
661 			    struct page **pagep, void **fsdata)
662 {
663 	struct gfs2_inode *ip = GFS2_I(mapping->host);
664 	struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
665 	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
666 	unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
667 	unsigned requested = 0;
668 	int alloc_required;
669 	int error = 0;
670 	pgoff_t index = pos >> PAGE_SHIFT;
671 	unsigned from = pos & (PAGE_SIZE - 1);
672 	struct page *page;
673 
674 	gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
675 	error = gfs2_glock_nq(&ip->i_gh);
676 	if (unlikely(error))
677 		goto out_uninit;
678 	if (&ip->i_inode == sdp->sd_rindex) {
679 		error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE,
680 					   GL_NOCACHE, &m_ip->i_gh);
681 		if (unlikely(error)) {
682 			gfs2_glock_dq(&ip->i_gh);
683 			goto out_uninit;
684 		}
685 	}
686 
687 	alloc_required = gfs2_write_alloc_required(ip, pos, len);
688 
689 	if (alloc_required || gfs2_is_jdata(ip))
690 		gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
691 
692 	if (alloc_required) {
693 		struct gfs2_alloc_parms ap = { .aflags = 0, };
694 		requested = data_blocks + ind_blocks;
695 		ap.target = requested;
696 		error = gfs2_quota_lock_check(ip, &ap);
697 		if (error)
698 			goto out_unlock;
699 
700 		error = gfs2_inplace_reserve(ip, &ap);
701 		if (error)
702 			goto out_qunlock;
703 	}
704 
705 	rblocks = RES_DINODE + ind_blocks;
706 	if (gfs2_is_jdata(ip))
707 		rblocks += data_blocks ? data_blocks : 1;
708 	if (ind_blocks || data_blocks)
709 		rblocks += RES_STATFS + RES_QUOTA;
710 	if (&ip->i_inode == sdp->sd_rindex)
711 		rblocks += 2 * RES_STATFS;
712 	if (alloc_required)
713 		rblocks += gfs2_rg_blocks(ip, requested);
714 
715 	error = gfs2_trans_begin(sdp, rblocks,
716 				 PAGE_SIZE/sdp->sd_sb.sb_bsize);
717 	if (error)
718 		goto out_trans_fail;
719 
720 	error = -ENOMEM;
721 	flags |= AOP_FLAG_NOFS;
722 	page = grab_cache_page_write_begin(mapping, index, flags);
723 	*pagep = page;
724 	if (unlikely(!page))
725 		goto out_endtrans;
726 
727 	if (gfs2_is_stuffed(ip)) {
728 		error = 0;
729 		if (pos + len > gfs2_max_stuffed_size(ip)) {
730 			error = gfs2_unstuff_dinode(ip, page);
731 			if (error == 0)
732 				goto prepare_write;
733 		} else if (!PageUptodate(page)) {
734 			error = stuffed_readpage(ip, page);
735 		}
736 		goto out;
737 	}
738 
739 prepare_write:
740 	error = __block_write_begin(page, from, len, gfs2_block_map);
741 out:
742 	if (error == 0)
743 		return 0;
744 
745 	unlock_page(page);
746 	put_page(page);
747 
748 	gfs2_trans_end(sdp);
749 	if (alloc_required) {
750 		gfs2_inplace_release(ip);
751 		if (pos + len > ip->i_inode.i_size)
752 			gfs2_trim_blocks(&ip->i_inode);
753 	}
754 	goto out_qunlock;
755 
756 out_endtrans:
757 	gfs2_trans_end(sdp);
758 out_trans_fail:
759 	if (alloc_required)
760 		gfs2_inplace_release(ip);
761 out_qunlock:
762 	if (alloc_required)
763 		gfs2_quota_unlock(ip);
764 out_unlock:
765 	if (&ip->i_inode == sdp->sd_rindex) {
766 		gfs2_glock_dq(&m_ip->i_gh);
767 		gfs2_holder_uninit(&m_ip->i_gh);
768 	}
769 	gfs2_glock_dq(&ip->i_gh);
770 out_uninit:
771 	gfs2_holder_uninit(&ip->i_gh);
772 	return error;
773 }
774 
775 /**
776  * adjust_fs_space - Adjusts the free space available due to gfs2_grow
777  * @inode: the rindex inode
778  */
779 static void adjust_fs_space(struct inode *inode)
780 {
781 	struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
782 	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
783 	struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
784 	struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
785 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
786 	struct buffer_head *m_bh, *l_bh;
787 	u64 fs_total, new_free;
788 
789 	/* Total up the file system space, according to the latest rindex. */
790 	fs_total = gfs2_ri_total(sdp);
791 	if (gfs2_meta_inode_buffer(m_ip, &m_bh) != 0)
792 		return;
793 
794 	spin_lock(&sdp->sd_statfs_spin);
795 	gfs2_statfs_change_in(m_sc, m_bh->b_data +
796 			      sizeof(struct gfs2_dinode));
797 	if (fs_total > (m_sc->sc_total + l_sc->sc_total))
798 		new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
799 	else
800 		new_free = 0;
801 	spin_unlock(&sdp->sd_statfs_spin);
802 	fs_warn(sdp, "File system extended by %llu blocks.\n",
803 		(unsigned long long)new_free);
804 	gfs2_statfs_change(sdp, new_free, new_free, 0);
805 
806 	if (gfs2_meta_inode_buffer(l_ip, &l_bh) != 0)
807 		goto out;
808 	update_statfs(sdp, m_bh, l_bh);
809 	brelse(l_bh);
810 out:
811 	brelse(m_bh);
812 }
813 
814 /**
815  * gfs2_stuffed_write_end - Write end for stuffed files
816  * @inode: The inode
817  * @dibh: The buffer_head containing the on-disk inode
818  * @pos: The file position
819  * @copied: How much was actually copied by the VFS
820  * @page: The page
821  *
822  * This copies the data from the page into the inode block after
823  * the inode data structure itself.
824  *
825  * Returns: errno
826  */
827 static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
828 				  loff_t pos, unsigned copied,
829 				  struct page *page)
830 {
831 	struct gfs2_inode *ip = GFS2_I(inode);
832 	u64 to = pos + copied;
833 	void *kaddr;
834 	unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
835 
836 	BUG_ON(pos + copied > gfs2_max_stuffed_size(ip));
837 
838 	kaddr = kmap_atomic(page);
839 	memcpy(buf + pos, kaddr + pos, copied);
840 	flush_dcache_page(page);
841 	kunmap_atomic(kaddr);
842 
843 	WARN_ON(!PageUptodate(page));
844 	unlock_page(page);
845 	put_page(page);
846 
847 	if (copied) {
848 		if (inode->i_size < to)
849 			i_size_write(inode, to);
850 		mark_inode_dirty(inode);
851 	}
852 	return copied;
853 }
854 
855 /**
856  * gfs2_write_end
857  * @file: The file to write to
858  * @mapping: The address space to write to
859  * @pos: The file position
860  * @len: The length of the data
861  * @copied: How much was actually copied by the VFS
862  * @page: The page that has been written
863  * @fsdata: The fsdata (unused in GFS2)
864  *
865  * The main write_end function for GFS2. We just put our locking around the VFS
866  * provided functions.
867  *
868  * Returns: errno
869  */
870 
871 static int gfs2_write_end(struct file *file, struct address_space *mapping,
872 			  loff_t pos, unsigned len, unsigned copied,
873 			  struct page *page, void *fsdata)
874 {
875 	struct inode *inode = page->mapping->host;
876 	struct gfs2_inode *ip = GFS2_I(inode);
877 	struct gfs2_sbd *sdp = GFS2_SB(inode);
878 	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
879 	struct buffer_head *dibh;
880 	int ret;
881 	struct gfs2_trans *tr = current->journal_info;
882 	BUG_ON(!tr);
883 
884 	BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == NULL);
885 
886 	ret = gfs2_meta_inode_buffer(ip, &dibh);
887 	if (unlikely(ret))
888 		goto out;
889 
890 	if (gfs2_is_stuffed(ip)) {
891 		ret = gfs2_stuffed_write_end(inode, dibh, pos, copied, page);
892 		page = NULL;
893 		goto out2;
894 	}
895 
896 	if (gfs2_is_jdata(ip))
897 		gfs2_page_add_databufs(ip, page, pos & ~PAGE_MASK, len);
898 	else
899 		gfs2_ordered_add_inode(ip);
900 
901 	ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
902 	page = NULL;
903 	if (tr->tr_num_buf_new)
904 		__mark_inode_dirty(inode, I_DIRTY_DATASYNC);
905 	else
906 		gfs2_trans_add_meta(ip->i_gl, dibh);
907 
908 out2:
909 	if (inode == sdp->sd_rindex) {
910 		adjust_fs_space(inode);
911 		sdp->sd_rindex_uptodate = 0;
912 	}
913 
914 	brelse(dibh);
915 out:
916 	if (page) {
917 		unlock_page(page);
918 		put_page(page);
919 	}
920 	gfs2_trans_end(sdp);
921 	gfs2_inplace_release(ip);
922 	if (ip->i_qadata && ip->i_qadata->qa_qd_num)
923 		gfs2_quota_unlock(ip);
924 	if (inode == sdp->sd_rindex) {
925 		gfs2_glock_dq(&m_ip->i_gh);
926 		gfs2_holder_uninit(&m_ip->i_gh);
927 	}
928 	gfs2_glock_dq(&ip->i_gh);
929 	gfs2_holder_uninit(&ip->i_gh);
930 	return ret;
931 }
932 
933 /**
934  * jdata_set_page_dirty - Page dirtying function
935  * @page: The page to dirty
936  *
937  * Returns: 1 if it dirtyed the page, or 0 otherwise
938  */
939 
940 static int jdata_set_page_dirty(struct page *page)
941 {
942 	SetPageChecked(page);
943 	return __set_page_dirty_buffers(page);
944 }
945 
946 /**
947  * gfs2_bmap - Block map function
948  * @mapping: Address space info
949  * @lblock: The block to map
950  *
951  * Returns: The disk address for the block or 0 on hole or error
952  */
953 
954 static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
955 {
956 	struct gfs2_inode *ip = GFS2_I(mapping->host);
957 	struct gfs2_holder i_gh;
958 	sector_t dblock = 0;
959 	int error;
960 
961 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
962 	if (error)
963 		return 0;
964 
965 	if (!gfs2_is_stuffed(ip))
966 		dblock = generic_block_bmap(mapping, lblock, gfs2_block_map);
967 
968 	gfs2_glock_dq_uninit(&i_gh);
969 
970 	return dblock;
971 }
972 
973 static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
974 {
975 	struct gfs2_bufdata *bd;
976 
977 	lock_buffer(bh);
978 	gfs2_log_lock(sdp);
979 	clear_buffer_dirty(bh);
980 	bd = bh->b_private;
981 	if (bd) {
982 		if (!list_empty(&bd->bd_list) && !buffer_pinned(bh))
983 			list_del_init(&bd->bd_list);
984 		else
985 			gfs2_remove_from_journal(bh, REMOVE_JDATA);
986 	}
987 	bh->b_bdev = NULL;
988 	clear_buffer_mapped(bh);
989 	clear_buffer_req(bh);
990 	clear_buffer_new(bh);
991 	gfs2_log_unlock(sdp);
992 	unlock_buffer(bh);
993 }
994 
995 static void gfs2_invalidatepage(struct page *page, unsigned int offset,
996 				unsigned int length)
997 {
998 	struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
999 	unsigned int stop = offset + length;
1000 	int partial_page = (offset || length < PAGE_SIZE);
1001 	struct buffer_head *bh, *head;
1002 	unsigned long pos = 0;
1003 
1004 	BUG_ON(!PageLocked(page));
1005 	if (!partial_page)
1006 		ClearPageChecked(page);
1007 	if (!page_has_buffers(page))
1008 		goto out;
1009 
1010 	bh = head = page_buffers(page);
1011 	do {
1012 		if (pos + bh->b_size > stop)
1013 			return;
1014 
1015 		if (offset <= pos)
1016 			gfs2_discard(sdp, bh);
1017 		pos += bh->b_size;
1018 		bh = bh->b_this_page;
1019 	} while (bh != head);
1020 out:
1021 	if (!partial_page)
1022 		try_to_release_page(page, 0);
1023 }
1024 
1025 /**
1026  * gfs2_ok_for_dio - check that dio is valid on this file
1027  * @ip: The inode
1028  * @offset: The offset at which we are reading or writing
1029  *
1030  * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
1031  *          1 (to accept the i/o request)
1032  */
1033 static int gfs2_ok_for_dio(struct gfs2_inode *ip, loff_t offset)
1034 {
1035 	/*
1036 	 * Should we return an error here? I can't see that O_DIRECT for
1037 	 * a stuffed file makes any sense. For now we'll silently fall
1038 	 * back to buffered I/O
1039 	 */
1040 	if (gfs2_is_stuffed(ip))
1041 		return 0;
1042 
1043 	if (offset >= i_size_read(&ip->i_inode))
1044 		return 0;
1045 	return 1;
1046 }
1047 
1048 
1049 
1050 static ssize_t gfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1051 {
1052 	struct file *file = iocb->ki_filp;
1053 	struct inode *inode = file->f_mapping->host;
1054 	struct address_space *mapping = inode->i_mapping;
1055 	struct gfs2_inode *ip = GFS2_I(inode);
1056 	loff_t offset = iocb->ki_pos;
1057 	struct gfs2_holder gh;
1058 	int rv;
1059 
1060 	/*
1061 	 * Deferred lock, even if its a write, since we do no allocation
1062 	 * on this path. All we need change is atime, and this lock mode
1063 	 * ensures that other nodes have flushed their buffered read caches
1064 	 * (i.e. their page cache entries for this inode). We do not,
1065 	 * unfortunately have the option of only flushing a range like
1066 	 * the VFS does.
1067 	 */
1068 	gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, 0, &gh);
1069 	rv = gfs2_glock_nq(&gh);
1070 	if (rv)
1071 		goto out_uninit;
1072 	rv = gfs2_ok_for_dio(ip, offset);
1073 	if (rv != 1)
1074 		goto out; /* dio not valid, fall back to buffered i/o */
1075 
1076 	/*
1077 	 * Now since we are holding a deferred (CW) lock at this point, you
1078 	 * might be wondering why this is ever needed. There is a case however
1079 	 * where we've granted a deferred local lock against a cached exclusive
1080 	 * glock. That is ok provided all granted local locks are deferred, but
1081 	 * it also means that it is possible to encounter pages which are
1082 	 * cached and possibly also mapped. So here we check for that and sort
1083 	 * them out ahead of the dio. The glock state machine will take care of
1084 	 * everything else.
1085 	 *
1086 	 * If in fact the cached glock state (gl->gl_state) is deferred (CW) in
1087 	 * the first place, mapping->nr_pages will always be zero.
1088 	 */
1089 	if (mapping->nrpages) {
1090 		loff_t lstart = offset & ~(PAGE_SIZE - 1);
1091 		loff_t len = iov_iter_count(iter);
1092 		loff_t end = PAGE_ALIGN(offset + len) - 1;
1093 
1094 		rv = 0;
1095 		if (len == 0)
1096 			goto out;
1097 		if (test_and_clear_bit(GIF_SW_PAGED, &ip->i_flags))
1098 			unmap_shared_mapping_range(ip->i_inode.i_mapping, offset, len);
1099 		rv = filemap_write_and_wait_range(mapping, lstart, end);
1100 		if (rv)
1101 			goto out;
1102 		if (iov_iter_rw(iter) == WRITE)
1103 			truncate_inode_pages_range(mapping, lstart, end);
1104 	}
1105 
1106 	rv = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
1107 				  gfs2_get_block_direct, NULL, NULL, 0);
1108 out:
1109 	gfs2_glock_dq(&gh);
1110 out_uninit:
1111 	gfs2_holder_uninit(&gh);
1112 	return rv;
1113 }
1114 
1115 /**
1116  * gfs2_releasepage - free the metadata associated with a page
1117  * @page: the page that's being released
1118  * @gfp_mask: passed from Linux VFS, ignored by us
1119  *
1120  * Call try_to_free_buffers() if the buffers in this page can be
1121  * released.
1122  *
1123  * Returns: 0
1124  */
1125 
1126 int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
1127 {
1128 	struct address_space *mapping = page->mapping;
1129 	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
1130 	struct buffer_head *bh, *head;
1131 	struct gfs2_bufdata *bd;
1132 
1133 	if (!page_has_buffers(page))
1134 		return 0;
1135 
1136 	/*
1137 	 * From xfs_vm_releasepage: mm accommodates an old ext3 case where
1138 	 * clean pages might not have had the dirty bit cleared.  Thus, it can
1139 	 * send actual dirty pages to ->releasepage() via shrink_active_list().
1140 	 *
1141 	 * As a workaround, we skip pages that contain dirty buffers below.
1142 	 * Once ->releasepage isn't called on dirty pages anymore, we can warn
1143 	 * on dirty buffers like we used to here again.
1144 	 */
1145 
1146 	gfs2_log_lock(sdp);
1147 	spin_lock(&sdp->sd_ail_lock);
1148 	head = bh = page_buffers(page);
1149 	do {
1150 		if (atomic_read(&bh->b_count))
1151 			goto cannot_release;
1152 		bd = bh->b_private;
1153 		if (bd && bd->bd_tr)
1154 			goto cannot_release;
1155 		if (buffer_dirty(bh) || WARN_ON(buffer_pinned(bh)))
1156 			goto cannot_release;
1157 		bh = bh->b_this_page;
1158 	} while(bh != head);
1159 	spin_unlock(&sdp->sd_ail_lock);
1160 
1161 	head = bh = page_buffers(page);
1162 	do {
1163 		bd = bh->b_private;
1164 		if (bd) {
1165 			gfs2_assert_warn(sdp, bd->bd_bh == bh);
1166 			if (!list_empty(&bd->bd_list))
1167 				list_del_init(&bd->bd_list);
1168 			bd->bd_bh = NULL;
1169 			bh->b_private = NULL;
1170 			kmem_cache_free(gfs2_bufdata_cachep, bd);
1171 		}
1172 
1173 		bh = bh->b_this_page;
1174 	} while (bh != head);
1175 	gfs2_log_unlock(sdp);
1176 
1177 	return try_to_free_buffers(page);
1178 
1179 cannot_release:
1180 	spin_unlock(&sdp->sd_ail_lock);
1181 	gfs2_log_unlock(sdp);
1182 	return 0;
1183 }
1184 
1185 static const struct address_space_operations gfs2_writeback_aops = {
1186 	.writepage = gfs2_writepage,
1187 	.writepages = gfs2_writepages,
1188 	.readpage = gfs2_readpage,
1189 	.readpages = gfs2_readpages,
1190 	.write_begin = gfs2_write_begin,
1191 	.write_end = gfs2_write_end,
1192 	.bmap = gfs2_bmap,
1193 	.invalidatepage = gfs2_invalidatepage,
1194 	.releasepage = gfs2_releasepage,
1195 	.direct_IO = gfs2_direct_IO,
1196 	.migratepage = buffer_migrate_page,
1197 	.is_partially_uptodate = block_is_partially_uptodate,
1198 	.error_remove_page = generic_error_remove_page,
1199 };
1200 
1201 static const struct address_space_operations gfs2_ordered_aops = {
1202 	.writepage = gfs2_writepage,
1203 	.writepages = gfs2_writepages,
1204 	.readpage = gfs2_readpage,
1205 	.readpages = gfs2_readpages,
1206 	.write_begin = gfs2_write_begin,
1207 	.write_end = gfs2_write_end,
1208 	.set_page_dirty = __set_page_dirty_buffers,
1209 	.bmap = gfs2_bmap,
1210 	.invalidatepage = gfs2_invalidatepage,
1211 	.releasepage = gfs2_releasepage,
1212 	.direct_IO = gfs2_direct_IO,
1213 	.migratepage = buffer_migrate_page,
1214 	.is_partially_uptodate = block_is_partially_uptodate,
1215 	.error_remove_page = generic_error_remove_page,
1216 };
1217 
1218 static const struct address_space_operations gfs2_jdata_aops = {
1219 	.writepage = gfs2_jdata_writepage,
1220 	.writepages = gfs2_jdata_writepages,
1221 	.readpage = gfs2_readpage,
1222 	.readpages = gfs2_readpages,
1223 	.write_begin = gfs2_write_begin,
1224 	.write_end = gfs2_write_end,
1225 	.set_page_dirty = jdata_set_page_dirty,
1226 	.bmap = gfs2_bmap,
1227 	.invalidatepage = gfs2_invalidatepage,
1228 	.releasepage = gfs2_releasepage,
1229 	.is_partially_uptodate = block_is_partially_uptodate,
1230 	.error_remove_page = generic_error_remove_page,
1231 };
1232 
1233 void gfs2_set_aops(struct inode *inode)
1234 {
1235 	struct gfs2_inode *ip = GFS2_I(inode);
1236 
1237 	if (gfs2_is_writeback(ip))
1238 		inode->i_mapping->a_ops = &gfs2_writeback_aops;
1239 	else if (gfs2_is_ordered(ip))
1240 		inode->i_mapping->a_ops = &gfs2_ordered_aops;
1241 	else if (gfs2_is_jdata(ip))
1242 		inode->i_mapping->a_ops = &gfs2_jdata_aops;
1243 	else
1244 		BUG();
1245 }
1246 
1247