Skip to contentSkip to conversation

fcntl: Add the F_READAHEAD command

PATCH Superseded Morten Hustveit 2 participants 3 messages Nov 25, 2010 – Nov 26, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fcntl: Add the F_READAHEAD command

Allow userland applications to control the read-ahead per file descriptor.

For example, this interface can be used to adjust read sizes in applications using sendfile() to serve many streams from disk.

The implementation is based on sys_readahead() and sys_fadvise64().

The same interface was added to FreeBSD in 2009.

---

fs/fcntl.c +28 -0
index ecc8b39..19a8a46 100644
@@ -340,6 +340,31 @@ static int f_getown_ex(struct file *filp, unsigned long arg)
 	return ret;
 }
 
+static int f_readahead(struct file *filp, unsigned long arg)
+{
+	struct address_space *mapping;
+
+	mapping = filp->f_mapping;
+	if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
+		return -EINVAL;
+
+	if (mapping->a_ops->get_xip_mem)
+		return 0;
+
+	if (arg) {
+		filp->f_ra.ra_pages = (arg + PAGE_SIZE - 1) >> PAGE_SHIFT;
+		spin_lock(&filp->f_lock);
+		filp->f_mode &= ~FMODE_RANDOM;
+		spin_unlock(&filp->f_lock);
+	} else {
+		spin_lock(&filp->f_lock);
+		filp->f_mode |= FMODE_RANDOM;
+		spin_unlock(&filp->f_lock);
+	}
+
+	return 0;
+}
+
 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
 		struct file *filp)
 {
@@ -420,6 +445,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
 	case F_GETPIPE_SZ:
 		err = pipe_fcntl(filp, cmd, arg);
 		break;
+	case F_READAHEAD:
+		err = f_readahead(filp, arg);
+		break;
 	default:
 		break;
 	}
include/linux/fcntl.h +5 -0
index afc00af..69d8e4d 100644
@@ -28,6 +28,11 @@
 #define F_GETPIPE_SZ	(F_LINUX_SPECIFIC_BASE + 8)
 
 /*
+ * Set read-ahead buffer size
+ */
+#define F_READAHEAD	(F_LINUX_SPECIFIC_BASE + 9)
+
+/*
  * Types of directory notifications that may be requested.
  */
 #define DN_ACCESS	0x00000001	/* File accessed */
#1 lore raw

fs/fcntl.c static int f_getown_ex(struct file *filp, unsigned long arg) · line 355

Quoted 4 lines · Morten Hustveit
fs/fcntl.c +4 -0
+static int f_readahead(struct file *filp, unsigned long arg)
+{
+	if (arg) {
+		filp->f_ra.ra_pages = (arg + PAGE_SIZE - 1) >> PAGE_SHIFT;

Functionality itself is fine, but it would be clearer if you used a better variable name than "arg" here. Something like "readahead_bytes" or similar would make it immediately clear what the parameter is.

Quoted 42 lines
fs/fcntl.c +12 -0
+		spin_lock(&filp->f_lock);
+		filp->f_mode &= ~FMODE_RANDOM;
+		spin_unlock(&filp->f_lock);
+	} else {
+		spin_lock(&filp->f_lock);
+		filp->f_mode |= FMODE_RANDOM;
+		spin_unlock(&filp->f_lock);
+	}
+
+	return 0;
+}
+

static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
struct file *filp)
{

fs/fcntl.c +3 -0
@@ -420,6 +445,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
 	case F_GETPIPE_SZ:
 		err = pipe_fcntl(filp, cmd, arg);
 		break;
+	case F_READAHEAD:
+		err = f_readahead(filp, arg);
+		break;
 	default:
 		break;
 	}
include/linux/fcntl.h +5 -1
index afc00af..69d8e4d 100644
@@ -28,6 +28,11 @@
 #define F_GETPIPE_SZ	(F_LINUX_SPECIFIC_BASE + 8)
 
 /*
+ * Set read-ahead buffer size
+ */
+#define F_READAHEAD	(F_LINUX_SPECIFIC_BASE + 9)
+
+/*
 * Types of directory notifications that may be requested.
 */
 #define DN_ACCESS	0x00000001	/* File accessed */
--
Quoted 3 lines

To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Cheers, Andreas

#2 lore raw

PATCH v2 1/1 fcntl: Add the F_READAHEAD command

Allow userland applications to control the read-ahead per file descriptor.

For example, this interface can be used to adjust read sizes in applications using sendfile() to serve many streams from disk.

The implementation is based on sys_readahead() and sys_fadvise64().

The same interface was added to FreeBSD in 2009.

---
Changes for v2:
- Renamed "arg" to "ra_bytes". "ra_bytes" was used instead of "readahead_bytes" to keep lines at less than 80 columns, and to be consistent with "ra_pages".

fs/fcntl.c +28 -0
index ecc8b39..9d78274 100644
@@ -340,6 +340,31 @@ static int f_getown_ex(struct file *filp, unsigned long arg)
 	return ret;
 }
 
+static int f_readahead(struct file *filp, unsigned long ra_bytes)
+{
+	struct address_space *mapping;
+
+	mapping = filp->f_mapping;
+	if (!mapping || !mapping->a_ops || !mapping->a_ops->readpage)
+		return -EINVAL;
+
+	if (mapping->a_ops->get_xip_mem)
+		return 0;
+
+	if (ra_bytes) {
+		filp->f_ra.ra_pages = (ra_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
+		spin_lock(&filp->f_lock);
+		filp->f_mode &= ~FMODE_RANDOM;
+		spin_unlock(&filp->f_lock);
+	} else {
+		spin_lock(&filp->f_lock);
+		filp->f_mode |= FMODE_RANDOM;
+		spin_unlock(&filp->f_lock);
+	}
+
+	return 0;
+}
+
 static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
 		struct file *filp)
 {
@@ -420,6 +445,9 @@ static long do_fcntl(int fd, unsigned int cmd, unsigned long arg,
 	case F_GETPIPE_SZ:
 		err = pipe_fcntl(filp, cmd, arg);
 		break;
+	case F_READAHEAD:
+		err = f_readahead(filp, arg);
+		break;
 	default:
 		break;
 	}
include/linux/fcntl.h +5 -0
index afc00af..69d8e4d 100644
@@ -28,6 +28,11 @@
 #define F_GETPIPE_SZ	(F_LINUX_SPECIFIC_BASE + 8)
 
 /*
+ * Set read-ahead buffer size
+ */
+#define F_READAHEAD	(F_LINUX_SPECIFIC_BASE + 9)
+
+/*
  * Types of directory notifications that may be requested.
  */
 #define DN_ACCESS	0x00000001	/* File accessed */