1
2
3
4
5
6
7
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/lm_interface.h>
23
24#include "gfs2.h"
25#include "incore.h"
26#include "bmap.h"
27#include "glock.h"
28#include "inode.h"
29#include "log.h"
30#include "meta_io.h"
31#include "ops_address.h"
32#include "quota.h"
33#include "trans.h"
34#include "rgrp.h"
35#include "ops_file.h"
36#include "super.h"
37#include "util.h"
38#include "glops.h"
39
40
41static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
42 unsigned int from, unsigned int to)
43{
44 struct buffer_head *head = page_buffers(page);
45 unsigned int bsize = head->b_size;
46 struct buffer_head *bh;
47 unsigned int start, end;
48
49 for (bh = head, start = 0; bh != head || !start;
50 bh = bh->b_this_page, start = end) {
51 end = start + bsize;
52 if (end <= from || start >= to)
53 continue;
54 if (gfs2_is_jdata(ip))
55 set_buffer_uptodate(bh);
56 gfs2_trans_add_bh(ip->i_gl, bh, 0);
57 }
58}
59
60
61
62
63
64
65
66
67
68
69
70int gfs2_get_block(struct inode *inode, sector_t lblock,
71 struct buffer_head *bh_result, int create)
72{
73 return gfs2_block_map(inode, lblock, create, bh_result);
74}
75
76
77
78
79
80
81
82
83
84
85
86static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
87 struct buffer_head *bh_result, int create)
88{
89 int error;
90
91 error = gfs2_block_map(inode, lblock, 0, bh_result);
92 if (error)
93 return error;
94 if (!buffer_mapped(bh_result))
95 return -EIO;
96 return 0;
97}
98
99static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
100 struct buffer_head *bh_result, int create)
101{
102 return gfs2_block_map(inode, lblock, 0, bh_result);
103}
104
105
106
107
108
109
110
111
112
113
114
115static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
116{
117 struct inode *inode = page->mapping->host;
118 struct gfs2_inode *ip = GFS2_I(inode);
119 struct gfs2_sbd *sdp = GFS2_SB(inode);
120 loff_t i_size = i_size_read(inode);
121 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
122 unsigned offset;
123 int error;
124 int done_trans = 0;
125
126 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
127 unlock_page(page);
128 return -EIO;
129 }
130 if (current->journal_info)
131 goto out_ignore;
132
133
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);
137 unlock_page(page);
138 return 0;
139 }
140
141 if ((sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) &&
142 PageChecked(page)) {
143 ClearPageChecked(page);
144 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
145 if (error)
146 goto out_ignore;
147 if (!page_has_buffers(page)) {
148 create_empty_buffers(page, inode->i_sb->s_blocksize,
149 (1 << BH_Dirty)|(1 << BH_Uptodate));
150 }
151 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
152 done_trans = 1;
153 }
154 error = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
155 if (done_trans)
156 gfs2_trans_end(sdp);
157 gfs2_meta_cache_flush(ip);
158 return error;
159
160out_ignore:
161 redirty_page_for_writepage(wbc, page);
162 unlock_page(page);
163 return 0;
164}
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179static int gfs2_writepages(struct address_space *mapping,
180 struct writeback_control *wbc)
181{
182 struct inode *inode = mapping->host;
183 struct gfs2_inode *ip = GFS2_I(inode);
184 struct gfs2_sbd *sdp = GFS2_SB(inode);
185
186 if (sdp->sd_args.ar_data == GFS2_DATA_WRITEBACK && !gfs2_is_jdata(ip))
187 return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
188
189 return generic_writepages(mapping, wbc);
190}
191
192
193
194
195
196
197
198
199
200static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
201{
202 struct buffer_head *dibh;
203 void *kaddr;
204 int error;
205
206
207
208
209
210
211 if (unlikely(page->index)) {
212 zero_user_page(page, 0, PAGE_CACHE_SIZE, KM_USER0);
213 return 0;
214 }
215
216 error = gfs2_meta_inode_buffer(ip, &dibh);
217 if (error)
218 return error;
219
220 kaddr = kmap_atomic(page, KM_USER0);
221 memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
222 ip->i_di.di_size);
223 memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
224 kunmap_atomic(kaddr, KM_USER0);
225 flush_dcache_page(page);
226 brelse(dibh);
227 SetPageUptodate(page);
228
229 return 0;
230}
231
232
233
234
235
236
237
238
239
240
241
242static int gfs2_readpage(struct file *file, struct page *page)
243{
244 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
245 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
246 struct gfs2_file *gf = NULL;
247 struct gfs2_holder gh;
248 int error;
249 int do_unlock = 0;
250
251 if (likely(file != &gfs2_internal_file_sentinel)) {
252 if (file) {
253 gf = file->private_data;
254 if (test_bit(GFF_EXLOCK, &gf->f_flags))
255
256 goto skip_lock;
257 }
258 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|LM_FLAG_TRY_1CB, &gh);
259 do_unlock = 1;
260 error = gfs2_glock_nq_atime(&gh);
261 if (unlikely(error))
262 goto out_unlock;
263 }
264
265skip_lock:
266 if (gfs2_is_stuffed(ip)) {
267 error = stuffed_readpage(ip, page);
268 unlock_page(page);
269 } else
270 error = mpage_readpage(page, gfs2_get_block);
271
272 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
273 error = -EIO;
274
275 if (do_unlock) {
276 gfs2_glock_dq_m(1, &gh);
277 gfs2_holder_uninit(&gh);
278 }
279out:
280 return error;
281out_unlock:
282 unlock_page(page);
283 if (error == GLR_TRYFAILED) {
284 error = AOP_TRUNCATED_PAGE;
285 yield();
286 }
287 if (do_unlock)
288 gfs2_holder_uninit(&gh);
289 goto out;
290}
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307static int gfs2_readpages(struct file *file, struct address_space *mapping,
308 struct list_head *pages, unsigned nr_pages)
309{
310 struct inode *inode = mapping->host;
311 struct gfs2_inode *ip = GFS2_I(inode);
312 struct gfs2_sbd *sdp = GFS2_SB(inode);
313 struct gfs2_holder gh;
314 int ret = 0;
315 int do_unlock = 0;
316
317 if (likely(file != &gfs2_internal_file_sentinel)) {
318 if (file) {
319 struct gfs2_file *gf = file->private_data;
320 if (test_bit(GFF_EXLOCK, &gf->f_flags))
321 goto skip_lock;
322 }
323 gfs2_holder_init(ip->i_gl, LM_ST_SHARED,
324 LM_FLAG_TRY_1CB|GL_ATIME, &gh);
325 do_unlock = 1;
326 ret = gfs2_glock_nq_atime(&gh);
327 if (ret == GLR_TRYFAILED)
328 goto out_noerror;
329 if (unlikely(ret))
330 goto out_unlock;
331 }
332skip_lock:
333 if (!gfs2_is_stuffed(ip))
334 ret = mpage_readpages(mapping, pages, nr_pages, gfs2_get_block);
335
336 if (do_unlock) {
337 gfs2_glock_dq_m(1, &gh);
338 gfs2_holder_uninit(&gh);
339 }
340out:
341 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
342 ret = -EIO;
343 return ret;
344out_noerror:
345 ret = 0;
346out_unlock:
347 if (do_unlock)
348 gfs2_holder_uninit(&gh);
349 goto out;
350}
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365static int gfs2_write_begin(struct file *file, struct address_space *mapping,
366 loff_t pos, unsigned len, unsigned flags,
367 struct page **pagep, void **fsdata)
368{
369 struct gfs2_inode *ip = GFS2_I(mapping->host);
370 struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
371 unsigned int data_blocks, ind_blocks, rblocks;
372 int alloc_required;
373 int error = 0;
374 struct gfs2_alloc *al;
375 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
376 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
377 unsigned to = from + len;
378 struct page *page;
379
380 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
381 error = gfs2_glock_nq_atime(&ip->i_gh);
382 if (unlikely(error))
383 goto out_uninit;
384
385 error = -ENOMEM;
386 page = __grab_cache_page(mapping, index);
387 *pagep = page;
388 if (!page)
389 goto out_unlock;
390
391 gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
392
393 error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
394 if (error)
395 goto out_putpage;
396
397
398 ip->i_alloc.al_requested = 0;
399 if (alloc_required) {
400 al = gfs2_alloc_get(ip);
401
402 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
403 if (error)
404 goto out_alloc_put;
405
406 error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
407 if (error)
408 goto out_qunlock;
409
410 al->al_requested = data_blocks + ind_blocks;
411 error = gfs2_inplace_reserve(ip);
412 if (error)
413 goto out_qunlock;
414 }
415
416 rblocks = RES_DINODE + ind_blocks;
417 if (gfs2_is_jdata(ip))
418 rblocks += data_blocks ? data_blocks : 1;
419 if (ind_blocks || data_blocks)
420 rblocks += RES_STATFS + RES_QUOTA;
421
422 error = gfs2_trans_begin(sdp, rblocks,
423 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
424 if (error)
425 goto out_trans_fail;
426
427 if (gfs2_is_stuffed(ip)) {
428 if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
429 error = gfs2_unstuff_dinode(ip, page);
430 if (error == 0)
431 goto prepare_write;
432 } else if (!PageUptodate(page))
433 error = stuffed_readpage(ip, page);
434 goto out;
435 }
436
437prepare_write:
438 error = block_prepare_write(page, from, to, gfs2_get_block);
439
440out:
441 if (error) {
442 gfs2_trans_end(sdp);
443out_trans_fail:
444 if (alloc_required) {
445 gfs2_inplace_release(ip);
446out_qunlock:
447 gfs2_quota_unlock(ip);
448out_alloc_put:
449 gfs2_alloc_put(ip);
450 }
451out_putpage:
452 page_cache_release(page);
453 if (pos + len > ip->i_inode.i_size)
454 vmtruncate(&ip->i_inode, ip->i_inode.i_size);
455out_unlock:
456 gfs2_glock_dq_m(1, &ip->i_gh);
457out_uninit:
458 gfs2_holder_uninit(&ip->i_gh);
459 }
460
461 return error;
462}
463
464
465
466
467
468static void adjust_fs_space(struct inode *inode)
469{
470 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
471 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
472 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
473 u64 fs_total, new_free;
474
475
476 fs_total = gfs2_ri_total(sdp);
477
478 spin_lock(&sdp->sd_statfs_spin);
479 if (fs_total > (m_sc->sc_total + l_sc->sc_total))
480 new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
481 else
482 new_free = 0;
483 spin_unlock(&sdp->sd_statfs_spin);
484 fs_warn(sdp, "File system extended by %llu blocks.\n",
485 (unsigned long long)new_free);
486 gfs2_statfs_change(sdp, new_free, new_free, 0);
487}
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
504 loff_t pos, unsigned len, unsigned copied,
505 struct page *page)
506{
507 struct gfs2_inode *ip = GFS2_I(inode);
508 struct gfs2_sbd *sdp = GFS2_SB(inode);
509 u64 to = pos + copied;
510 void *kaddr;
511 unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
512 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
513
514 BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
515 kaddr = kmap_atomic(page, KM_USER0);
516 memcpy(buf + pos, kaddr + pos, copied);
517 memset(kaddr + pos + copied, 0, len - copied);
518 flush_dcache_page(page);
519 kunmap_atomic(kaddr, KM_USER0);
520
521 if (!PageUptodate(page))
522 SetPageUptodate(page);
523 unlock_page(page);
524 page_cache_release(page);
525
526 if (inode->i_size < to) {
527 i_size_write(inode, to);
528 ip->i_di.di_size = inode->i_size;
529 di->di_size = cpu_to_be64(inode->i_size);
530 mark_inode_dirty(inode);
531 }
532
533 if (inode == sdp->sd_rindex)
534 adjust_fs_space(inode);
535
536 brelse(dibh);
537 gfs2_trans_end(sdp);
538 gfs2_glock_dq(&ip->i_gh);
539 gfs2_holder_uninit(&ip->i_gh);
540 return copied;
541}
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560static int gfs2_write_end(struct file *file, struct address_space *mapping,
561 loff_t pos, unsigned len, unsigned copied,
562 struct page *page, void *fsdata)
563{
564 struct inode *inode = page->mapping->host;
565 struct gfs2_inode *ip = GFS2_I(inode);
566 struct gfs2_sbd *sdp = GFS2_SB(inode);
567 struct buffer_head *dibh;
568 struct gfs2_alloc *al = &ip->i_alloc;
569 struct gfs2_dinode *di;
570 unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
571 unsigned int to = from + len;
572 int ret;
573
574 BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == 0);
575
576 ret = gfs2_meta_inode_buffer(ip, &dibh);
577 if (unlikely(ret)) {
578 unlock_page(page);
579 page_cache_release(page);
580 goto failed;
581 }
582
583 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
584
585 if (gfs2_is_stuffed(ip))
586 return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
587
588 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
589 gfs2_page_add_databufs(ip, page, from, to);
590
591 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
592
593 if (likely(ret >= 0)) {
594 copied = ret;
595 if ((pos + copied) > inode->i_size) {
596 di = (struct gfs2_dinode *)dibh->b_data;
597 ip->i_di.di_size = inode->i_size;
598 di->di_size = cpu_to_be64(inode->i_size);
599 mark_inode_dirty(inode);
600 }
601 }
602
603 if (inode == sdp->sd_rindex)
604 adjust_fs_space(inode);
605
606 brelse(dibh);
607 gfs2_trans_end(sdp);
608failed:
609 if (al->al_requested) {
610 gfs2_inplace_release(ip);
611 gfs2_quota_unlock(ip);
612 gfs2_alloc_put(ip);
613 }
614 gfs2_glock_dq(&ip->i_gh);
615 gfs2_holder_uninit(&ip->i_gh);
616 return ret;
617}
618
619
620
621
622
623
624
625
626static int gfs2_set_page_dirty(struct page *page)
627{
628 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
629 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
630
631 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
632 SetPageChecked(page);
633 return __set_page_dirty_buffers(page);
634}
635
636
637
638
639
640
641
642
643
644static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
645{
646 struct gfs2_inode *ip = GFS2_I(mapping->host);
647 struct gfs2_holder i_gh;
648 sector_t dblock = 0;
649 int error;
650
651 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
652 if (error)
653 return 0;
654
655 if (!gfs2_is_stuffed(ip))
656 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
657
658 gfs2_glock_dq_uninit(&i_gh);
659
660 return dblock;
661}
662
663static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
664{
665 struct gfs2_bufdata *bd;
666
667 lock_buffer(bh);
668 gfs2_log_lock(sdp);
669 clear_buffer_dirty(bh);
670 bd = bh->b_private;
671 if (bd) {
672 if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
673 list_del_init(&bd->bd_le.le_list);
674 else
675 gfs2_remove_from_journal(bh, current->journal_info, 0);
676 }
677 bh->b_bdev = NULL;
678 clear_buffer_mapped(bh);
679 clear_buffer_req(bh);
680 clear_buffer_new(bh);
681 gfs2_log_unlock(sdp);
682 unlock_buffer(bh);
683}
684
685static void gfs2_invalidatepage(struct page *page, unsigned long offset)
686{
687 struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
688 struct buffer_head *bh, *head;
689 unsigned long pos = 0;
690
691 BUG_ON(!PageLocked(page));
692 if (offset == 0)
693 ClearPageChecked(page);
694 if (!page_has_buffers(page))
695 goto out;
696
697 bh = head = page_buffers(page);
698 do {
699 if (offset <= pos)
700 gfs2_discard(sdp, bh);
701 pos += bh->b_size;
702 bh = bh->b_this_page;
703 } while (bh != head);
704out:
705 if (offset == 0)
706 try_to_release_page(page, 0);
707}
708
709
710
711
712
713
714
715
716
717
718static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
719{
720
721
722
723
724
725
726 if (gfs2_is_jdata(ip))
727 return 0;
728
729 if (gfs2_is_stuffed(ip))
730 return 0;
731
732 if (offset > i_size_read(&ip->i_inode))
733 return 0;
734 return 1;
735}
736
737
738
739static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
740 const struct iovec *iov, loff_t offset,
741 unsigned long nr_segs)
742{
743 struct file *file = iocb->ki_filp;
744 struct inode *inode = file->f_mapping->host;
745 struct gfs2_inode *ip = GFS2_I(inode);
746 struct gfs2_holder gh;
747 int rv;
748
749
750
751
752
753
754
755
756
757 gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh);
758 rv = gfs2_glock_nq_atime(&gh);
759 if (rv)
760 return rv;
761 rv = gfs2_ok_for_dio(ip, rw, offset);
762 if (rv != 1)
763 goto out;
764
765 rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
766 iov, offset, nr_segs,
767 gfs2_get_block_direct, NULL);
768out:
769 gfs2_glock_dq_m(1, &gh);
770 gfs2_holder_uninit(&gh);
771 return rv;
772}
773
774
775
776
777
778
779
780
781
782
783
784
785int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
786{
787 struct inode *aspace = page->mapping->host;
788 struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
789 struct buffer_head *bh, *head;
790 struct gfs2_bufdata *bd;
791
792 if (!page_has_buffers(page))
793 return 0;
794
795 gfs2_log_lock(sdp);
796 head = bh = page_buffers(page);
797 do {
798 if (atomic_read(&bh->b_count))
799 goto cannot_release;
800 bd = bh->b_private;
801 if (bd && bd->bd_ail)
802 goto cannot_release;
803 gfs2_assert_warn(sdp, !buffer_pinned(bh));
804 gfs2_assert_warn(sdp, !buffer_dirty(bh));
805 bh = bh->b_this_page;
806 } while(bh != head);
807 gfs2_log_unlock(sdp);
808
809 head = bh = page_buffers(page);
810 do {
811 gfs2_log_lock(sdp);
812 bd = bh->b_private;
813 if (bd) {
814 gfs2_assert_warn(sdp, bd->bd_bh == bh);
815 gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
816 if (!list_empty(&bd->bd_le.le_list)) {
817 if (!buffer_pinned(bh))
818 list_del_init(&bd->bd_le.le_list);
819 else
820 bd = NULL;
821 }
822 if (bd)
823 bd->bd_bh = NULL;
824 bh->b_private = NULL;
825 }
826 gfs2_log_unlock(sdp);
827 if (bd)
828 kmem_cache_free(gfs2_bufdata_cachep, bd);
829
830 bh = bh->b_this_page;
831 } while (bh != head);
832
833 return try_to_free_buffers(page);
834cannot_release:
835 gfs2_log_unlock(sdp);
836 return 0;
837}
838
839const struct address_space_operations gfs2_file_aops = {
840 .writepage = gfs2_writepage,
841 .writepages = gfs2_writepages,
842 .readpage = gfs2_readpage,
843 .readpages = gfs2_readpages,
844 .sync_page = block_sync_page,
845 .write_begin = gfs2_write_begin,
846 .write_end = gfs2_write_end,
847 .set_page_dirty = gfs2_set_page_dirty,
848 .bmap = gfs2_bmap,
849 .invalidatepage = gfs2_invalidatepage,
850 .releasepage = gfs2_releasepage,
851 .direct_IO = gfs2_direct_IO,
852};
853
854