fcntl(2) F_READAHEAD set to zero doesn't work [patch]

Konstantin Belousov kostikbel at gmail.com
Sun Feb 10 21:48:23 UTC 2013


On Fri, Feb 08, 2013 at 04:58:09PM -0700, Ian Lepore wrote:
> I discovered today that fcntl(fd, F_READAHEAD, 0) doesn't work as
> advertised.  It's supposed to disable readahead, but instead it restores
> the default readahead behavior (if it had previously been changed), and
> there is no way to disable readahead.[1]  I think the attached patch
> fixes it, but it's not immediately clear from the patch why; here's the
> deal...
> 
> The amount of readahead is calculated by sequential_heuristic() in
> vfs_vnops.c.  If the FRDAHEAD flag is set on the file it uses the value
> stored in the file's f_seqcount, otherwise it calculates a value (and
> updates f_seqcount, which doesn't ever happen when FRDAHEAD is set).
> 
> So the patch causes the FRDAHEAD flag to be set even in the case of the
> readahead amount being zero.  Because it seems like a useful concept, it
> still allows the readahead to be restored to default behavior, now by
> passing a negative value.
> 
> Does this look right to those of you who understand this part of the
> system better than I do?
It looks correct.

I do wonder if it better to keep the actual behaviour as is and set
the readahead amount to default on the arg == 0, disabling readahead
for arg < 0. It is too exteme, most likely, please proceed.

> 
> -- Ian
> 
> [1] No way using F_READAHEAD; I know about POSIX_FADV_RANDOM.

> Index: sys/kern/kern_descrip.c
> ===================================================================
> --- sys/kern/kern_descrip.c	(revision 246337)
> +++ sys/kern/kern_descrip.c	(working copy)
> @@ -776,7 +776,7 @@
>  		}
>  		fhold(fp);
>  		FILEDESC_SUNLOCK(fdp);
> -		if (arg != 0) {
> +		if (arg >= 0) {
>  			vp = fp->f_vnode;
>  			error = vn_lock(vp, LK_SHARED);
>  			if (error != 0) {
> Index: lib/libc/sys/fcntl.2
> ===================================================================
> --- lib/libc/sys/fcntl.2	(revision 246337)
> +++ lib/libc/sys/fcntl.2	(working copy)
> @@ -28,7 +28,7 @@
>  .\"     @(#)fcntl.2	8.2 (Berkeley) 1/12/94
>  .\" $FreeBSD$
>  .\"
> -.Dd July 27, 2012
> +.Dd February 8, 2013
>  .Dt FCNTL 2
>  .Os
>  .Sh NAME
> @@ -171,7 +171,7 @@
>  which is rounded up to the nearest block size.
>  A zero value in
>  .Fa arg
> -turns off read ahead.
> +turns off read ahead, a negative value restores the system default.
>  .It Dv F_RDAHEAD
>  Equivalent to Darwin counterpart which sets read ahead amount of 128KB
>  when the third argument,

> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 834 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20130210/1e6482cc/attachment.sig>


More information about the freebsd-hackers mailing list