linux-8086.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Juan Perez-Sanchez <lithoxs@gmail.com>
To: linux-8086 <linux-8086@vger.kernel.org>
Subject: [PATCH 4 OF 5]
Date: Mon, 16 Mar 2015 01:27:38 -0600	[thread overview]
Message-ID: <CAD6VGuZ9QNKbmwLQLYj_LDOmJXU8ZOvZJfr=26e_cM256CdWuw@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 698 bytes --]

Hi,

This patch implements the behavior of open() and the flag O_NONBLOCK
when opening named pipes.

Moved release of pipe buffers from iput() to pipe_release(). Now,
fs/inode.c knows even less about pipes.

When possible, declared variables and functions in fs/pipe.c as static
and removed their prototypes from fs.h

Pipes can be tested with the following command:

# ls -l /bin | more

Named pipes can be checked with:

# mknod fifo p
# cat /etc/passwd > fifo &
# cat fifo

When you hit enter in the last command, you will see the contents of
file /etc/passwd on screen and then the message of the first 'cat'
exiting. Finally, remove the fifo:

# rm fifo

Code size was increased by 144 bytes.

[-- Attachment #2: elks-3p.patch --]
[-- Type: text/x-patch, Size: 3534 bytes --]

diff -Nur elks.orig/fs/inode.c elks/fs/inode.c
--- elks.orig/fs/inode.c	2015-03-12 12:24:04.000000000 -0600
+++ elks/fs/inode.c	2015-03-12 12:25:35.000000000 -0600
@@ -359,11 +359,6 @@
 	    }
 
 	    wake_up(&inode_wait);
-	    if (((inode->i_mode & S_IFMT) == S_IFIFO) && inode->u.pipe_i.base) {
-	    /* Free up any memory allocated to the pipe */
-		free_pipe_mem(inode->u.pipe_i.base);
-		inode->u.pipe_i.base = NULL;
-	    }
 
 	    if (inode->i_sb) {
 		struct super_operations *sop = inode->i_sb->s_op;
diff -Nur elks.orig/fs/pipe.c elks/fs/pipe.c
--- elks.orig/fs/pipe.c	2015-03-12 12:24:04.000000000 -0600
+++ elks/fs/pipe.c	2015-03-12 12:33:51.000000000 -0600
@@ -59,11 +59,11 @@
  *	V7, and they should be buffers
  */
 
-char pipe_base[MAX_PIPES][PIPE_BUF];
+static char pipe_base[MAX_PIPES][PIPE_BUF];
 
-int pipe_in_use[(MAX_PIPES + 15)/16];
+static int pipe_in_use[(MAX_PIPES + 15)/16];
 
-char *get_pipe_mem(void)
+static char *get_pipe_mem(void)
 {
     int i = 0;
 
@@ -77,7 +77,7 @@
     return NULL;
 }
 
-void free_pipe_mem(char *buf)
+static void free_pipe_mem(char *buf)
 {
     int i;
 
@@ -211,7 +211,15 @@
     if (filp->f_mode & FMODE_WRITE)
 	(inode->u.pipe_i.writers)--;
 
-    wake_up_interruptible(&(inode->u.pipe_i.wait));
+    if(!(inode->u.pipe_i.readers + inode->u.pipe_i.writers)) {
+	if(inode->u.pipe_i.base) {
+	/* Free up any memory allocated to the pipe */
+	    free_pipe_mem(inode->u.pipe_i.base);
+	    inode->u.pipe_i.base = NULL;
+	}
+    }
+    else
+	wake_up_interruptible(&(inode->u.pipe_i.wait));
 }
 
 #ifdef STRICT_PIPES
@@ -237,12 +245,6 @@
 {
     debug("PIPE: rdwr called.\n");
 
-    if (filp->f_mode & FMODE_READ)
-	(inode->u.pipe_i.readers)++;
-
-    if (filp->f_mode & FMODE_WRITE)
-	(inode->u.pipe_i.writers)++;
-
     if(!PIPE_BASE(*inode)) {
 	if(!(PIPE_BASE(*inode) = get_pipe_mem()))
 	    return -ENOMEM;
@@ -254,6 +256,32 @@
 	PIPE_LOCK(*inode) = 0;
 #endif
     }
+    if (filp->f_mode & FMODE_READ) {
+	(inode->u.pipe_i.readers)++;
+	if(inode->u.pipe_i.writers > 0) {
+	    if(inode->u.pipe_i.readers < 2)
+		wake_up_interruptible(&(inode->u.pipe_i.wait));
+	}
+	else {
+	    if(!(filp->f_flags & O_NONBLOCK) && (inode->i_sb))
+		while(!(inode->u.pipe_i.writers))
+		    interruptible_sleep_on(&(inode->u.pipe_i.wait));
+	}
+    }
+
+    if (filp->f_mode & FMODE_WRITE) {
+	(inode->u.pipe_i.writers)++;
+	if(inode->u.pipe_i.readers > 0) {
+	    if(inode->u.pipe_i.writers < 2)
+		wake_up_interruptible(&(inode->u.pipe_i.wait));
+	}
+	else {
+	    if(filp->f_flags & O_NONBLOCK)
+		return -ENXIO;
+	    while(!(inode->u.pipe_i.readers))
+		interruptible_sleep_on(&(inode->u.pipe_i.wait));
+	}
+    }
     return 0;
 }
 
@@ -320,7 +348,7 @@
 
 /*@+type@*/
 
-int do_pipe(int *fd)
+static int do_pipe(int *fd)
 {
     register struct inode *inode;
     struct file *f1;
diff -Nur elks.orig/include/linuxmt/fs.h elks/include/linuxmt/fs.h
--- elks.orig/include/linuxmt/fs.h	2015-03-12 12:24:04.000000000 -0600
+++ elks/include/linuxmt/fs.h	2015-03-12 12:34:09.000000000 -0600
@@ -453,7 +453,6 @@
 
 extern int open_namei(char *,int,int,struct inode **,struct inode *);
 extern int do_mknod(char *,int,dev_t);
-extern int do_pipe(int *);
 extern void iput(struct inode *);
 
 extern struct inode *get_empty_inode(void);
@@ -510,8 +509,6 @@
 extern struct buffer_head *bread(dev_t,block_t);
 
 extern int get_unused_fd(struct file *);
-extern char *get_pipe_mem(void);
-extern void free_pipe_mem(char *buf);
 
 extern void mark_buffer_uptodate(struct buffer_head *,int);
 

             reply	other threads:[~2015-03-16  7:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-16  7:27 Juan Perez-Sanchez [this message]
  -- strict thread matches above, loose matches on Subject: below --
2014-11-09 19:34 [PATCH 4 of 5] Juan Perez-Sanchez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAD6VGuZ9QNKbmwLQLYj_LDOmJXU8ZOvZJfr=26e_cM256CdWuw@mail.gmail.com' \
    --to=lithoxs@gmail.com \
    --cc=linux-8086@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).