1
2
3
4
5
6
7
8
9
10#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
14#include <linux/pagemap.h>
15#include <linux/uio.h>
16#include <linux/blkdev.h>
17#include <linux/mm.h>
18#include <linux/mount.h>
19#include <linux/fs.h>
20#include <linux/gfs2_ondisk.h>
21#include <linux/falloc.h>
22#include <linux/swap.h>
23#include <linux/crc32.h>
24#include <linux/writeback.h>
25#include <asm/uaccess.h>
26#include <linux/dlm.h>
27#include <linux/dlm_plock.h>
28#include <linux/aio.h>
29#include <linux/delay.h>
30#include <linux/backing-dev.h>
31
32#include "gfs2.h"
33#include "incore.h"
34#include "bmap.h"
35#include "aops.h"
36#include "dir.h"
37#include "glock.h"
38#include "glops.h"
39#include "inode.h"
40#include "log.h"
41#include "meta_io.h"
42#include "quota.h"
43#include "rgrp.h"
44#include "trans.h"
45#include "util.h"
46
47
48
49
50
51
52
53
54
55
56
57
58
59static loff_t gfs2_llseek(struct file *file, loff_t offset, int whence)
60{
61 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
62 struct gfs2_holder i_gh;
63 loff_t error;
64
65 switch (whence) {
66 case SEEK_END:
67 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
68 &i_gh);
69 if (!error) {
70 error = generic_file_llseek(file, offset, whence);
71 gfs2_glock_dq_uninit(&i_gh);
72 }
73 break;
74
75 case SEEK_DATA:
76 error = gfs2_seek_data(file, offset);
77 break;
78
79 case SEEK_HOLE:
80 error = gfs2_seek_hole(file, offset);
81 break;
82
83 case SEEK_CUR:
84 case SEEK_SET:
85
86
87
88
89 error = generic_file_llseek(file, offset, whence);
90 break;
91 default:
92 error = -EINVAL;
93 }
94
95 return error;
96}
97
98
99
100
101
102
103
104
105
106
107static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
108{
109 struct inode *dir = file->f_mapping->host;
110 struct gfs2_inode *dip = GFS2_I(dir);
111 struct gfs2_holder d_gh;
112 u64 offset = file->f_pos;
113 int error;
114
115 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
116 error = gfs2_glock_nq(&d_gh);
117 if (error) {
118 gfs2_holder_uninit(&d_gh);
119 return error;
120 }
121
122 error = gfs2_dir_read(dir, &offset, dirent, filldir, &file->f_ra);
123
124 gfs2_glock_dq_uninit(&d_gh);
125
126 file->f_pos = offset;
127
128 return error;
129}
130
131
132
133
134
135
136
137static struct {
138 u32 fsflag;
139 u32 gfsflag;
140} fsflag_gfs2flag[] = {
141 {FS_SYNC_FL, GFS2_DIF_SYNC},
142 {FS_IMMUTABLE_FL, GFS2_DIF_IMMUTABLE},
143 {FS_APPEND_FL, GFS2_DIF_APPENDONLY},
144 {FS_NOATIME_FL, GFS2_DIF_NOATIME},
145 {FS_INDEX_FL, GFS2_DIF_EXHASH},
146 {FS_TOPDIR_FL, GFS2_DIF_TOPDIR},
147 {FS_JOURNAL_DATA_FL, GFS2_DIF_JDATA | GFS2_DIF_INHERIT_JDATA},
148};
149
150static int gfs2_get_flags(struct file *filp, u32 __user *ptr)
151{
152 struct inode *inode = file_inode(filp);
153 struct gfs2_inode *ip = GFS2_I(inode);
154 struct gfs2_holder gh;
155 int i, error;
156 u32 gfsflags, fsflags = 0;
157
158 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
159 error = gfs2_glock_nq(&gh);
160 if (error)
161 goto out_uninit;
162
163 gfsflags = ip->i_diskflags;
164 if (S_ISDIR(inode->i_mode))
165 gfsflags &= ~GFS2_DIF_JDATA;
166 else
167 gfsflags &= ~GFS2_DIF_INHERIT_JDATA;
168 for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++)
169 if (gfsflags & fsflag_gfs2flag[i].gfsflag)
170 fsflags |= fsflag_gfs2flag[i].fsflag;
171
172 if (put_user(fsflags, ptr))
173 error = -EFAULT;
174
175 gfs2_glock_dq(&gh);
176out_uninit:
177 gfs2_holder_uninit(&gh);
178 return error;
179}
180
181void gfs2_set_inode_flags(struct inode *inode)
182{
183 struct gfs2_inode *ip = GFS2_I(inode);
184 unsigned int flags = inode->i_flags;
185
186 flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_NOSEC);
187 if ((ip->i_eattr == 0) && !is_sxid(inode->i_mode))
188 flags |= S_NOSEC;
189 if (ip->i_diskflags & GFS2_DIF_IMMUTABLE)
190 flags |= S_IMMUTABLE;
191 if (ip->i_diskflags & GFS2_DIF_APPENDONLY)
192 flags |= S_APPEND;
193 if (ip->i_diskflags & GFS2_DIF_NOATIME)
194 flags |= S_NOATIME;
195 if (ip->i_diskflags & GFS2_DIF_SYNC)
196 flags |= S_SYNC;
197 inode->i_flags = flags;
198}
199
200
201#define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
202 GFS2_DIF_IMMUTABLE| \
203 GFS2_DIF_APPENDONLY| \
204 GFS2_DIF_NOATIME| \
205 GFS2_DIF_SYNC| \
206 GFS2_DIF_TOPDIR| \
207 GFS2_DIF_INHERIT_JDATA)
208
209
210
211
212
213
214
215
216static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask)
217{
218 struct inode *inode = file_inode(filp);
219 struct gfs2_inode *ip = GFS2_I(inode);
220 struct gfs2_sbd *sdp = GFS2_SB(inode);
221 struct buffer_head *bh;
222 struct gfs2_holder gh;
223 int error;
224 u32 new_flags, flags;
225
226 error = mnt_want_write_file(filp);
227 if (error)
228 return error;
229
230 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
231 if (error)
232 goto out_drop_write;
233
234 error = -EACCES;
235 if (!inode_owner_or_capable(inode))
236 goto out;
237
238 error = 0;
239 flags = ip->i_diskflags;
240 new_flags = (flags & ~mask) | (reqflags & mask);
241 if ((new_flags ^ flags) == 0)
242 goto out;
243
244 error = -EPERM;
245 if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE))
246 goto out;
247 if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY))
248 goto out;
249 if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) &&
250 !capable(CAP_LINUX_IMMUTABLE))
251 goto out;
252 if (!IS_IMMUTABLE(inode)) {
253 error = gfs2_permission(inode, MAY_WRITE);
254 if (error)
255 goto out;
256 }
257 if ((flags ^ new_flags) & GFS2_DIF_JDATA) {
258 if (new_flags & GFS2_DIF_JDATA)
259 gfs2_log_flush(sdp, ip->i_gl);
260 error = filemap_fdatawrite(inode->i_mapping);
261 if (error)
262 goto out;
263 error = filemap_fdatawait(inode->i_mapping);
264 if (error)
265 goto out;
266 if (new_flags & GFS2_DIF_JDATA)
267 gfs2_ordered_del_inode(ip);
268 }
269 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
270 if (error)
271 goto out;
272 error = gfs2_meta_inode_buffer(ip, &bh);
273 if (error)
274 goto out_trans_end;
275 inode->i_ctime = CURRENT_TIME;
276 gfs2_trans_add_meta(ip->i_gl, bh);
277 ip->i_diskflags = new_flags;
278 gfs2_dinode_out(ip, bh->b_data);
279 brelse(bh);
280 gfs2_set_inode_flags(inode);
281 gfs2_set_aops(inode);
282out_trans_end:
283 gfs2_trans_end(sdp);
284out:
285 gfs2_glock_dq_uninit(&gh);
286out_drop_write:
287 mnt_drop_write_file(filp);
288 return error;
289}
290
291static int gfs2_set_flags(struct file *filp, u32 __user *ptr)
292{
293 struct inode *inode = file_inode(filp);
294 u32 fsflags, gfsflags = 0;
295 u32 mask;
296 int i;
297
298 if (get_user(fsflags, ptr))
299 return -EFAULT;
300
301 for (i = 0; i < ARRAY_SIZE(fsflag_gfs2flag); i++) {
302 if (fsflags & fsflag_gfs2flag[i].fsflag) {
303 fsflags &= ~fsflag_gfs2flag[i].fsflag;
304 gfsflags |= fsflag_gfs2flag[i].gfsflag;
305 }
306 }
307 if (fsflags || gfsflags & ~GFS2_FLAGS_USER_SET)
308 return -EINVAL;
309
310 mask = GFS2_FLAGS_USER_SET;
311 if (S_ISDIR(inode->i_mode)) {
312 mask &= ~GFS2_DIF_JDATA;
313 } else {
314
315 if (gfsflags & GFS2_DIF_TOPDIR)
316 return -EINVAL;
317 mask &= ~(GFS2_DIF_TOPDIR | GFS2_DIF_INHERIT_JDATA);
318 }
319
320 return do_gfs2_set_flags(filp, gfsflags, mask);
321}
322
323static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
324{
325 switch(cmd) {
326 case FS_IOC_GETFLAGS:
327 return gfs2_get_flags(filp, (u32 __user *)arg);
328 case FS_IOC_SETFLAGS:
329 return gfs2_set_flags(filp, (u32 __user *)arg);
330 case FITRIM:
331 return gfs2_fitrim(filp, (void __user *)arg);
332 }
333 return -ENOTTY;
334}
335
336
337
338
339
340
341
342
343
344
345
346
347
348static void gfs2_size_hint(struct file *filep, loff_t offset, size_t size)
349{
350 struct inode *inode = file_inode(filep);
351 struct gfs2_sbd *sdp = GFS2_SB(inode);
352 struct gfs2_inode *ip = GFS2_I(inode);
353 size_t blks = (size + sdp->sd_sb.sb_bsize - 1) >> sdp->sd_sb.sb_bsize_shift;
354 int hint = min_t(size_t, INT_MAX, blks);
355
356 if (hint > atomic_read(&ip->i_res.rs_sizehint))
357 atomic_set(&ip->i_res.rs_sizehint, hint);
358}
359
360
361
362
363
364
365
366
367
368
369
370static int gfs2_allocate_page_backing(struct page *page)
371{
372 struct inode *inode = page->mapping->host;
373 struct buffer_head bh;
374 unsigned long size = PAGE_CACHE_SIZE;
375 u64 lblock = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
376
377 do {
378 bh.b_state = 0;
379 bh.b_size = size;
380 gfs2_block_map(inode, lblock, &bh, 1);
381 if (!buffer_mapped(&bh))
382 return -EIO;
383 size -= bh.b_size;
384 lblock += (bh.b_size >> inode->i_blkbits);
385 } while(size > 0);
386 return 0;
387}
388
389
390
391
392
393
394
395
396
397
398static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
399{
400 struct page *page = vmf->page;
401 struct inode *inode = file_inode(vma->vm_file);
402 struct gfs2_inode *ip = GFS2_I(inode);
403 struct gfs2_sbd *sdp = GFS2_SB(inode);
404 struct gfs2_alloc_parms ap = { .aflags = 0, };
405 unsigned long last_index;
406 u64 pos = page->index << PAGE_CACHE_SHIFT;
407 unsigned int data_blocks, ind_blocks, rblocks;
408 struct gfs2_holder gh;
409 loff_t size;
410 int ret;
411
412 sb_start_pagefault(inode->i_sb);
413
414 ret = gfs2_rsqa_alloc(ip);
415 if (ret)
416 goto out;
417
418 gfs2_size_hint(vma->vm_file, pos, PAGE_CACHE_SIZE);
419
420 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
421 ret = gfs2_glock_nq(&gh);
422 if (ret)
423 goto out_uninit;
424
425
426 file_update_time(vma->vm_file);
427
428 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
429 set_bit(GIF_SW_PAGED, &ip->i_flags);
430
431 if (!gfs2_write_alloc_required(ip, pos, PAGE_CACHE_SIZE)) {
432 lock_page(page);
433 if (!PageUptodate(page) || page->mapping != inode->i_mapping) {
434 ret = -EAGAIN;
435 unlock_page(page);
436 }
437 goto out_unlock;
438 }
439
440 ret = gfs2_rindex_update(sdp);
441 if (ret)
442 goto out_unlock;
443
444 gfs2_write_calc_reserv(ip, PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
445 ap.target = data_blocks + ind_blocks;
446 ret = gfs2_quota_lock_check(ip, &ap);
447 if (ret)
448 goto out_unlock;
449 ret = gfs2_inplace_reserve(ip, &ap);
450 if (ret)
451 goto out_quota_unlock;
452
453 rblocks = RES_DINODE + ind_blocks;
454 if (gfs2_is_jdata(ip))
455 rblocks += data_blocks ? data_blocks : 1;
456 if (ind_blocks || data_blocks) {
457 rblocks += RES_STATFS + RES_QUOTA;
458 rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
459 }
460 ret = gfs2_trans_begin(sdp, rblocks, 0);
461 if (ret)
462 goto out_trans_fail;
463
464 lock_page(page);
465 ret = -EINVAL;
466 size = i_size_read(inode);
467 last_index = (size - 1) >> PAGE_CACHE_SHIFT;
468
469 if (size == 0 || (page->index > last_index))
470 goto out_trans_end;
471
472 ret = -EAGAIN;
473
474
475
476 if (!PageUptodate(page) || page->mapping != inode->i_mapping)
477 goto out_trans_end;
478
479
480 ret = 0;
481 if (gfs2_is_stuffed(ip))
482 ret = gfs2_unstuff_dinode(ip, page);
483 if (ret == 0)
484 ret = gfs2_allocate_page_backing(page);
485
486out_trans_end:
487 if (ret)
488 unlock_page(page);
489 gfs2_trans_end(sdp);
490out_trans_fail:
491 gfs2_inplace_release(ip);
492out_quota_unlock:
493 gfs2_quota_unlock(ip);
494out_unlock:
495 gfs2_glock_dq(&gh);
496out_uninit:
497 gfs2_holder_uninit(&gh);
498 if (ret == 0) {
499 set_page_dirty(page);
500 wait_for_stable_page(page);
501 }
502out:
503 sb_end_pagefault(inode->i_sb);
504 return block_page_mkwrite_return(ret);
505}
506
507static const struct vm_operations_struct gfs2_vm_ops = {
508 .fault = filemap_fault,
509 .page_mkwrite = gfs2_page_mkwrite,
510 .remap_pages = generic_file_remap_pages,
511};
512
513
514
515
516
517
518
519
520
521
522
523
524
525static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
526{
527 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
528
529 if (!(file->f_flags & O_NOATIME) &&
530 !IS_NOATIME(&ip->i_inode)) {
531 struct gfs2_holder i_gh;
532 int error;
533
534 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
535 &i_gh);
536 if (error)
537 return error;
538
539 gfs2_glock_dq_uninit(&i_gh);
540 file_accessed(file);
541 }
542 vma->vm_ops = &gfs2_vm_ops;
543
544 return 0;
545}
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560int gfs2_open_common(struct inode *inode, struct file *file)
561{
562 struct gfs2_file *fp;
563 int ret;
564
565 if (S_ISREG(inode->i_mode)) {
566 ret = generic_file_open(inode, file);
567 if (ret)
568 return ret;
569 }
570
571 fp = kzalloc(sizeof(struct gfs2_file), GFP_NOFS);
572 if (!fp)
573 return -ENOMEM;
574
575 mutex_init(&fp->f_fl_mutex);
576
577 gfs2_assert_warn(GFS2_SB(inode), !file->private_data);
578 file->private_data = fp;
579 return 0;
580}
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596static int gfs2_open(struct inode *inode, struct file *file)
597{
598 struct gfs2_inode *ip = GFS2_I(inode);
599 struct gfs2_holder i_gh;
600 int error;
601 bool need_unlock = false;
602
603 if (S_ISREG(ip->i_inode.i_mode)) {
604 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
605 &i_gh);
606 if (error)
607 return error;
608 need_unlock = true;
609 }
610
611 error = gfs2_open_common(inode, file);
612
613 if (need_unlock)
614 gfs2_glock_dq_uninit(&i_gh);
615
616 return error;
617}
618
619
620
621
622
623
624
625
626
627static int gfs2_release(struct inode *inode, struct file *file)
628{
629 struct gfs2_inode *ip = GFS2_I(inode);
630
631 kfree(file->private_data);
632 file->private_data = NULL;
633
634 if (!(file->f_mode & FMODE_WRITE))
635 return 0;
636
637 gfs2_rsqa_delete(ip, &inode->i_writecount);
638 return 0;
639}
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
663 int datasync)
664{
665 struct address_space *mapping = file->f_mapping;
666 struct inode *inode = mapping->host;
667 int sync_state = inode->i_state & I_DIRTY;
668 struct gfs2_inode *ip = GFS2_I(inode);
669 int ret = 0, ret1 = 0;
670
671 if (mapping->nrpages) {
672 ret1 = filemap_fdatawrite_range(mapping, start, end);
673 if (ret1 == -EIO)
674 return ret1;
675 }
676
677 if (!gfs2_is_jdata(ip))
678 sync_state &= ~I_DIRTY_PAGES;
679 if (datasync)
680 sync_state &= ~I_DIRTY_SYNC;
681
682 if (sync_state) {
683 ret = sync_inode_metadata(inode, 1);
684 if (ret)
685 return ret;
686 if (gfs2_is_jdata(ip))
687 filemap_write_and_wait(mapping);
688 gfs2_ail_flush(ip->i_gl, 1);
689 }
690
691 if (mapping->nrpages)
692 ret = filemap_fdatawait_range(mapping, start, end);
693
694 return ret ? ret : ret1;
695}
696
697
698
699
700
701
702
703
704
705
706
707
708
709static ssize_t gfs2_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
710 unsigned long nr_segs, loff_t pos)
711{
712 struct file *file = iocb->ki_filp;
713 struct address_space *mapping = file->f_mapping;
714 struct inode *inode = mapping->host;
715 struct gfs2_inode *ip = GFS2_I(inode);
716 size_t ocount, count;
717 ssize_t ret;
718
719 ret = gfs2_rsqa_alloc(ip);
720 if (ret)
721 return ret;
722
723 gfs2_size_hint(file, pos, iov_length(iov, nr_segs));
724
725 if (file->f_flags & O_APPEND) {
726 struct gfs2_holder gh;
727
728 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
729 if (ret)
730 return ret;
731 gfs2_glock_dq_uninit(&gh);
732 }
733
734 if (io_is_direct(file))
735 return generic_file_aio_write(iocb, iov, nr_segs, pos);
736
737 ocount = 0;
738 ret = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
739 if (ret)
740 return ret;
741
742 count = ocount;
743
744 BUG_ON(iocb->ki_pos != pos);
745
746 inode_lock(inode);
747 ret = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
748 if (ret)
749 goto out;
750
751 if (count == 0)
752 goto out;
753
754
755 current->backing_dev_info = mapping->backing_dev_info;
756
757 ret = file_remove_privs(file);
758 if (ret)
759 goto out2;
760
761 ret = file_update_time(file);
762 if (ret)
763 goto out2;
764
765 ret = iomap_file_buffered_write(iocb, iov, nr_segs, pos, &iocb->ki_pos,
766 count, &gfs2_iomap_ops);
767
768out2:
769 current->backing_dev_info = NULL;
770out:
771 inode_unlock(inode);
772 if (likely(ret > 0)) {
773 ssize_t err;
774
775
776 err = generic_write_sync(file, pos, ret);
777 if (err < 0 && ret > 0)
778 ret = err;
779 }
780 return ret;
781}
782
783static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
784 int mode)
785{
786 struct super_block *sb = inode->i_sb;
787 struct gfs2_inode *ip = GFS2_I(inode);
788 loff_t end = offset + len;
789 struct buffer_head *dibh;
790 struct iomap iomap = { };
791 int error;
792
793 error = gfs2_meta_inode_buffer(ip, &dibh);
794 if (unlikely(error))
795 return error;
796
797 gfs2_trans_add_meta(ip->i_gl, dibh);
798
799 if (gfs2_is_stuffed(ip)) {
800 error = gfs2_unstuff_dinode(ip, NULL);
801 if (unlikely(error))
802 goto out;
803 }
804
805 while (offset < end) {
806 error = gfs2_iomap_get_alloc(inode, offset, end - offset,
807 &iomap);
808 if (error)
809 goto out;
810 offset = iomap.offset + iomap.length;
811 if (!(iomap.flags & IOMAP_F_NEW))
812 continue;
813 error = sb_issue_zeroout(sb, iomap.addr >> inode->i_blkbits,
814 iomap.length >> inode->i_blkbits,
815 GFP_NOFS);
816 if (error) {
817 fs_err(GFS2_SB(inode), "Failed to zero data buffers\n");
818 goto out;
819 }
820 }
821out:
822 brelse(dibh);
823 return error;
824}
825
826
827
828
829
830
831
832
833
834
835
836static void calc_max_reserv(struct gfs2_inode *ip, loff_t *len,
837 unsigned int *data_blocks, unsigned int *ind_blocks,
838 unsigned int max_blocks)
839{
840 loff_t max = *len;
841 const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
842 unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
843
844 for (tmp = max_data; tmp > sdp->sd_diptrs;) {
845 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
846 max_data -= tmp;
847 }
848
849 *data_blocks = max_data;
850 *ind_blocks = max_blocks - max_data;
851 *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
852 if (*len > max) {
853 *len = max;
854 gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
855 }
856}
857
858static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
859{
860 struct inode *inode = file_inode(file);
861 struct gfs2_sbd *sdp = GFS2_SB(inode);
862 struct gfs2_inode *ip = GFS2_I(inode);
863 struct gfs2_alloc_parms ap = { .aflags = 0, };
864 unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
865 loff_t bytes, max_bytes, max_blks;
866 int error;
867 const loff_t pos = offset;
868 const loff_t count = len;
869 loff_t bsize_mask = ~((loff_t)sdp->sd_sb.sb_bsize - 1);
870 loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
871 loff_t max_chunk_size = UINT_MAX & bsize_mask;
872
873 next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
874
875 offset &= bsize_mask;
876
877 len = next - offset;
878 bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
879 if (!bytes)
880 bytes = UINT_MAX;
881 bytes &= bsize_mask;
882 if (bytes == 0)
883 bytes = sdp->sd_sb.sb_bsize;
884
885 gfs2_size_hint(file, offset, len);
886
887 gfs2_write_calc_reserv(ip, PAGE_SIZE, &data_blocks, &ind_blocks);
888 ap.min_target = data_blocks + ind_blocks;
889
890 while (len > 0) {
891 if (len < bytes)
892 bytes = len;
893 if (!gfs2_write_alloc_required(ip, offset, bytes)) {
894 len -= bytes;
895 offset += bytes;
896 continue;
897 }
898
899
900
901
902
903 max_bytes = (len > max_chunk_size) ? max_chunk_size : len;
904
905
906
907
908
909 gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
910 ap.target = data_blocks + ind_blocks;
911
912 error = gfs2_quota_lock_check(ip, &ap);
913 if (error)
914 return error;
915
916
917 max_blks = UINT_MAX;
918 if (ap.allowed)
919 max_blks = ap.allowed;
920
921 error = gfs2_inplace_reserve(ip, &ap);
922 if (error)
923 goto out_qunlock;
924
925
926 if (ap.allowed && ap.allowed < max_blks)
927 max_blks = ap.allowed;
928
929
930
931
932 calc_max_reserv(ip, &max_bytes, &data_blocks,
933 &ind_blocks, max_blks);
934
935 rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
936 RES_RG_HDR + gfs2_rg_blocks(ip, data_blocks + ind_blocks);
937 if (gfs2_is_jdata(ip))
938 rblocks += data_blocks ? data_blocks : 1;
939
940 error = gfs2_trans_begin(sdp, rblocks,
941 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
942 if (error)
943 goto out_trans_fail;
944
945 error = fallocate_chunk(inode, offset, max_bytes, mode);
946 gfs2_trans_end(sdp);
947
948 if (error)
949 goto out_trans_fail;
950
951 len -= max_bytes;
952 offset += max_bytes;
953 gfs2_inplace_release(ip);
954 gfs2_quota_unlock(ip);
955 }
956
957 if (!(mode & FALLOC_FL_KEEP_SIZE) && (pos + count) > inode->i_size) {
958 i_size_write(inode, pos + count);
959 file_update_time(file);
960 mark_inode_dirty(inode);
961 }
962
963 return generic_write_sync(file, pos, count);
964
965out_trans_fail:
966 gfs2_inplace_release(ip);
967out_qunlock:
968 gfs2_quota_unlock(ip);
969 return error;
970}
971
972static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
973{
974 struct inode *inode = file_inode(file);
975 struct gfs2_sbd *sdp = GFS2_SB(inode);
976 struct gfs2_inode *ip = GFS2_I(inode);
977 struct gfs2_holder gh;
978 int ret;
979
980 if (mode & ~(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE))
981 return -EOPNOTSUPP;
982
983 if (gfs2_is_jdata(ip) && inode != sdp->sd_rindex)
984 return -EOPNOTSUPP;
985
986 mutex_lock(&inode->i_mutex);
987
988 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
989 ret = gfs2_glock_nq(&gh);
990 if (ret)
991 goto out_uninit;
992
993 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
994 (offset + len) > inode->i_size) {
995 ret = inode_newsize_ok(inode, offset + len);
996 if (ret)
997 goto out_unlock;
998 }
999
1000 ret = get_write_access(inode);
1001 if (ret)
1002 goto out_unlock;
1003
1004 if (mode & FALLOC_FL_PUNCH_HOLE) {
1005 ret = __gfs2_punch_hole(file, offset, len);
1006 } else {
1007 ret = gfs2_rsqa_alloc(ip);
1008 if (ret)
1009 goto out_putw;
1010
1011 ret = __gfs2_fallocate(file, mode, offset, len);
1012
1013 if (ret)
1014 gfs2_rs_deltree(&ip->i_res);
1015 }
1016
1017out_putw:
1018 put_write_access(inode);
1019out_unlock:
1020 gfs2_glock_dq(&gh);
1021out_uninit:
1022 gfs2_holder_uninit(&gh);
1023 mutex_unlock(&inode->i_mutex);
1024 return ret;
1025}
1026
1027static ssize_t gfs2_file_splice_read(struct file *in, loff_t *ppos,
1028 struct pipe_inode_info *pipe, size_t len,
1029 unsigned int flags)
1030{
1031 struct inode *inode = in->f_mapping->host;
1032 struct gfs2_inode *ip = GFS2_I(inode);
1033 struct gfs2_holder gh;
1034 int ret;
1035
1036 mutex_lock(&inode->i_mutex);
1037
1038 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1039 if (ret) {
1040 mutex_unlock(&inode->i_mutex);
1041 return ret;
1042 }
1043
1044 gfs2_glock_dq_uninit(&gh);
1045 mutex_unlock(&inode->i_mutex);
1046
1047 return generic_file_splice_read(in, ppos, pipe, len, flags);
1048}
1049
1050static ssize_t gfs2_file_splice_write(struct pipe_inode_info *pipe,
1051 struct file *out, loff_t *ppos,
1052 size_t len, unsigned int flags)
1053{
1054 int error;
1055 struct gfs2_inode *ip = GFS2_I(out->f_mapping->host);
1056
1057 error = gfs2_rsqa_alloc(ip);
1058 if (error)
1059 return (ssize_t)error;
1060
1061 gfs2_size_hint(out, *ppos, len);
1062
1063 return generic_file_splice_write(pipe, out, ppos, len, flags);
1064}
1065
1066#ifdef CONFIG_GFS2_FS_LOCKING_DLM
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083static int gfs2_setlease(struct file *file, long arg, struct file_lock **fl, void **priv)
1084{
1085 return -EINVAL;
1086}
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
1098{
1099 struct gfs2_inode *ip = GFS2_I(file->f_mapping->host);
1100 struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host);
1101 struct lm_lockstruct *ls = &sdp->sd_lockstruct;
1102
1103 if (!(fl->fl_flags & FL_POSIX))
1104 return -ENOLCK;
1105 if (__mandatory_lock(&ip->i_inode) && fl->fl_type != F_UNLCK)
1106 return -ENOLCK;
1107
1108 if (cmd == F_CANCELLK) {
1109
1110 cmd = F_SETLK;
1111 fl->fl_type = F_UNLCK;
1112 }
1113 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) {
1114 if (fl->fl_type == F_UNLCK)
1115 locks_lock_file_wait(file, fl);
1116 return -EIO;
1117 }
1118 if (IS_GETLK(cmd))
1119 return dlm_posix_get(ls->ls_dlm, ip->i_no_addr, file, fl);
1120 else if (fl->fl_type == F_UNLCK)
1121 return dlm_posix_unlock(ls->ls_dlm, ip->i_no_addr, file, fl);
1122 else
1123 return dlm_posix_lock(ls->ls_dlm, ip->i_no_addr, file, cmd, fl);
1124}
1125
1126static int do_flock(struct file *file, int cmd, struct file_lock *fl)
1127{
1128 struct gfs2_file *fp = file->private_data;
1129 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
1130 struct gfs2_inode *ip = GFS2_I(file_inode(file));
1131 struct gfs2_glock *gl;
1132 unsigned int state;
1133 u16 flags;
1134 int error = 0;
1135 int sleeptime;
1136
1137 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
1138 flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY_1CB) | GL_EXACT;
1139
1140 mutex_lock(&fp->f_fl_mutex);
1141
1142 gl = fl_gh->gh_gl;
1143 if (gl) {
1144 if (fl_gh->gh_state == state)
1145 goto out;
1146 locks_lock_file_wait(file,
1147 &(struct file_lock) {
1148 .fl_type = F_UNLCK,
1149 .fl_flags = FL_FLOCK
1150 });
1151 gfs2_glock_dq(fl_gh);
1152 gfs2_holder_reinit(state, flags, fl_gh);
1153 } else {
1154 error = gfs2_glock_get(GFS2_SB(&ip->i_inode), ip->i_no_addr,
1155 &gfs2_flock_glops, CREATE, &gl);
1156 if (error)
1157 goto out;
1158 gfs2_holder_init(gl, state, flags, fl_gh);
1159 gfs2_glock_put(gl);
1160 }
1161 for (sleeptime = 1; sleeptime <= 4; sleeptime <<= 1) {
1162 error = gfs2_glock_nq(fl_gh);
1163 if (error != GLR_TRYFAILED)
1164 break;
1165 fl_gh->gh_flags = LM_FLAG_TRY | GL_EXACT;
1166 fl_gh->gh_error = 0;
1167 msleep(sleeptime);
1168 }
1169 if (error) {
1170 gfs2_holder_uninit(fl_gh);
1171 if (error == GLR_TRYFAILED)
1172 error = -EAGAIN;
1173 } else {
1174 error = locks_lock_file_wait(file, fl);
1175 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1176 }
1177
1178out:
1179 mutex_unlock(&fp->f_fl_mutex);
1180 return error;
1181}
1182
1183static void do_unflock(struct file *file, struct file_lock *fl)
1184{
1185 struct gfs2_file *fp = file->private_data;
1186 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
1187
1188 mutex_lock(&fp->f_fl_mutex);
1189 locks_lock_file_wait(file, fl);
1190 if (gfs2_holder_initialized(fl_gh)) {
1191 gfs2_glock_dq(fl_gh);
1192 gfs2_holder_uninit(fl_gh);
1193 }
1194 mutex_unlock(&fp->f_fl_mutex);
1195}
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
1207{
1208 if (!(fl->fl_flags & FL_FLOCK))
1209 return -ENOLCK;
1210 if (fl->fl_type & LOCK_MAND)
1211 return -EOPNOTSUPP;
1212
1213 if (fl->fl_type == F_UNLCK) {
1214 do_unflock(file, fl);
1215 return 0;
1216 } else {
1217 return do_flock(file, cmd, fl);
1218 }
1219}
1220
1221const struct file_operations gfs2_file_fops = {
1222 .llseek = gfs2_llseek,
1223 .read = do_sync_read,
1224 .aio_read = generic_file_aio_read,
1225 .write = do_sync_write,
1226 .aio_write = gfs2_file_aio_write,
1227 .unlocked_ioctl = gfs2_ioctl,
1228 .mmap = gfs2_mmap,
1229 .open = gfs2_open,
1230 .release = gfs2_release,
1231 .fsync = gfs2_fsync,
1232 .lock = gfs2_lock,
1233 .flock = gfs2_flock,
1234 .splice_read = gfs2_file_splice_read,
1235 .splice_write = gfs2_file_splice_write,
1236 .setlease = gfs2_setlease,
1237 .fallocate = gfs2_fallocate,
1238};
1239
1240const struct file_operations gfs2_dir_fops = {
1241 .readdir = gfs2_readdir,
1242 .unlocked_ioctl = gfs2_ioctl,
1243 .open = gfs2_open,
1244 .release = gfs2_release,
1245 .fsync = gfs2_fsync,
1246 .lock = gfs2_lock,
1247 .flock = gfs2_flock,
1248 .llseek = default_llseek,
1249};
1250
1251#endif
1252
1253const struct file_operations gfs2_file_fops_nolock = {
1254 .llseek = gfs2_llseek,
1255 .read = do_sync_read,
1256 .aio_read = generic_file_aio_read,
1257 .write = do_sync_write,
1258 .aio_write = gfs2_file_aio_write,
1259 .unlocked_ioctl = gfs2_ioctl,
1260 .mmap = gfs2_mmap,
1261 .open = gfs2_open,
1262 .release = gfs2_release,
1263 .fsync = gfs2_fsync,
1264 .splice_read = gfs2_file_splice_read,
1265 .splice_write = gfs2_file_splice_write,
1266 .setlease = generic_setlease,
1267 .fallocate = gfs2_fallocate,
1268};
1269
1270const struct file_operations gfs2_dir_fops_nolock = {
1271 .readdir = gfs2_readdir,
1272 .unlocked_ioctl = gfs2_ioctl,
1273 .open = gfs2_open,
1274 .release = gfs2_release,
1275 .fsync = gfs2_fsync,
1276 .llseek = default_llseek,
1277};
1278
1279