10 #include <linux/slab.h>
15 #include <linux/uio.h>
21 #include <linux/falloc.h>
25 #include <asm/uaccess.h>
26 #include <linux/dlm.h>
27 #include <linux/dlm_plock.h>
92 static int gfs2_readdir(
struct file *file,
void *
dirent,
filldir_t filldir)
138 static const u32 fsflags_to_gfs2[32] = {
148 static const u32 gfs2_to_fsflags[32] = {
158 static int gfs2_get_flags(
struct file *filp,
u32 __user *
ptr)
171 fsflags = fsflags_cvt(gfs2_to_fsflags, ip->
i_diskflags);
202 #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \
203 GFS2_DIF_IMMUTABLE| \
204 GFS2_DIF_APPENDONLY| \
209 GFS2_DIF_INHERIT_JDATA)
218 static int do_gfs2_set_flags(
struct file *filp,
u32 reqflags,
u32 mask)
220 struct inode *inode = filp->
f_path.dentry->d_inode;
222 struct gfs2_sbd *sdp = GFS2_SB(inode);
223 struct buffer_head *bh;
242 new_flags = (flags & ~mask) | (reqflags & mask);
243 if ((new_flags ^ flags) == 0)
264 if (flags & GFS2_DIF_JDATA)
276 error = gfs2_meta_inode_buffer(ip, &bh);
294 static int gfs2_set_flags(
struct file *filp,
u32 __user *ptr)
296 struct inode *inode = filp->
f_path.dentry->d_inode;
297 u32 fsflags, gfsflags;
302 gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags);
307 return do_gfs2_set_flags(filp, gfsflags, ~0);
312 static long gfs2_ioctl(
struct file *filp,
unsigned int cmd,
unsigned long arg)
316 return gfs2_get_flags(filp, (
u32 __user *)arg);
318 return gfs2_set_flags(filp, (
u32 __user *)arg);
337 static void gfs2_size_hint(
struct file *filep, loff_t offset,
size_t size)
339 struct inode *inode = filep->f_dentry->d_inode;
340 struct gfs2_sbd *sdp = GFS2_SB(inode);
342 size_t blks = (size + sdp->
sd_sb.sb_bsize - 1) >> sdp->
sd_sb.sb_bsize_shift;
358 static int gfs2_allocate_page_backing(
struct page *
page)
360 struct inode *inode = page->
mapping->host;
361 struct buffer_head bh;
369 if (!buffer_mapped(&bh))
372 lblock += (bh.b_size >> inode->
i_blkbits);
386 static int gfs2_page_mkwrite(
struct vm_area_struct *vma,
struct vm_fault *vmf)
388 struct page *page = vmf->page;
389 struct inode *inode = vma->
vm_file->f_path.dentry->d_inode;
391 struct gfs2_sbd *sdp = GFS2_SB(inode);
392 unsigned long last_index;
394 unsigned int data_blocks, ind_blocks, rblocks;
399 sb_start_pagefault(inode->
i_sb);
431 ret = gfs2_quota_lock_check(ip);
434 gfs2_write_calc_reserv(ip,
PAGE_CACHE_SIZE, &data_blocks, &ind_blocks);
437 goto out_quota_unlock;
440 if (gfs2_is_jdata(ip))
441 rblocks += data_blocks ? data_blocks : 1;
442 if (ind_blocks || data_blocks) {
444 rblocks += gfs2_rg_blocks(ip, data_blocks + ind_blocks);
452 size = i_size_read(inode);
455 if (size == 0 || (page->
index > last_index))
467 if (gfs2_is_stuffed(ip))
470 ret = gfs2_allocate_page_backing(page);
486 wait_on_page_writeback(page);
488 sb_end_pagefault(inode->
i_sb);
489 return block_page_mkwrite_return(ret);
492 static const struct vm_operations_struct gfs2_vm_ops = {
494 .page_mkwrite = gfs2_page_mkwrite,
510 static int gfs2_mmap(
struct file *file,
struct vm_area_struct *vma)
527 vma->
vm_ops = &gfs2_vm_ops;
540 static int gfs2_open(
struct inode *inode,
struct file *file)
589 static int gfs2_release(
struct inode *inode,
struct file *file)
624 static int gfs2_fsync(
struct file *file, loff_t
start, loff_t
end,
628 struct inode *inode = mapping->
host;
631 int ret = 0, ret1 = 0;
646 if (gfs2_is_jdata(ip))
654 return ret ? ret : ret1;
672 unsigned long nr_segs, loff_t pos)
674 struct file *file = iocb->
ki_filp;
675 size_t writesize = iov_length(iov, nr_segs);
684 gfs2_size_hint(file, pos, writesize);
698 static int fallocate_chunk(
struct inode *inode, loff_t offset, loff_t len,
702 struct buffer_head *dibh;
705 unsigned int nr_blks;
708 error = gfs2_meta_inode_buffer(ip, &dibh);
714 if (gfs2_is_stuffed(ip)) {
721 struct buffer_head bh_map = { .b_state = 0, .b_blocknr = 0 };
723 set_buffer_zeronew(&bh_map);
728 len -= bh_map.b_size;
729 nr_blks = bh_map.b_size >> inode->
i_blkbits;
731 if (!buffer_new(&bh_map))
733 if (
unlikely(!buffer_zeronew(&bh_map))) {
739 i_size_write(inode, offset + size);
741 mark_inode_dirty(inode);
748 static void calc_max_reserv(
struct gfs2_inode *ip, loff_t
max, loff_t *len,
749 unsigned int *data_blocks,
unsigned int *ind_blocks)
752 unsigned int max_blocks = ip->
i_rgd->rd_free_clone;
755 for (tmp = max_data; tmp > sdp->
sd_diptrs;) {
761 if (max_data <= *data_blocks)
763 *data_blocks = max_data;
764 *ind_blocks = max_blocks - max_data;
765 *len = ((loff_t)max_data - 3) << sdp->
sd_sb.sb_bsize_shift;
768 gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
772 static long gfs2_fallocate(
struct file *file,
int mode, loff_t offset,
775 struct inode *inode = file->
f_path.dentry->d_inode;
776 struct gfs2_sbd *sdp = GFS2_SB(inode);
778 unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
781 const loff_t pos =
offset;
782 const loff_t
count = len;
783 loff_t bsize_mask = ~((loff_t)sdp->
sd_sb.sb_bsize - 1);
784 loff_t
next = (offset + len - 1) >> sdp->
sd_sb.sb_bsize_shift;
785 loff_t max_chunk_size =
UINT_MAX & bsize_mask;
786 next = (next + 1) << sdp->
sd_sb.sb_bsize_shift;
792 offset &= bsize_mask;
800 bytes = sdp->
sd_sb.sb_bsize;
811 gfs2_size_hint(file, offset, len);
821 error = gfs2_quota_lock_check(ip);
826 gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
830 if (error == -
ENOSPC && bytes > sdp->
sd_sb.sb_bsize) {
834 bytes = sdp->
sd_sb.sb_bsize;
840 calc_max_reserv(ip, (len > max_chunk_size)? max_chunk_size: len,
841 &max_bytes, &data_blocks, &ind_blocks);
844 RES_RG_HDR + gfs2_rg_blocks(ip, data_blocks + ind_blocks);
845 if (gfs2_is_jdata(ip))
846 rblocks += data_blocks ? data_blocks : 1;
853 error = fallocate_chunk(inode, offset, max_bytes, mode);
880 #ifdef CONFIG_GFS2_FS_LOCKING_DLM
897 static int gfs2_setlease(
struct file *file,
long arg,
struct file_lock **
fl)
911 static int gfs2_lock(
struct file *file,
int cmd,
struct file_lock *
fl)
937 static int do_flock(
struct file *file,
int cmd,
struct file_lock *fl)
983 static void do_unflock(
struct file *file,
struct file_lock *fl)
1006 static int gfs2_flock(
struct file *file,
int cmd,
struct file_lock *fl)
1014 do_unflock(file, fl);
1017 return do_flock(file, cmd, fl);
1022 .llseek = gfs2_llseek,
1026 .aio_write = gfs2_file_aio_write,
1027 .unlocked_ioctl = gfs2_ioctl,
1030 .release = gfs2_release,
1031 .fsync = gfs2_fsync,
1033 .flock = gfs2_flock,
1036 .setlease = gfs2_setlease,
1037 .fallocate = gfs2_fallocate,
1041 .readdir = gfs2_readdir,
1042 .unlocked_ioctl = gfs2_ioctl,
1044 .release = gfs2_release,
1045 .fsync = gfs2_fsync,
1047 .flock = gfs2_flock,
1054 .llseek = gfs2_llseek,
1058 .aio_write = gfs2_file_aio_write,
1059 .unlocked_ioctl = gfs2_ioctl,
1062 .release = gfs2_release,
1063 .fsync = gfs2_fsync,
1067 .fallocate = gfs2_fallocate,
1071 .readdir = gfs2_readdir,
1072 .unlocked_ioctl = gfs2_ioctl,
1074 .release = gfs2_release,
1075 .fsync = gfs2_fsync,