@@ -428,11 +428,11 @@ static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
428428 if (ret )
429429 goto out_unlock ;
430430
431- ret = gfs2_quota_lock_check (ip );
432- if (ret )
433- goto out_unlock ;
434431 gfs2_write_calc_reserv (ip , PAGE_CACHE_SIZE , & data_blocks , & ind_blocks );
435432 ap .target = data_blocks + ind_blocks ;
433+ ret = gfs2_quota_lock_check (ip , & ap );
434+ if (ret )
435+ goto out_unlock ;
436436 ret = gfs2_inplace_reserve (ip , & ap );
437437 if (ret )
438438 goto out_quota_unlock ;
@@ -764,22 +764,30 @@ static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
764764 brelse (dibh );
765765 return error ;
766766}
767-
768- static void calc_max_reserv (struct gfs2_inode * ip , loff_t max , loff_t * len ,
769- unsigned int * data_blocks , unsigned int * ind_blocks )
767+ /**
768+ * calc_max_reserv() - Reverse of write_calc_reserv. Given a number of
769+ * blocks, determine how many bytes can be written.
770+ * @ip: The inode in question.
771+ * @len: Max cap of bytes. What we return in *len must be <= this.
772+ * @data_blocks: Compute and return the number of data blocks needed
773+ * @ind_blocks: Compute and return the number of indirect blocks needed
774+ * @max_blocks: The total blocks available to work with.
775+ *
776+ * Returns: void, but @len, @data_blocks and @ind_blocks are filled in.
777+ */
778+ static void calc_max_reserv (struct gfs2_inode * ip , loff_t * len ,
779+ unsigned int * data_blocks , unsigned int * ind_blocks ,
780+ unsigned int max_blocks )
770781{
782+ loff_t max = * len ;
771783 const struct gfs2_sbd * sdp = GFS2_SB (& ip -> i_inode );
772- unsigned int max_blocks = ip -> i_rgd -> rd_free_clone ;
773784 unsigned int tmp , max_data = max_blocks - 3 * (sdp -> sd_max_height - 1 );
774785
775786 for (tmp = max_data ; tmp > sdp -> sd_diptrs ;) {
776787 tmp = DIV_ROUND_UP (tmp , sdp -> sd_inptrs );
777788 max_data -= tmp ;
778789 }
779- /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
780- so it might end up with fewer data blocks */
781- if (max_data <= * data_blocks )
782- return ;
790+
783791 * data_blocks = max_data ;
784792 * ind_blocks = max_blocks - max_data ;
785793 * len = ((loff_t )max_data - 3 ) << sdp -> sd_sb .sb_bsize_shift ;
@@ -796,7 +804,7 @@ static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t
796804 struct gfs2_inode * ip = GFS2_I (inode );
797805 struct gfs2_alloc_parms ap = { .aflags = 0 , };
798806 unsigned int data_blocks = 0 , ind_blocks = 0 , rblocks ;
799- loff_t bytes , max_bytes ;
807+ loff_t bytes , max_bytes , max_blks = UINT_MAX ;
800808 int error ;
801809 const loff_t pos = offset ;
802810 const loff_t count = len ;
@@ -818,6 +826,9 @@ static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t
818826
819827 gfs2_size_hint (file , offset , len );
820828
829+ gfs2_write_calc_reserv (ip , PAGE_SIZE , & data_blocks , & ind_blocks );
830+ ap .min_target = data_blocks + ind_blocks ;
831+
821832 while (len > 0 ) {
822833 if (len < bytes )
823834 bytes = len ;
@@ -826,27 +837,41 @@ static long __gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t
826837 offset += bytes ;
827838 continue ;
828839 }
829- error = gfs2_quota_lock_check (ip );
840+
841+ /* We need to determine how many bytes we can actually
842+ * fallocate without exceeding quota or going over the
843+ * end of the fs. We start off optimistically by assuming
844+ * we can write max_bytes */
845+ max_bytes = (len > max_chunk_size ) ? max_chunk_size : len ;
846+
847+ /* Since max_bytes is most likely a theoretical max, we
848+ * calculate a more realistic 'bytes' to serve as a good
849+ * starting point for the number of bytes we may be able
850+ * to write */
851+ gfs2_write_calc_reserv (ip , bytes , & data_blocks , & ind_blocks );
852+ ap .target = data_blocks + ind_blocks ;
853+
854+ error = gfs2_quota_lock_check (ip , & ap );
830855 if (error )
831856 return error ;
832- retry :
833- gfs2_write_calc_reserv (ip , bytes , & data_blocks , & ind_blocks );
857+ /* ap.allowed tells us how many blocks quota will allow
858+ * us to write. Check if this reduces max_blks */
859+ if (ap .allowed && ap .allowed < max_blks )
860+ max_blks = ap .allowed ;
834861
835- ap .target = data_blocks + ind_blocks ;
836862 error = gfs2_inplace_reserve (ip , & ap );
837- if (error ) {
838- if (error == - ENOSPC && bytes > sdp -> sd_sb .sb_bsize ) {
839- bytes >>= 1 ;
840- bytes &= bsize_mask ;
841- if (bytes == 0 )
842- bytes = sdp -> sd_sb .sb_bsize ;
843- goto retry ;
844- }
863+ if (error )
845864 goto out_qunlock ;
846- }
847- max_bytes = bytes ;
848- calc_max_reserv (ip , (len > max_chunk_size )? max_chunk_size : len ,
849- & max_bytes , & data_blocks , & ind_blocks );
865+
866+ /* check if the selected rgrp limits our max_blks further */
867+ if (ap .allowed && ap .allowed < max_blks )
868+ max_blks = ap .allowed ;
869+
870+ /* Almost done. Calculate bytes that can be written using
871+ * max_blks. We also recompute max_bytes, data_blocks and
872+ * ind_blocks */
873+ calc_max_reserv (ip , & max_bytes , & data_blocks ,
874+ & ind_blocks , max_blks );
850875
851876 rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
852877 RES_RG_HDR + gfs2_rg_blocks (ip , data_blocks + ind_blocks );
@@ -930,6 +955,22 @@ static long gfs2_fallocate(struct file *file, int mode, loff_t offset, loff_t le
930955 return ret ;
931956}
932957
958+ static ssize_t gfs2_file_splice_write (struct pipe_inode_info * pipe ,
959+ struct file * out , loff_t * ppos ,
960+ size_t len , unsigned int flags )
961+ {
962+ int error ;
963+ struct gfs2_inode * ip = GFS2_I (out -> f_mapping -> host );
964+
965+ error = gfs2_rs_alloc (ip );
966+ if (error )
967+ return (ssize_t )error ;
968+
969+ gfs2_size_hint (out , * ppos , len );
970+
971+ return iter_file_splice_write (pipe , out , ppos , len , flags );
972+ }
973+
933974#ifdef CONFIG_GFS2_FS_LOCKING_DLM
934975
935976/**
@@ -1076,7 +1117,7 @@ const struct file_operations gfs2_file_fops = {
10761117 .lock = gfs2_lock ,
10771118 .flock = gfs2_flock ,
10781119 .splice_read = generic_file_splice_read ,
1079- .splice_write = iter_file_splice_write ,
1120+ .splice_write = gfs2_file_splice_write ,
10801121 .setlease = simple_nosetlease ,
10811122 .fallocate = gfs2_fallocate ,
10821123};
@@ -1106,7 +1147,7 @@ const struct file_operations gfs2_file_fops_nolock = {
11061147 .release = gfs2_release ,
11071148 .fsync = gfs2_fsync ,
11081149 .splice_read = generic_file_splice_read ,
1109- .splice_write = iter_file_splice_write ,
1150+ .splice_write = gfs2_file_splice_write ,
11101151 .setlease = generic_setlease ,
11111152 .fallocate = gfs2_fallocate ,
11121153};
0 commit comments