Skip to contentSkip to conversation

Kernel bug: Bad page state: related to generic symlink code and mmap

DISCUSSION Anton Altaparmakov 6 participants 31 messages Aug 19, 2005 – Aug 20, 2005

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

Hi,

There is a bug somewhere in 2.6.11.4 and I can't figure out where it is. I assume it is present in older and newer kernels, too as the related code hasn't changed much AFAICS and googling for "Bad page state" returns rather a lot of hits relating to both older (up to 2.5.70!) and newer kernels...

Note: PLEASE do not stop reading because you read ncpfs below as I am pretty sure it is not ncpfs related! And looking at google a lot of people have reported such similar problems since 2.5.70 or so and they were all told to go away as they have bad ram. That is impossible because this happens on well over 600 workstations and several servers 100% reproducible. Many different types of hardware, different makes, difference age, all running smp kernels even if single cpu. You can't tell me they all have bad ram. Windows works fine and Linux works fine except for that one specific problem which is 100% reproducible...

The bug only appears, but it appears 100% reproducibly when a cross volume symlink on ncpfs is accessed using nautilus under gnome. I.e. double click on a cross volume symlink on ncpfs in nautilus and the machine locks up solid. Here is what is logged to syslog over the network (this is an example, after the second message the machine is locked up solid, I can send you other traces if you want to see more of them):

Bad page state at free_hot_cold_page (in process 'nautilus', page c10d33c0) flags:0x2000000c mapping:cd83f5d0 mapcount:0 count:0

Backtrace:
 [<c01470ec>] bad_page+0x7c/0xb0
 [<c014790c>] free_hot_cold_page+0x7c/0x110
 [<c01482fc>] __pagevec_free+0x1c/0x30
 [<c014d3e3>] release_pages+0x163/0x180
 [<c014d572>] __pagevec_lru_add+0xb2/0xc0
 [<c014d1a5>] lru_add_drain+0x45/0x50
 [<c015640f>] unmap_region+0x2f/0x130
 [<c015653a>] detach_vmas_to_be_unmapped+0x2a/0x60
 [<c0156782>] do_munmap+0x102/0x150
 [<c0156818>] sys_munmap+0x48/0x70
 [<c0104079>] sysenter_past_esp+0x52/0x79
Trying to fix it up, but a reboot is needed
Bad page state at free_hot_cold_page (in process 'nautilus', page c10d33c0)
flags:0x20000004 mapping:00000000 mapcount:1 count:0
Backtrace:
 [<c01470ec>] bad_page+0x7c/0xb0
 [<c014790c>] free_hot_cold_page+0x7c/0x110
 [<c0171659>] link_path_walk+0x929/0xe80
 [<c02b6882>] ip_local_deliver+0x192/0x290
 [<c02b6d94>] ip_rcv+0x414/0x540
 [<c0171e96>] path_lookup+0xa6/0x1c0
 [<c01726f2>] open_namei+0x82/0x680
 [<c011b297>] recalc_task_prio+0xe7/0x200
 [<c0162588>] filp_open+0x28/0x50
 [<c016284a>] get_unused_fd+0x9a/0xc0
 [<c01705e8>] getname+0x68/0xe0
 [<c016295c>] sys_open+0x3c/0xa0
 [<c0104079>] sysenter_past_esp+0x52/0x79

Trying to fix it up, but a reboot is needed

So somehow pages get into a bad state.

Note that using gnome-terminal or console to access the symlink works fine. It is only nautilus and looking at the backtrace it has something to do with mmap.

Note this is not an ncpfs bug AFAICS because ncpfs uses the generic symlink code provided by VFS. From fs/ncpfs/inode.c:

static struct inode_operations ncp_symlink_inode_operations = {

        .readlink       = generic_readlink,
        .follow_link    = page_follow_link_light,
        .put_link       = page_put_link,
        .setattr        = ncp_notify_change,

};

And from fs/ncpfs/symlink.c:

struct address_space_operations ncp_symlink_aops = {
.readpage = ncp_symlink_readpage,
};

And ncp_symlink_readpage() is:

#define NCP_SYMLINK_MAGIC0 cpu_to_le32(0x6c6d7973) /* "symlnk->" */
#define NCP_SYMLINK_MAGIC1 cpu_to_le32(0x3e2d6b6e)

/* ----- read a symbolic link ------------------------------------------ */

static int ncp_symlink_readpage(struct file *file, struct page *page)

{
        struct inode *inode = page->mapping->host;
        int error, length, len;
        char *link, *rawlink;
        char *buf = kmap(page);

        error = -ENOMEM;
        rawlink=(char *)kmalloc(NCP_MAX_SYMLINK_SIZE, GFP_KERNEL);
        if (!rawlink)
                goto fail;

        if (ncp_make_open(inode,O_RDONLY))
                goto failEIO;

        error=ncp_read_kernel(NCP_SERVER(inode),NCP_FINFO(inode)->file_handle,
                         0,NCP_MAX_SYMLINK_SIZE,rawlink,&length);

        ncp_inode_close(inode);
        /* Close file handle if no other users... */
        ncp_make_closed(inode);
        if (error)
                goto failEIO;

        if (NCP_FINFO(inode)->flags & NCPI_KLUDGE_SYMLINK) {
                if (length<NCP_MIN_SYMLINK_SIZE ||
                    ((__le32 *)rawlink)[0]!=NCP_SYMLINK_MAGIC0 ||
                    ((__le32 *)rawlink)[1]!=NCP_SYMLINK_MAGIC1)
                        goto failEIO;
                link = rawlink + 8;
                length -= 8;
        } else {
                link = rawlink;
        }

        len = NCP_MAX_SYMLINK_SIZE;
        error = ncp_vol2io(NCP_SERVER(inode), buf, &len, link, length, 0);
        kfree(rawlink);
        if (error)
                goto fail;
        SetPageUptodate(page);
        kunmap(page);
        unlock_page(page);
        return 0;

failEIO:

        error = -EIO;
        kfree(rawlink);
fail:
        SetPageError(page);
        kunmap(page);
        unlock_page(page);
        return error;
}

Looking at fs/namei.c at the generic VFS symlink helpers ncpfs uses I have no idea what is going wrong. Any ideas?

Note that this problem is fixed by applying the below patch against current 2.6.git tree.

Linus/Andrew, unless Petr objects, I would suggest you apply it so at least ncpfs is fixed.

This is a really serious problem since _any_ user can completely lockup a machine by just creating a cross volume symlink using ncpfs and going across it using nautilus. For us here at Cambridge University this is a big problem since we have home directories on ncpfs (we have local patches to make ncpfs work well enough for that) and on our multiuser public university Linux servers one user can kill the whole server at will...

However this patch will not fix whatever the underlying problem in the vfs/mm is. It just hides it for ncpfs by not using the generic kernel symlink mechanism any more. So it would be good to find and kill the bug.

The patch is tested and does indeed make the hangs and bad page state messages go away.

Best regards,

Anton

Signature · 176 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK
Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net
WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/

[ncpfs] Fix cross volume symlink lockups due to nautilus.

Signed-off-by: Anton Altaparmakov <[email protected]>

diff -urNp linux-2.6.git/fs/ncpfs.old/inode.c linux-2.6.git/fs/ncpfs/inode.c
--- linux-2.6.git/fs/ncpfs.old/inode.c	2005-07-17 07:19:56.000000000 +0100
+++ linux-2.6.git/fs/ncpfs/inode.c	2005-08-19 12:00:49.000000000 +0100
@@ -104,7 +104,8 @@ static struct super_operations ncp_sops 
 
 extern struct dentry_operations ncp_root_dentry_operations;
 #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
-extern struct address_space_operations ncp_symlink_aops;
+extern int ncp_follow_link(struct dentry *, struct nameidata *);
+extern void ncp_put_link(struct dentry *, struct nameidata *);
 extern int ncp_symlink(struct inode*, struct dentry*, const char*);
 #endif
 
@@ -233,8 +234,8 @@ static void ncp_set_attr(struct inode *i
 #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
 static struct inode_operations ncp_symlink_inode_operations = {
 	.readlink	= generic_readlink,
-	.follow_link	= page_follow_link_light,
-	.put_link	= page_put_link,
+	.follow_link	= ncp_follow_link,
+	.put_link	= ncp_put_link,
 	.setattr	= ncp_notify_change,
 };
 #endif
@@ -272,7 +273,6 @@ ncp_iget(struct super_block *sb, struct 
 #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
 		} else if (S_ISLNK(inode->i_mode)) {
 			inode->i_op = &ncp_symlink_inode_operations;
-			inode->i_data.a_ops = &ncp_symlink_aops;
 #endif
 		} else {
 			make_bad_inode(inode);
diff -urNp linux-2.6.git/fs/ncpfs.old/symlink.c linux-2.6.git/fs/ncpfs/symlink.c
--- linux-2.6.git/fs/ncpfs.old/symlink.c	2005-07-17 07:19:56.000000000 +0100
+++ linux-2.6.git/fs/ncpfs/symlink.c	2005-08-19 12:00:49.000000000 +0100
@@ -30,6 +30,7 @@
 #include <linux/time.h>
 #include <linux/mm.h>
 #include <linux/stat.h>
+#include <linux/namei.h>
 #include "ncplib_kernel.h"
 
 
@@ -41,68 +42,76 @@
 
 /* ----- read a symbolic link ------------------------------------------ */
 
-static int ncp_symlink_readpage(struct file *file, struct page *page)
+static inline int ncp_read_link(struct inode *inode, char *link, int link_len)
 {
-	struct inode *inode = page->mapping->host;
-	int error, length, len;
-	char *link, *rawlink;
-	char *buf = kmap(page);
+	char *rawlink, *tmplink;
+	int err, len;
 
-	error = -ENOMEM;
-	rawlink=(char *)kmalloc(NCP_MAX_SYMLINK_SIZE, GFP_KERNEL);
+	rawlink = (char*)kmalloc(NCP_MAX_SYMLINK_SIZE, GFP_KERNEL);
 	if (!rawlink)
-		goto fail;
-
-	if (ncp_make_open(inode,O_RDONLY))
-		goto failEIO;
-
-	error=ncp_read_kernel(NCP_SERVER(inode),NCP_FINFO(inode)->file_handle,
-                         0,NCP_MAX_SYMLINK_SIZE,rawlink,&length);
+		return -ENOMEM;
 
+	if (ncp_make_open(inode, O_RDONLY)) {
+		err = -EIO;
+		goto fail;
+	}
+	err = ncp_read_kernel(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
+                         0, NCP_MAX_SYMLINK_SIZE, rawlink, &len);
 	ncp_inode_close(inode);
 	/* Close file handle if no other users... */
 	ncp_make_closed(inode);
-	if (error)
-		goto failEIO;
-
+	if (err) {
+		err = -EIO;
+		goto fail;
+	}
 	if (NCP_FINFO(inode)->flags & NCPI_KLUDGE_SYMLINK) {
-		if (length<NCP_MIN_SYMLINK_SIZE || 
-		    ((__le32 *)rawlink)[0]!=NCP_SYMLINK_MAGIC0 ||
-		    ((__le32 *)rawlink)[1]!=NCP_SYMLINK_MAGIC1)
-		    	goto failEIO;
-		link = rawlink + 8;
-		length -= 8;
+		if (len < NCP_MIN_SYMLINK_SIZE ||
+				((__le32 *)rawlink)[0] != NCP_SYMLINK_MAGIC0 ||
+				((__le32 *)rawlink)[1] != NCP_SYMLINK_MAGIC1) {
+			err = -EIO;
+			goto fail;
+		}
+		tmplink = rawlink + 8;
+		len -= 8;
 	} else {
-		link = rawlink;
+		tmplink = rawlink;
 	}
-
-	len = NCP_MAX_SYMLINK_SIZE;
-	error = ncp_vol2io(NCP_SERVER(inode), buf, &len, link, length, 0);
+	if (link_len > NCP_MAX_SYMLINK_SIZE)
+		link_len = NCP_MAX_SYMLINK_SIZE;
+	/* Returned string is zero terminated. */
+	err = ncp_vol2io(NCP_SERVER(inode), link, &link_len, tmplink, len, 0);
+	if (!err)
+		err = link_len;
+fail:
 	kfree(rawlink);
-	if (error)
-		goto fail;
-	SetPageUptodate(page);
-	kunmap(page);
-	unlock_page(page);
+	return err;
+}
+
+int ncp_follow_link(struct dentry *dentry, struct nameidata *nd)
+{
+	char *link = __getname();
+
+	if (!link)
+		link = ERR_PTR(-ENOMEM);
+	else {
+		/* Returned string is zero terminated. */
+		int len = ncp_read_link(dentry->d_inode, link, PATH_MAX);
+		if (len < 0) {
+			__putname(link);
+			link = ERR_PTR(len);
+		}
+	}
+	nd_set_link(nd, link);
 	return 0;
+}
 
-failEIO:
-	error = -EIO;
-	kfree(rawlink);
-fail:
-	SetPageError(page);
-	kunmap(page);
-	unlock_page(page);
-	return error;
+void ncp_put_link(struct dentry *dentry, struct nameidata *nd)
+{
+	char *s = nd_get_link(nd);
+	if (!IS_ERR(s))
+		__putname(s);
 }
 
-/*
- * symlinks can't do much...
- */
-struct address_space_operations ncp_symlink_aops = {
-	.readpage	= ncp_symlink_readpage,
-};
-	
 /* ----- create a new symbolic link -------------------------------------- */
  
 int ncp_symlink(struct inode *dir, struct dentry *dentry, const char *symname) {
#1 lore raw
Quoted 18 lines · Anton Altaparmakov

Hi,

There is a bug somewhere in 2.6.11.4 and I can't figure out where it is. I assume it is present in older and newer kernels, too as the related code hasn't changed much AFAICS and googling for "Bad page state" returns rather a lot of hits relating to both older (up to 2.5.70!) and newer kernels...

Note: PLEASE do not stop reading because you read ncpfs below as I am pretty sure it is not ncpfs related! And looking at google a lot of people have reported such similar problems since 2.5.70 or so and they were all told to go away as they have bad ram. That is impossible because this happens on well over 600 workstations and several servers 100% reproducible. Many different types of hardware, different makes, difference age, all running smp kernels even if single cpu. You can't tell me they all have bad ram. Windows works fine and Linux works fine except for that one specific problem which is 100% reproducible...

Quoted 4 lines

The bug only appears, but it appears 100% reproducibly when a cross volume symlink on ncpfs is accessed using nautilus under gnome. I.e. double click on a cross volume symlink on ncpfs in nautilus and the machine locks up solid.

Ugh... Could you at least tell what does nautilus attempt to do at that point? Something that wouldn't show up with simple ls -l <symlink> or cat <symlink> >/dev/null, judging by the above, but what?

#2 lore raw
Quoted 17 lines · Al Viro
Quoted 16 lines · Anton Altaparmakov

There is a bug somewhere in 2.6.11.4 and I can't figure out where it is. I assume it is present in older and newer kernels, too as the related code hasn't changed much AFAICS and googling for "Bad page state" returns rather a lot of hits relating to both older (up to 2.5.70!) and newer kernels...

Note: PLEASE do not stop reading because you read ncpfs below as I am pretty sure it is not ncpfs related! And looking at google a lot of people have reported such similar problems since 2.5.70 or so and they were all told to go away as they have bad ram. That is impossible because this happens on well over 600 workstations and several servers 100% reproducible. Many different types of hardware, different makes, difference age, all running smp kernels even if single cpu. You can't tell me they all have bad ram. Windows works fine and Linux works fine except for that one specific problem which is 100% reproducible...

Quoted 8 lines
Quoted 4 lines

The bug only appears, but it appears 100% reproducibly when a cross volume symlink on ncpfs is accessed using nautilus under gnome. I.e. double click on a cross volume symlink on ncpfs in nautilus and the machine locks up solid.

Ugh... Could you at least tell what does nautilus attempt to do at that point? Something that wouldn't show up with simple ls -l <symlink> or cat <symlink> >/dev/null, judging by the above, but what?

One important thing I forgot to add is that the symlink must point to a directory not a file. If it points to a file all works fine.

I tried stracing nautilus to answer your question. And this time for the first time instead of a Bad page state I got a BUG() triggered in fs/namei.c, the arrow below marks the spot:

void page_put_link(struct dentry *dentry, struct nameidata *nd)

{
	if (!IS_ERR(nd_get_link(nd))) {
		struct page *page;
		page = find_get_page(dentry->d_inode->i_mapping, 0);
		if (!page)
---->			BUG();
		kunmap(page);
		page_cache_release(page);
		page_cache_release(page);
	}
}

Here is the BUG output:

kernel BUG at fs/namei.c:2329!
invalid operand: 0000 [#1]
SMP

Modules linked in: autofs4 nls_cp850 nls_utf8 ncpfs subfs fuse snd

soundcore speedstep_lib freq_table thermal processor fan button battery ac nvram ipt_REJECT iptable_filter ip_tables af_packet edd joydev evdev st video1394 ohci1394 raw1394 ieee1394 capability sg sr_mod dm_mod e1000 uhci_hcd usbcore i2c_i801 i2c_core hw_random parport_pc lp parport reiserfs ide_cd cdrom ide_disk aic7xxx aic79xx via82cxxx ata_piix libata piix ide_core sd_mod scsi_mod

CPU:    3
EIP:    0060:[<c0174e3e>]    Tainted: G     U VLI
EFLAGS: 00010246   (2.6.11.4-21.8-smp)

EIP is at page_put_link+0x3e/0x50
eax: 00000000 ebx: 00000000 ecx: d9c75654 edx: 00000000
esi: 00000000 edi: d9d30000 ebp: ffac2000 esp: d9d31eac
ds: 007b es: 007b ss: 0068
Process nautilus (pid: 21910, threadinfo=d9d30000 task=f5caf550)

Stack: d9c75654 c0171659 00000000 00004000 c2025060 00000001 d9d31f1c
dff3ec80
       d9c75654 001d981f 00000003 d9eb2014 00000000 d9d30000 d9d31f1c
00000000
       d9eb2000 c0171e96 d9eb2000 d9d31f1c 00000001 d9eb2000 c017211f
08542878
Call Trace:
 [<c0171659>] link_path_walk+0x929/0xe80
 [<c0171e96>] path_lookup+0xa6/0x1c0
 [<c017211f>] __user_walk+0x2f/0x70
 [<c016c6fd>] vfs_stat+0x1d/0x60
 [<c016ce5f>] sys_stat64+0xf/0x30
 [<c0109051>] do_syscall_trace+0xb1/0x175
 [<c01040d7>] syscall_call+0x7/0xb
Code: 31 d2 8b 80 a8 00 00 00 e8 80 e6 fc ff 85 c0 89 c3 74 18 89 d8 e8

93 5e fa ff 89 d8 e8 2c 80 fd ff 89 d8 5b e9 24 80 fd ff 5b c3 <0f> 0b 19 09 76 bb 32 c0 eb de 90 8d b4 26 00 00 00 00 83 ec 28

The compressed strace output is attached. It was tracing the main nautilus process (strace -f -F -o /strace.log -p 21910) which is where the bug was hit as well. FWIW I attached strace to the process just before double-clicking on the symlink in the nautilus window...

Looking at the bottom of the strace.log file it seems like two possibly concurrent stat64() calls of the symlink were running, one of which did not complete... (note this was done on a dual-cpu machine with hyperthreading enabled, i.e. it appears to have four cpus).

Does this help?

Let me know if I can provide any other details... (I can provide an ssh connection to a machine for testing purposes if required.)

Best regards,

Anton

Signature · 4 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK
Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net
WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/
#3 lore raw
Quoted 11 lines · Anton Altaparmakov

I tried stracing nautilus to answer your question. And this time for the first time instead of a Bad page state I got a BUG() triggered in fs/namei.c, the arrow below marks the spot:

void page_put_link(struct dentry *dentry, struct nameidata *nd)

{
	if (!IS_ERR(nd_get_link(nd))) {
		struct page *page;
		page = find_get_page(dentry->d_inode->i_mapping, 0);
		if (!page)

----> BUG();

Ergo, something had blown page cache for our inode. Interesting...

#4 lore raw
In reply to Anton Altaparmakov
Quoted 5 lines · Anton Altaparmakov
		struct page *page;
		page = find_get_page(dentry->d_inode->i_mapping, 0);
		if (!page)

----> BUG();

Something has truncated the mapping.

My guess is that you had a cache invalidate event that removed the page from the mapping while it was being used. That might also explain why this only happens for ncpfs. I bet that in the other cases, the mapping was also invalidated, but re-filled immediately, and your strace slowed the other process down enough that it didn't get to re-fill the cache it invalidated or something like that.

The fact that it happens only for cross-volume things might also be explained that way - is there something ncpfs does when switching volumes that might trigger a cache invalidate for another volume (either on the client side or the server side - maybe the server tends to try to break the connection for the old volume when you start traversing a new one?)

The generic "page cache for symlinks" code does _not_ support invalidating the cache while it's being used. A local filesystem will obviously never invalidate the cache at all.

Hmm.. NFS _does_ use the page cache for symlinks, but uses it slightly
differently: instead of relying on the page cache entry being the same when freeing the page, it just caches the page it looked up in the page cache (ie "nfs_follow_link()" does look up the page cache, but then hides the page pointer inside the page data itself (uglee), and thus does not depend on the mapping staying the same (nfs_put_link() just takes the page from the symlink data).

Linus

#5 lore raw
Quoted 7 lines · Linus Torvalds

Hmm.. NFS _does_ use the page cache for symlinks, but uses it slightly
differently: instead of relying on the page cache entry being the same when freeing the page, it just caches the page it looked up in the page cache (ie "nfs_follow_link()" does look up the page cache, but then hides the page pointer inside the page data itself (uglee), and thus does not depend on the mapping staying the same (nfs_put_link() just takes the page from the symlink data).

For NFS that was done exactly to deal with cache invalidation. IIRC, I've convinced myself that it wasn't going to happen on ncpfs and happily abstained from duplicating the NFS variant.

#6 lore raw
In reply to Linus Torvalds
Quoted 6 lines · Linus Torvalds

The generic "page cache for symlinks" code does _not_ support invalidating the cache while it's being used. A local filesystem will obviously never invalidate the cache at all.

Hmm.. NFS _does_ use the page cache for symlinks [..]

Looking more and more at this, I'm convinced this is it.

Basically, page_follow_link_light() and page_put_link() depend on the fact that the page in the page cache is the same one the whole time:
page_follow_link_light() will increment the page count of the page it finds at offset 0, and page_put_link() will decrement it. If the page has changed, they increment/decrement different pages.

There's two ways to fix this:

- document this as a fundamental fact, and apply the ncpfs patch. Local filesystems can still continue to use the generic helper functions (all other users _are_ local filesystems).

- make "nameidata" contain not just the virtual addresses of the names, but also have a "struct page *pages[MAX_NESTED_LINKS + 1]", and save away the page there. That will fix ncpfs, and we could then make NFS also use the generic routines.

I suspect that #1 is the prudent one. We have a patch already, and we don't want to grow nameidata. I'll commit a comment at the head of page_follow_link_light() too.

Linus

#7 lore raw
Quoted 4 lines · Linus Torvalds

- document this as a fundamental fact, and apply the ncpfs patch. Local filesystems can still continue to use the generic helper functions (all other users _are_ local filesystems).

Actually, looking at the ncpfs patch, I'd rather not apply that patch as-is. It looks like it will totally disable symlink caching, which would be kind of sad. Somebody willing to do the same thing NFS does?

NFS hides away the "struct page *" pointer at the end of the page data, so that when we pass the pointer to the virtual address around, we can trivially look up the "struct page".

An alternative is to make the symlink address-space use a gfp_mask of GFP_KERNEL (no highmem), at which point ncpfs_put_link() just becomes something like

	void ncpfs_put_link(struct dentry *dentry, struct nameidata *nd)
	{
		char *addr = nd_get_link(nd);

		if (!IS_ERR(addr))
			page_cache_release(virt_to_page(addr));
	}

which is pretty ugly, but even simpler than the NFS trick.

Anybody?

Linus

#8 lore raw
Quoted 20 lines · Linus Torvalds

Actually, looking at the ncpfs patch, I'd rather not apply that patch as-is. It looks like it will totally disable symlink caching, which would be kind of sad. Somebody willing to do the same thing NFS does?

NFS hides away the "struct page *" pointer at the end of the page data, so that when we pass the pointer to the virtual address around, we can trivially look up the "struct page".

An alternative is to make the symlink address-space use a gfp_mask of GFP_KERNEL (no highmem), at which point ncpfs_put_link() just becomes something like

	void ncpfs_put_link(struct dentry *dentry, struct nameidata *nd)
	{
		char *addr = nd_get_link(nd);

		if (!IS_ERR(addr))
			page_cache_release(virt_to_page(addr));
	}
Quoted 3 lines

which is pretty ugly, but even simpler than the NFS trick.

Anybody?

I'm taking NFS helpers to libfs.c and switching ncpfs to them. IMO that's better than copying the damn thing and other network filesystems might have the same needs eventually...

#9 lore raw
Quoted 3 lines · Al Viro

I'm taking NFS helpers to libfs.c and switching ncpfs to them. IMO that's better than copying the damn thing and other network filesystems might have the same needs eventually...

[something like this - completely untested]

* stray_page_get_link(inode, filler) - returns ERR_PTR(error) or pointer to symlink body. Said symlink body sits in a page at offset equal to offsetof(page, struct stray_page_link). filler() is expected to put it at such offset. Page is cached.

* stray_page_put_link() - ->put_link() suitable for links obtained from stray_page_get_link(). Unlike the usual pagecache-based variants, this sucker does _not_ rely on page staying cached.

* nfs and ncpfs switched to the helpers above.

----

fs/libfs.c +39 -0
diff -urN RC14-rc6-git10-base/fs/libfs.c current/fs/libfs.c
@@ -7,6 +7,7 @@
 #include <linux/pagemap.h>
 #include <linux/mount.h>
 #include <linux/vfs.h>
+#include <linux/namei.h>
 #include <asm/uaccess.h>
 
 int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
@@ -616,6 +617,42 @@
 	return ret;
 }
 
+char *stray_page_get_link(struct inode *inode, int (*filler)(struct inode *, struct page *))
+{
+	struct stray_page_symlink *p;
+	struct page *page;
+
+	page = read_cache_page(&inode->i_data, 0, (filler_t *)filler, inode);
+	if (IS_ERR(page))
+		goto read_failed;
+	if (!PageUptodate(page))
+		goto getlink_read_error;
+	p = kmap(page);
+	p->page = page;
+	return p->body;
+
+getlink_read_error:
+	page_cache_release(page);
+	page = ERR_PTR(-EIO);
+read_failed:
+	return (char *)page;	/* error */
+}
+
+void stray_page_put_link(struct dentry *dentry, struct nameidata *nd)
+{
+	char *s = nd_get_link(nd);
+	if (!IS_ERR(s)) {
+		struct stray_page_symlink *p;
+		struct page *page;
+
+		p = container_of(s, struct stray_page_symlink, body[0]);
+		page = p->page;
+
+		kunmap(page);
+		page_cache_release(page);
+	}
+}
+
 EXPORT_SYMBOL(dcache_dir_close);
 EXPORT_SYMBOL(dcache_dir_lseek);
 EXPORT_SYMBOL(dcache_dir_open);
@@ -648,3 +685,5 @@
 EXPORT_SYMBOL_GPL(simple_attr_close);
 EXPORT_SYMBOL_GPL(simple_attr_read);
 EXPORT_SYMBOL_GPL(simple_attr_write);
+EXPORT_SYMBOL(stray_page_get_link);
+EXPORT_SYMBOL(stray_page_put_link);
fs/ncpfs/inode.c +3 -4
diff -urN RC13-rc6-git10-base/fs/ncpfs/inode.c current/fs/ncpfs/inode.c
@@ -104,7 +104,7 @@
 
 extern struct dentry_operations ncp_root_dentry_operations;
 #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
-extern struct address_space_operations ncp_symlink_aops;
+extern int ncp_follow_link(struct dentry *dentry, struct nameidata *nd);
 extern int ncp_symlink(struct inode*, struct dentry*, const char*);
 #endif
 
@@ -233,8 +233,8 @@
 #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
 static struct inode_operations ncp_symlink_inode_operations = {
 	.readlink	= generic_readlink,
-	.follow_link	= page_follow_link_light,
-	.put_link	= page_put_link,
+	.follow_link	= ncp_follow_link,
+	.put_link	= stray_page_put_link,
 	.setattr	= ncp_notify_change,
 };
 #endif
@@ -272,7 +272,6 @@
 #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS)
 		} else if (S_ISLNK(inode->i_mode)) {
 			inode->i_op = &ncp_symlink_inode_operations;
-			inode->i_data.a_ops = &ncp_symlink_aops;
 #endif
 		} else {
 			make_bad_inode(inode);
fs/ncpfs/symlink.c +9 -9
diff -urN RC13-rc6-git10-base/fs/ncpfs/symlink.c current/fs/ncpfs/symlink.c
@@ -30,6 +30,7 @@
 #include <linux/time.h>
 #include <linux/mm.h>
 #include <linux/stat.h>
+#include <linux/namei.h>
 #include "ncplib_kernel.h"
 
 
@@ -41,9 +42,8 @@
 
 /* ----- read a symbolic link ------------------------------------------ */
 
-static int ncp_symlink_readpage(struct file *file, struct page *page)
+static int symlink_filler(struct inode *inode, struct page *page)
 {
-	struct inode *inode = page->mapping->host;
 	int error, length, len;
 	char *link, *rawlink;
 	char *buf = kmap(page);
@@ -77,6 +77,7 @@
 	}
 
 	len = NCP_MAX_SYMLINK_SIZE;
+	buf += offsetof(struct stray_page_symlink, body);
 	error = ncp_vol2io(NCP_SERVER(inode), buf, &len, link, length, 0);
 	kfree(rawlink);
 	if (error)
@@ -96,13 +97,12 @@
 	return error;
 }
 
-/*
- * symlinks can't do much...
- */
-struct address_space_operations ncp_symlink_aops = {
-	.readpage	= ncp_symlink_readpage,
-};
-	
+int ncp_follow_link(struct dentry *dentry, struct nameidata *nd)
+{
+	nd_set_link(nd, stray_page_get_link(dentry->d_inode, symlink_filler));
+	return 0;
+}
+
 /* ----- create a new symbolic link -------------------------------------- */
  
 int ncp_symlink(struct inode *dir, struct dentry *dentry, const char *symname) {
fs/nfs/symlink.c +6 -41

66 lines not on this page · raw message

include/linux/fs.h +7 -0
diff -urN RC13-rc6-git10-base/include/linux/fs.h current/include/linux/fs.h
@@ -1614,6 +1614,13 @@
 loff_t inode_get_bytes(struct inode *inode);
 void inode_set_bytes(struct inode *inode, loff_t bytes);
 
+struct stray_page_symlink {
+	struct page *page;
+	char body[0];
+};
+char *stray_page_get_link(struct inode *inode, int (*filler)(struct inode *, struct page *));
+void stray_page_put_link(struct dentry *dentry, struct nameidata *nd);
+
 extern int vfs_readdir(struct file *, filldir_t, void *);
 
 extern int vfs_stat(char __user *, struct kstat *);
#10 lore raw
Quoted 12 lines · Al Viro
Quoted 3 lines · Al Viro

I'm taking NFS helpers to libfs.c and switching ncpfs to them. IMO that's better than copying the damn thing and other network filesystems might have the same needs eventually...

[something like this - completely untested]

* stray_page_get_link(inode, filler) - returns ERR_PTR(error) or pointer to symlink body. Said symlink body sits in a page at offset equal to offsetof(page, struct stray_page_link). filler() is expected to put it at such offset. Page is cached.

Quoted 5 lines

* stray_page_put_link() - ->put_link() suitable for links obtained from stray_page_get_link(). Unlike the usual pagecache-based variants, this sucker does _not_ rely on page staying cached.

* nfs and ncpfs switched to the helpers above.

Can you add some kerneldoc comments to describe them? Especially as the name is not very descriptive.

#11 lore raw
Quoted 13 lines · Christoph Hellwig
Quoted 12 lines · Al Viro
Quoted 3 lines · Al Viro

I'm taking NFS helpers to libfs.c and switching ncpfs to them. IMO that's better than copying the damn thing and other network filesystems might have the same needs eventually...

[something like this - completely untested]

* stray_page_get_link(inode, filler) - returns ERR_PTR(error) or pointer to symlink body. Said symlink body sits in a page at offset equal to offsetof(page, struct stray_page_link). filler() is expected to put it at such offset. Page is cached.

Quoted 8 lines
Quoted 5 lines

* stray_page_put_link() - ->put_link() suitable for links obtained from stray_page_get_link(). Unlike the usual pagecache-based variants, this sucker does _not_ rely on page staying cached.

* nfs and ncpfs switched to the helpers above.

Can you add some kerneldoc comments to describe them? Especially as the name is not very descriptive.

Hey, if anybody has suggestions on names - they are very welcome ;-)

FWIW, I'd rather take page_symlink(), page_symlink_inode_operations, page_put_link(), page_follow_link_light(), page_readlink(), page_getlink(), generic_readlink() and vfs_readlink() to the same place where these guys would live. They all belong together and none of them has any business in fs/namei.c. Options: fs/libfs.c or separate library since fs/libfs.c is getting crowded. Linus, do you have any objections to that or suggestions on filename here?

#12 lore raw
Quoted 7 lines · Al Viro

FWIW, I'd rather take page_symlink(), page_symlink_inode_operations, page_put_link(), page_follow_link_light(), page_readlink(), page_getlink(), generic_readlink() and vfs_readlink() to the same place where these guys would live. They all belong together and none of them has any business in fs/namei.c. Options: fs/libfs.c or separate library since fs/libfs.c is getting crowded. Linus, do you have any objections to that or suggestions on filename here?

fs/symlink.c?

Signature · 6 lines
"Next the statesmen will invent cheap lies, putting the blame upon 
the nation that is attacked, and every man will be glad of those
conscience-soothing falsities, and will diligently study them, and refuse
to examine any refutations of them; and thus he will by and by convince 
himself that the war is just, and will thank God for the better sleep 
he enjoys after this process of grotesque self-deception." -- Mark Twain
#13 lore raw
Quoted 4 lines · Matthew Wilcox
Quoted 2 lines

is getting crowded. Linus, do you have any objections to that or suggestions on filename here?

fs/symlink.c?

Or fs/lib/symlink.c...

#14 lore raw
Quoted 7 lines · Al Viro
Quoted 4 lines · Matthew Wilcox
Quoted 2 lines

is getting crowded. Linus, do you have any objections to that or suggestions on filename here?

fs/symlink.c?

Or fs/lib/symlink.c...

That's a very good idea. We were in need of fs/lib/ for a long time to unwind all the generic routines of the VFS logic.

#15 lore raw
In reply to Al Viro
Quoted 8 lines · Al Viro

FWIW, I'd rather take page_symlink(), page_symlink_inode_operations, page_put_link(), page_follow_link_light(), page_readlink(), page_getlink(), generic_readlink() and vfs_readlink() to the same place where these guys would live. They all belong together and none of them has any business in fs/namei.c. Options: fs/libfs.c or separate library since fs/libfs.c is getting crowded. Linus, do you have any objections to that or suggestions on filename here?

I'm not sure if this merits a new file or new organization (hey, fs/lib/xxx might be good in theory). In particular, I had actually been hoping to release 2.6.13 today, but this seems like a valid thing to hold things up for - but not if we're going to re-organize things.

The one thing that strikes me is that we might actually have less pain if we just changed the calling convention for follow_link/put_link slightly instead of creating a new library function. The existing "page cache" thing really _does_ work very well, and would work fine for NFS and ncpfs too, if we just allowed an extra cookie to be passed along from "follow_link()" to "put_link()".

A patch like this (totally untested, and you'd need to update any filesystems that don't use the regular page_follow_link interface) would seem to clean up the mess nicely.. The basic change is that follow_link() returns a error-pointer cookie instead of just zero or error, and that is passed into put_link().

That simple calling convention change solves all problems. It so _happens_ that any old binary code also continues to work (the cookie will be zero, and put_link will ignore it), so it shouldn't even break any unconverted stuff (unless they mix using their own functions _and_ using the helpher functions, which is of course possible).

The "shouldn't break nonconverted filesystems" makes me think this is a safe change. Comments?

NOTE NOTE NOTE! Let me say again that it's untested. It might not break nonconverted filesystems, but it equally well migth break even the converted ones ;)

Linus

----

fs/namei.c +17 -15
@@ -501,6 +501,7 @@ struct path {
 static inline int __do_follow_link(struct path *path, struct nameidata *nd)
 {
 	int error;
+	void *cookie;
 	struct dentry *dentry = path->dentry;
 
 	touch_atime(path->mnt, dentry);
@@ -508,13 +509,15 @@ static inline int __do_follow_link(struc
 
 	if (path->mnt == nd->mnt)
 		mntget(path->mnt);
-	error = dentry->d_inode->i_op->follow_link(dentry, nd);
-	if (!error) {
+	cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
+	error = PTR_ERR(cookie);
+	if (!IS_ERR(cookie)) {
 		char *s = nd_get_link(nd);
+		error = 0;
 		if (s)
 			error = __vfs_follow_link(nd, s);
 		if (dentry->d_inode->i_op->put_link)
-			dentry->d_inode->i_op->put_link(dentry, nd);
+			dentry->d_inode->i_op->put_link(dentry, nd, cookie);
 	}
 	dput(dentry);
 	mntput(path->mnt);
@@ -2345,14 +2348,17 @@ int generic_readlink(struct dentry *dent
 {
 	struct nameidata nd;
 	int res;
+	void *cookie;
+
 	nd.depth = 0;
-	res = dentry->d_inode->i_op->follow_link(dentry, &nd);
-	if (!res) {
+	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
+	if (!IS_ERR(cookie)) {
 		res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
 		if (dentry->d_inode->i_op->put_link)
-			dentry->d_inode->i_op->put_link(dentry, &nd);
+			dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
+		cookie = ERR_PTR(0);
 	}
-	return res;
+	return PTR_ERR(cookie);
 }
 
 int vfs_follow_link(struct nameidata *nd, const char *link)
@@ -2395,23 +2401,19 @@ int page_readlink(struct dentry *dentry,
 	return res;
 }
 
-int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
+void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
 {
 	struct page *page;
 	nd_set_link(nd, page_getlink(dentry, &page));
-	return 0;
+	return page;
 }
 
-void page_put_link(struct dentry *dentry, struct nameidata *nd)
+void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
 {
 	if (!IS_ERR(nd_get_link(nd))) {
-		struct page *page;
-		page = find_get_page(dentry->d_inode->i_mapping, 0);
-		if (!page)
-			BUG();
+		struct page *page = cookie;
 		kunmap(page);
 		page_cache_release(page);
-		page_cache_release(page);
 	}
 }
 
include/linux/fs.h +4 -4
@@ -993,8 +993,8 @@ struct inode_operations {
 	int (*rename) (struct inode *, struct dentry *,
 			struct inode *, struct dentry *);
 	int (*readlink) (struct dentry *, char __user *,int);
-	int (*follow_link) (struct dentry *, struct nameidata *);
-	void (*put_link) (struct dentry *, struct nameidata *);
+	void * (*follow_link) (struct dentry *, struct nameidata *);
+	void (*put_link) (struct dentry *, struct nameidata *, void *);
 	void (*truncate) (struct inode *);
 	int (*permission) (struct inode *, int, struct nameidata *);
 	int (*setattr) (struct dentry *, struct iattr *);
@@ -1602,8 +1602,8 @@ extern struct file_operations generic_ro
 extern int vfs_readlink(struct dentry *, char __user *, int, const char *);
 extern int vfs_follow_link(struct nameidata *, const char *);
 extern int page_readlink(struct dentry *, char __user *, int);
-extern int page_follow_link_light(struct dentry *, struct nameidata *);
-extern void page_put_link(struct dentry *, struct nameidata *);
+extern void *page_follow_link_light(struct dentry *, struct nameidata *);
+extern void page_put_link(struct dentry *, struct nameidata *, void *);
 extern int page_symlink(struct inode *inode, const char *symname, int len);
 extern struct inode_operations page_symlink_inode_operations;
 extern int generic_readlink(struct dentry *, char __user *, int);
#16 lore raw
Quoted 65 lines · Linus Torvalds

The one thing that strikes me is that we might actually have less pain if we just changed the calling convention for follow_link/put_link slightly instead of creating a new library function. The existing "page cache" thing really _does_ work very well, and would work fine for NFS and ncpfs too, if we just allowed an extra cookie to be passed along from "follow_link()" to "put_link()".

A patch like this (totally untested, and you'd need to update any filesystems that don't use the regular page_follow_link interface) would seem to clean up the mess nicely.. The basic change is that follow_link() returns a error-pointer cookie instead of just zero or error, and that is passed into put_link().

That simple calling convention change solves all problems. It so _happens_ that any old binary code also continues to work (the cookie will be zero, and put_link will ignore it), so it shouldn't even break any unconverted stuff (unless they mix using their own functions _and_ using the helpher functions, which is of course possible).

The "shouldn't break nonconverted filesystems" makes me think this is a safe change. Comments?

NOTE NOTE NOTE! Let me say again that it's untested. It might not break nonconverted filesystems, but it equally well migth break even the converted ones ;)

Linus

----

fs/namei.c +8 -3
@@ -501,6 +501,7 @@ struct path {
 static inline int __do_follow_link(struct path *path, struct nameidata *nd)
 {
 	int error;
+	void *cookie;
 	struct dentry *dentry = path->dentry;
 
 	touch_atime(path->mnt, dentry);
@@ -508,13 +509,15 @@ static inline int __do_follow_link(struc
 
 	if (path->mnt == nd->mnt)
 		mntget(path->mnt);
-	error = dentry->d_inode->i_op->follow_link(dentry, nd);
-	if (!error) {
+	cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
+	error = PTR_ERR(cookie);
+	if (!IS_ERR(cookie)) {
 		char *s = nd_get_link(nd);
+		error = 0;
 		if (s)
 			error = __vfs_follow_link(nd, s);
 		if (dentry->d_inode->i_op->put_link)
-			dentry->d_inode->i_op->put_link(dentry, nd);
+			dentry->d_inode->i_op->put_link(dentry, nd, cookie);
 	}
 	dput(dentry);
 	mntput(path->mnt);
@@ -2345,14 +2348,17 @@ int generic_readlink(struct dentry *dent
 {
 	struct nameidata nd;
 	int res;
+	void *cookie;
+

fs/namei.c int generic_readlink(struct dentry *dent · line 2359

Quoted 10 lines
fs/namei.c +4 -3
 	nd.depth = 0;
-	res = dentry->d_inode->i_op->follow_link(dentry, &nd);
-	if (!res) {
+	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
+	if (!IS_ERR(cookie)) {
 		res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
 		if (dentry->d_inode->i_op->put_link)
-			dentry->d_inode->i_op->put_link(dentry, &nd);
+			dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
+		cookie = ERR_PTR(0);

You are throwing away the error return from vfs_readlink(). I suspect you wanted:

+ cookie = ERR_PTR(res);

Quoted 49 lines
fs/namei.c +5 -9
 	}
-	return res;
+	return PTR_ERR(cookie);
 }
 
 int vfs_follow_link(struct nameidata *nd, const char *link)
@@ -2395,23 +2401,19 @@ int page_readlink(struct dentry *dentry,
 	return res;
 }
 
-int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
+void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
 {
 	struct page *page;
 	nd_set_link(nd, page_getlink(dentry, &page));
-	return 0;
+	return page;
 }
 
-void page_put_link(struct dentry *dentry, struct nameidata *nd)
+void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
 {
 	if (!IS_ERR(nd_get_link(nd))) {
-		struct page *page;
-		page = find_get_page(dentry->d_inode->i_mapping, 0);
-		if (!page)
-			BUG();
+		struct page *page = cookie;
 		kunmap(page);
 		page_cache_release(page);
-		page_cache_release(page);
 	}
 }
 
include/linux/fs.h +2 -2
@@ -993,8 +993,8 @@ struct inode_operations {
 	int (*rename) (struct inode *, struct dentry *,
 			struct inode *, struct dentry *);
 	int (*readlink) (struct dentry *, char __user *,int);
-	int (*follow_link) (struct dentry *, struct nameidata *);
-	void (*put_link) (struct dentry *, struct nameidata *);
+	void * (*follow_link) (struct dentry *, struct nameidata *);
+	void (*put_link) (struct dentry *, struct nameidata *, void *);
 	void (*truncate) (struct inode *);
 	int (*permission) (struct inode *, int, struct nameidata *);
 	int (*setattr) (struct dentry *, struct iattr *);

include/linux/fs.h extern struct file_operations generic_ro · line 1609

Quoted 10 lines
include/linux/fs.h +2 -2
@@ -1602,8 +1602,8 @@ extern struct file_operations generic_ro
 extern int vfs_readlink(struct dentry *, char __user *, int, const char *);
 extern int vfs_follow_link(struct nameidata *, const char *);
 extern int page_readlink(struct dentry *, char __user *, int);
-extern int page_follow_link_light(struct dentry *, struct nameidata *);
-extern void page_put_link(struct dentry *, struct nameidata *);
+extern void *page_follow_link_light(struct dentry *, struct nameidata *);
+extern void page_put_link(struct dentry *, struct nameidata *, void *);
 extern int page_symlink(struct inode *inode, const char *symname, int len);
 extern struct inode_operations page_symlink_inode_operations;
 extern int generic_readlink(struct dentry *, char __user *, int);

Best regards,

Anton

Signature · 4 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK
Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net
WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/
#17 lore raw

Review

Quoted 5 lines · Anton Altaparmakov

You are throwing away the error return from vfs_readlink(). I suspect you wanted:

(quoted diff) +1 -0
+		cookie = ERR_PTR(res);

Yes, thanks.

Linus

#18 lore raw
In reply to Linus Torvalds
Quoted 11 lines · Linus Torvalds

I'm not sure if this merits a new file or new organization (hey, fs/lib/xxx might be good in theory). In particular, I had actually been hoping to release 2.6.13 today, but this seems like a valid thing to hold things up for - but not if we're going to re-organize things.

The one thing that strikes me is that we might actually have less pain if we just changed the calling convention for follow_link/put_link slightly instead of creating a new library function. The existing "page cache" thing really _does_ work very well, and would work fine for NFS and ncpfs too, if we just allowed an extra cookie to be passed along from "follow_link()" to "put_link()".

Er... We can do that, but current calling conventions for ->follow_link() are already too complex (and we had bugs in that area). What we have is
1) you can return -error; then you must release nameidata yourself
and ->put_link() will _not_ be called.
2) you can do nd_set_link(nd, ERR_PTR(-error)) and return 0
3) you can do nd_set_link(nd, path) and return 0
4) you can return 0 (after having moved nameidata yourself)
Note that in practice (2) is far more convenient than (1). So pretty much everything does it for errors and (1) turns out to be a source of bugs - people forget to drop nameidata. The only real case for (1) is failed
(4) - when we went too far and already dropped nameidata naturally.

Let me take a look at the current situation...
* a bunch of filesystems do only (2) and (3). That's the normal
case.
* jffs2 follow_link() is broken - it has an exit where it returns
-EIO and leaks nameidata.
* procfs special symlinks - (1) or (4), correctly used.
* afs magic - (1) or (4), correctly used.
* hppfs - badly broken for unrelated reasons.

Adding "you can return a cookie as long as it's not ERR_PTR() and it will be treated like (2), (3) or (4), except that you won't want it in (4)" will add to confusion. And of course, we will have to update every ->put_link() and ->follow_link() out there...

_If_ you want 2.6.13 with minimal PITA right now, I'd suggest leaving API as is until 2.6.13-git1 and using stray_page_... + patch below to deal with jffs2 until then. Then we can deal with API change - immediately after 2.6.13 release. Removing stray_... in process.

fs/jffs2/symlink.c +6 -4
diff -urN RC13-rc6-git10-base/fs/jffs2/symlink.c current/fs/jffs2/symlink.c
@@ -30,6 +30,7 @@
 static int jffs2_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
+	char *p = (char *)f->dents;
 	
 	/*
 	 * We don't acquire the f->sem mutex here since the only data we
@@ -45,13 +46,14 @@
 	 * nd_set_link() call.
 	 */
 	
-	if (!f->dents) {
+	if (!p) {
 		printk(KERN_ERR "jffs2_follow_link(): can't find symlink taerget\n");
-		return -EIO;
+		p = ERR_PTR(-EIO);
+	} else {
+		D1(printk(KERN_DEBUG "jffs2_follow_link(): target path is '%s'\n", (char *) f->dents));
 	}
-	D1(printk(KERN_DEBUG "jffs2_follow_link(): target path is '%s'\n", (char *) f->dents));
 
-	nd_set_link(nd, (char *)f->dents);
+	nd_set_link(nd, p);
 	
 	/*
 	 * We unlock the f->sem mutex but VFS will use the f->dents string. This is safe
#19 lore raw
In reply to Al Viro
Quoted 16 lines · Al Viro
Quoted 5 lines · Al Viro

I'm taking NFS helpers to libfs.c and switching ncpfs to them. IMO that's better than copying the damn thing and other network filesystems might have the same needs eventually...

[something like this - completely untested]

* stray_page_get_link(inode, filler) - returns ERR_PTR(error) or pointer to symlink body. Said symlink body sits in a page at offset equal to offsetof(page, struct stray_page_link). filler() is expected to put it at such offset. Page is cached.

Quoted 10 lines

* stray_page_put_link() - ->put_link() suitable for links obtained from stray_page_get_link(). Unlike the usual pagecache-based variants, this sucker does _not_ rely on page staying cached.

* nfs and ncpfs switched to the helpers above.

----

Just out of curiosity - what protects even local filesystems against concurrent truncate and symlink resolving when using the page cache helpers?

--Mika

#20 lore raw
Quoted 2 lines · Mika Penttilä

Just out of curiosity - what protects even local filesystems against concurrent truncate and symlink resolving when using the page cache helpers?

How do you get truncate(2) or ftruncate(2) to do something with a symlink? The former follows links, the latter takes an open file...
-
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

#21 lore raw
Quoted 7 lines · Al Viro
Quoted 3 lines · Mika Penttilä

Just out of curiosity - what protects even local filesystems against concurrent truncate and symlink resolving when using the page cache helpers?

Quoted 10 lines

How do you get truncate(2) or ftruncate(2) to do something with a symlink? The former follows links, the latter takes an open file...
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Yes that is right, there is no way to invalidate the symlink inode mapping page(s) from user space.

--Mika

-
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

#22 lore raw
In reply to Linus Torvalds
Quoted 9 lines · Linus Torvalds
Quoted 4 lines · Linus Torvalds

- document this as a fundamental fact, and apply the ncpfs patch. Local filesystems can still continue to use the generic helper functions (all other users _are_ local filesystems).

Actually, looking at the ncpfs patch, I'd rather not apply that patch as-is. It looks like it will totally disable symlink caching, which would be kind of sad. Somebody willing to do the same thing NFS does?

It does disable link caching. But I didn't make this up. This is exactly what smbfs uses. I just copied smbfs given ncpfs copies almost everything smbfs does anyway...

Best regards,

Anton

Signature · 4 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK
Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net
WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/
#23 lore raw
Quoted 4 lines · Anton Altaparmakov

It does disable link caching. But I didn't make this up. This is exactly what smbfs uses. I just copied smbfs given ncpfs copies almost everything smbfs does anyway...

Can you test whether the untested test-patch I sent out seems to work for your case that bugs out? You'll probably get a few new "initialization from incompatible pointer type" warnings, but I do believe that they should be harmless at least on normal architectures.

Linus

#24 lore raw
Quoted 9 lines · Linus Torvalds
Quoted 3 lines · Anton Altaparmakov

It does disable link caching. But I didn't make this up. This is exactly what smbfs uses. I just copied smbfs given ncpfs copies almost everything smbfs does anyway...

Can you test whether the untested test-patch I sent out seems to work for your case that bugs out? You'll probably get a few new "initialization from incompatible pointer type" warnings, but I do believe that they should be harmless at least on normal architectures.

Yes, sure. I have applied your patch to our 2.6.11.4 tree (with the one liner change I emailed you just now) and have kicked off a compile. I am afraid the actual testing will have to wait for tomorrow as I was actually on the way to bed and the make modules is taking forever or at least longer than I can keep my eyes open...

Best regards,

Anton

Signature · 4 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK
Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net
WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/
#25 lore raw
Quoted 3 lines · Anton Altaparmakov

Yes, sure. I have applied your patch to our 2.6.11.4 tree (with the one liner change I emailed you just now) and have kicked off a compile.

Actually, hold on. The original patch had another problem: it returned an uninitialized "page" pointer when page_getlink() failed.

This one should have that fixed, and has converted a few other filesystems. Most of them trivially, but I took the opportunity to just simplify NFS while I was at it, since it now has no reason to need to save off the "struct page *" any more.

It's still not tested, but at least I've looked at it a bit more ;)

Linus

---

fs/autofs/symlink.c +3 -2
@@ -12,11 +12,12 @@
 
 #include "autofs_i.h"
 
-static int autofs_follow_link(struct dentry *dentry, struct nameidata *nd)
+/* Nothing to release.. */
+static void *autofs_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	char *s=((struct autofs_symlink *)dentry->d_inode->u.generic_ip)->data;
 	nd_set_link(nd, s);
-	return 0;
+	return NULL;
 }
 
 struct inode_operations autofs_symlink_inode_operations = {
fs/cifs/cifsfs.h +2 -2
@@ -83,8 +83,8 @@ extern int cifs_dir_notify(struct file *
 extern struct dentry_operations cifs_dentry_ops;
 
 /* Functions related to symlinks */
-extern int cifs_follow_link(struct dentry *direntry, struct nameidata *nd);
-extern void cifs_put_link(struct dentry *direntry, struct nameidata *nd);
+extern void *cifs_follow_link(struct dentry *direntry, struct nameidata *nd);
+extern void cifs_put_link(struct dentry *direntry, struct nameidata *nd, void *);
 extern int cifs_readlink(struct dentry *direntry, char __user *buffer, 
 			 int buflen);
 extern int cifs_symlink(struct inode *inode, struct dentry *direntry,
fs/cifs/link.c +3 -3
@@ -92,7 +92,7 @@ cifs_hl_exit:
 	return rc;
 }
 
-int
+void *
 cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
 {
 	struct inode *inode = direntry->d_inode;
@@ -148,7 +148,7 @@ out:
 out_no_free:
 	FreeXid(xid);
 	nd_set_link(nd, target_path);
-	return 0;
+	return NULL;	/* No cookie */
 }
 
 int
@@ -330,7 +330,7 @@ cifs_readlink(struct dentry *direntry, c
 	return rc;
 }
 
-void cifs_put_link(struct dentry *direntry, struct nameidata *nd)
+void cifs_put_link(struct dentry *direntry, struct nameidata *nd, void *cookie)
 {
 	char *p = nd_get_link(nd);
 	if (!IS_ERR(p))
fs/ext2/symlink.c +2 -2
@@ -21,11 +21,11 @@
 #include "xattr.h"
 #include <linux/namei.h>
 
-static int ext2_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *ext2_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct ext2_inode_info *ei = EXT2_I(dentry->d_inode);
 	nd_set_link(nd, (char *)ei->i_data);
-	return 0;
+	return NULL;
 }
 
 struct inode_operations ext2_symlink_inode_operations = {
fs/ext3/symlink.c +2 -2
@@ -23,11 +23,11 @@
 #include <linux/namei.h>
 #include "xattr.h"
 
-static int ext3_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void * ext3_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct ext3_inode_info *ei = EXT3_I(dentry->d_inode);
 	nd_set_link(nd, (char*)ei->i_data);
-	return 0;
+	return NULL;
 }
 
 struct inode_operations ext3_symlink_inode_operations = {
fs/namei.c +21 -19
@@ -501,6 +501,7 @@ struct path {
 static inline int __do_follow_link(struct path *path, struct nameidata *nd)
 {
 	int error;
+	void *cookie;
 	struct dentry *dentry = path->dentry;
 
 	touch_atime(path->mnt, dentry);
@@ -508,13 +509,15 @@ static inline int __do_follow_link(struc
 
 	if (path->mnt == nd->mnt)
 		mntget(path->mnt);
-	error = dentry->d_inode->i_op->follow_link(dentry, nd);
-	if (!error) {
+	cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
+	error = PTR_ERR(cookie);
+	if (!IS_ERR(cookie)) {
 		char *s = nd_get_link(nd);
+		error = 0;
 		if (s)
 			error = __vfs_follow_link(nd, s);
 		if (dentry->d_inode->i_op->put_link)
-			dentry->d_inode->i_op->put_link(dentry, nd);
+			dentry->d_inode->i_op->put_link(dentry, nd, cookie);
 	}
 	dput(dentry);
 	mntput(path->mnt);
@@ -2344,15 +2347,17 @@ out:
 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
 {
 	struct nameidata nd;
-	int res;
+	void *cookie;
+
 	nd.depth = 0;
-	res = dentry->d_inode->i_op->follow_link(dentry, &nd);
-	if (!res) {
-		res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
+	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
+	if (!IS_ERR(cookie)) {
+		int res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
 		if (dentry->d_inode->i_op->put_link)
-			dentry->d_inode->i_op->put_link(dentry, &nd);
+			dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
+		cookie = ERR_PTR(res);
 	}
-	return res;
+	return PTR_ERR(cookie);
 }
 
 int vfs_follow_link(struct nameidata *nd, const char *link)
@@ -2395,23 +2400,20 @@ int page_readlink(struct dentry *dentry,
 	return res;
 }
 
-int page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
+void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
 {
-	struct page *page;
+	struct page *page = NULL;
 	nd_set_link(nd, page_getlink(dentry, &page));
-	return 0;
+	return page;
 }
 
-void page_put_link(struct dentry *dentry, struct nameidata *nd)
+void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
 {
-	if (!IS_ERR(nd_get_link(nd))) {
-		struct page *page;
-		page = find_get_page(dentry->d_inode->i_mapping, 0);
-		if (!page)
-			BUG();
+	struct page *page = cookie;
+
+	if (page) {
 		kunmap(page);
 		page_cache_release(page);
-		page_cache_release(page);
 	}
 }
 
fs/nfs/symlink.c +8 -29
@@ -27,26 +27,14 @@
 
 /* Symlink caching in the page cache is even more simplistic
  * and straight-forward than readdir caching.
- *
- * At the beginning of the page we store pointer to struct page in question,
- * simplifying nfs_put_link() (if inode got invalidated we can't find the page
- * to be freed via pagecache lookup).
- * The NUL-terminated string follows immediately thereafter.
  */
 
-struct nfs_symlink {
-	struct page *page;
-	char body[0];
-};
-
 static int nfs_symlink_filler(struct inode *inode, struct page *page)
 {
-	const unsigned int pgbase = offsetof(struct nfs_symlink, body);
-	const unsigned int pglen = PAGE_SIZE - pgbase;
 	int error;
 
 	lock_kernel();
-	error = NFS_PROTO(inode)->readlink(inode, page, pgbase, pglen);
+	error = NFS_PROTO(inode)->readlink(inode, page, 0, PAGE_SIZE);
 	unlock_kernel();
 	if (error < 0)
 		goto error;
@@ -60,11 +48,10 @@ error:
 	return -EIO;
 }
 
-static int nfs_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *nfs_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct inode *inode = dentry->d_inode;
 	struct page *page;
-	struct nfs_symlink *p;
 	void *err = ERR_PTR(nfs_revalidate_inode(NFS_SERVER(inode), inode));
 	if (err)
 		goto read_failed;
@@ -78,28 +65,20 @@ static int nfs_follow_link(struct dentry
 		err = ERR_PTR(-EIO);
 		goto getlink_read_error;
 	}
-	p = kmap(page);
-	p->page = page;
-	nd_set_link(nd, p->body);
-	return 0;
+	nd_set_link(nd, kmap(page));
+	return page;
 
 getlink_read_error:
 	page_cache_release(page);
 read_failed:
 	nd_set_link(nd, err);
-	return 0;
+	return NULL;
 }
 
-static void nfs_put_link(struct dentry *dentry, struct nameidata *nd)
+static void nfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
 {
-	char *s = nd_get_link(nd);
-	if (!IS_ERR(s)) {
-		struct nfs_symlink *p;
-		struct page *page;
-
-		p = container_of(s, struct nfs_symlink, body[0]);
-		page = p->page;
-
+	if (cookie) {
+		struct page *page = cookie;
 		kunmap(page);
 		page_cache_release(page);
 	}
fs/sysfs/symlink.c +3 -3
@@ -151,17 +151,17 @@ static int sysfs_getlink(struct dentry *
 
 }
 
-static int sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	int error = -ENOMEM;
 	unsigned long page = get_zeroed_page(GFP_KERNEL);
 	if (page)
 		error = sysfs_getlink(dentry, (char *) page); 
 	nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
-	return 0;
+	return NULL;
 }
 
-static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd)
+static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
 {
 	char *page = nd_get_link(nd);
 	if (!IS_ERR(page))
include/linux/fs.h +4 -4
@@ -993,8 +993,8 @@ struct inode_operations {
 	int (*rename) (struct inode *, struct dentry *,
 			struct inode *, struct dentry *);
 	int (*readlink) (struct dentry *, char __user *,int);
-	int (*follow_link) (struct dentry *, struct nameidata *);
-	void (*put_link) (struct dentry *, struct nameidata *);
+	void * (*follow_link) (struct dentry *, struct nameidata *);
+	void (*put_link) (struct dentry *, struct nameidata *, void *);
 	void (*truncate) (struct inode *);
 	int (*permission) (struct inode *, int, struct nameidata *);
 	int (*setattr) (struct dentry *, struct iattr *);
@@ -1602,8 +1602,8 @@ extern struct file_operations generic_ro
 extern int vfs_readlink(struct dentry *, char __user *, int, const char *);
 extern int vfs_follow_link(struct nameidata *, const char *);
 extern int page_readlink(struct dentry *, char __user *, int);
-extern int page_follow_link_light(struct dentry *, struct nameidata *);
-extern void page_put_link(struct dentry *, struct nameidata *);
+extern void *page_follow_link_light(struct dentry *, struct nameidata *);
+extern void page_put_link(struct dentry *, struct nameidata *, void *);
 extern int page_symlink(struct inode *inode, const char *symname, int len);
 extern struct inode_operations page_symlink_inode_operations;
 extern int generic_readlink(struct dentry *, char __user *, int);
mm/shmem.c +6 -11
@@ -1773,32 +1773,27 @@ static int shmem_symlink(struct inode *d
 	return 0;
 }
 
-static int shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
+static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd)
 {
 	nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode));
-	return 0;
+	return NULL;
 }
 
-static int shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct page *page = NULL;
 	int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL);
 	nd_set_link(nd, res ? ERR_PTR(res) : kmap(page));
-	return 0;
+	return page;
 }
 
-static void shmem_put_link(struct dentry *dentry, struct nameidata *nd)
+static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
 {
 	if (!IS_ERR(nd_get_link(nd))) {
-		struct page *page;
-
-		page = find_get_page(dentry->d_inode->i_mapping, 0);
-		if (!page)
-			BUG();
+		struct page *page = cookie;
 		kunmap(page);
 		mark_page_accessed(page);
 		page_cache_release(page);
-		page_cache_release(page);
 	}
 }
 
#26 lore raw
Quoted 10 lines · Linus Torvalds
Quoted 3 lines · Anton Altaparmakov

Yes, sure. I have applied your patch to our 2.6.11.4 tree (with the one liner change I emailed you just now) and have kicked off a compile.

Actually, hold on. The original patch had another problem: it returned an uninitialized "page" pointer when page_getlink() failed.

Quoted 6 lines

This one should have that fixed, and has converted a few other filesystems. Most of them trivially, but I took the opportunity to just simplify NFS while I was at it, since it now has no reason to need to save off the "struct page *" any more.

It's still not tested, but at least I've looked at it a bit more ;)

That looks OK except for
* jffs2 is b0rken (see patch in another mail)
* afs, autofs4, befs, devfs, freevxfs, jffs2, jfs, ncpfs, procfs,
smbfs, sysvfs, ufs, xfs - prototype change for ->follow_link()
* befs, smbfs, xfs - same for ->put_link()
* ncpfs fix is actually missing here

Prototype changes are covered by patch below (incremental on top of your + jffs2 fix upthread). No ncpfs changes - these will go separately, assuming you haven't done them yet; just a plain janitor stuff.

#27 lore raw
Quoted 11 lines · Al Viro

That looks OK except for
* jffs2 is b0rken (see patch in another mail)
* afs, autofs4, befs, devfs, freevxfs, jffs2, jfs, ncpfs, procfs,
smbfs, sysvfs, ufs, xfs - prototype change for ->follow_link()
* befs, smbfs, xfs - same for ->put_link()
* ncpfs fix is actually missing here

Prototype changes are covered by patch below (incremental on top of your + jffs2 fix upthread). No ncpfs changes - these will go separately, assuming you haven't done them yet; just a plain janitor stuff.

Gaack... And here's the patch itself - sorry.

fs/afs/mntpt.c +4 -4
diff -urN RC13-rc6-git10-base/fs/afs/mntpt.c current/fs/afs/mntpt.c
@@ -30,7 +30,7 @@
 				       struct dentry *dentry,
 				       struct nameidata *nd);
 static int afs_mntpt_open(struct inode *inode, struct file *file);
-static int afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
+static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd);
 
 struct file_operations afs_mntpt_file_operations = {
 	.open		= afs_mntpt_open,
@@ -233,7 +233,7 @@
 /*
  * follow a link from a mountpoint directory, thus causing it to be mounted
  */
-static int afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct vfsmount *newmnt;
 	struct dentry *old_dentry;
@@ -249,7 +249,7 @@
 	newmnt = afs_mntpt_do_automount(dentry);
 	if (IS_ERR(newmnt)) {
 		path_release(nd);
-		return PTR_ERR(newmnt);
+		return (void *)newmnt;
 	}
 
 	old_dentry = nd->dentry;
@@ -267,7 +267,7 @@
 	}
 
 	kleave(" = %d", err);
-	return err;
+	return ERR_PTR(err);
 } /* end afs_mntpt_follow_link() */
 
 /*****************************************************************************/
fs/autofs4/symlink.c +2 -2
diff -urN RC13-rc6-git10-base/fs/autofs4/symlink.c current/fs/autofs4/symlink.c
@@ -12,11 +12,11 @@
 
 #include "autofs_i.h"
 
-static int autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct autofs_info *ino = autofs4_dentry_ino(dentry);
 	nd_set_link(nd, (char *)ino->u.symlink);
-	return 0;
+	return NULL;
 }
 
 struct inode_operations autofs4_symlink_inode_operations = {
fs/befs/linuxvfs.c +4 -4
diff -urN RC13-rc6-git10-base/fs/befs/linuxvfs.c current/fs/befs/linuxvfs.c
@@ -41,8 +41,8 @@
 static void befs_destroy_inode(struct inode *inode);
 static int befs_init_inodecache(void);
 static void befs_destroy_inodecache(void);
-static int befs_follow_link(struct dentry *, struct nameidata *);
-static void befs_put_link(struct dentry *, struct nameidata *);
+static void *befs_follow_link(struct dentry *, struct nameidata *);
+static void befs_put_link(struct dentry *, struct nameidata *, void *);
 static int befs_utf2nls(struct super_block *sb, const char *in, int in_len,
 			char **out, int *out_len);
 static int befs_nls2utf(struct super_block *sb, const char *in, int in_len,
@@ -487,10 +487,10 @@
 	}
 
 	nd_set_link(nd, link);
-	return 0;
+	return NULL;
 }
 
-static void befs_put_link(struct dentry *dentry, struct nameidata *nd)
+static void befs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
 {
 	befs_inode_info *befs_ino = BEFS_I(dentry->d_inode);
 	if (befs_ino->i_flags & BEFS_LONG_SYMLINK) {
fs/devfs/base.c +2 -2
diff -urN RC13-rc6-git10-base/fs/devfs/base.c current/fs/devfs/base.c
@@ -2491,11 +2491,11 @@
 	return 0;
 }				/*  End Function devfs_mknod  */
 
-static int devfs_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *devfs_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct devfs_entry *p = get_devfs_entry_from_vfs_inode(dentry->d_inode);
 	nd_set_link(nd, p ? p->u.symlink.linkname : ERR_PTR(-ENODEV));
-	return 0;
+	return NULL;
 }				/*  End Function devfs_follow_link  */
 
 static struct inode_operations devfs_iops = {
fs/freevxfs/vxfs_immed.c +2 -2
diff -urN RC13-rc6-git10-base/fs/freevxfs/vxfs_immed.c current/fs/freevxfs/vxfs_immed.c
@@ -38,7 +38,7 @@
 #include "vxfs_inode.h"
 
 
-static int	vxfs_immed_follow_link(struct dentry *, struct nameidata *);
+static void *	vxfs_immed_follow_link(struct dentry *, struct nameidata *);
 
 static int	vxfs_immed_readpage(struct file *, struct page *);
 
@@ -77,7 +77,7 @@
 {
 	struct vxfs_inode_info		*vip = VXFS_INO(dp->d_inode);
 	nd_set_link(np, vip->vii_immed.vi_immed);
-	return 0;
+	return NULL;
 }
 
 /**
fs/jffs2/symlink.c +3 -3
diff -urN RC13-rc6-git10-base/fs/jffs2/symlink.c current/fs/jffs2/symlink.c
@@ -18,7 +18,7 @@
 #include <linux/namei.h>
 #include "nodelist.h"
 
-static int jffs2_follow_link(struct dentry *dentry, struct nameidata *nd);
+static void *jffs2_follow_link(struct dentry *dentry, struct nameidata *nd);
 
 struct inode_operations jffs2_symlink_inode_operations =
 {	
@@ -27,7 +27,7 @@
 	.setattr =	jffs2_setattr
 };
 
-static int jffs2_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *jffs2_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
 	char *p = (char *)f->dents;
@@ -60,6 +60,6 @@
 	 * since the only way that may cause f->dents to be changed is iput() operation.
 	 * But VFS will not use f->dents after iput() has been called.
 	 */
-	return 0;
+	return NULL;
 }
 
fs/jfs/symlink.c +2 -2
diff -urN RC13-rc6-git10-base/fs/jfs/symlink.c current/fs/jfs/symlink.c
@@ -22,11 +22,11 @@
 #include "jfs_inode.h"
 #include "jfs_xattr.h"
 
-static int jfs_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *jfs_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	char *s = JFS_IP(dentry->d_inode)->i_inline;
 	nd_set_link(nd, s);
-	return 0;
+	return NULL;
 }
 
 struct inode_operations jfs_symlink_inode_operations = {
fs/proc/base.c +4 -4
diff -urN RC13-rc6-git10-base/fs/proc/base.c current/fs/proc/base.c
@@ -890,7 +890,7 @@
 };
 #endif /* CONFIG_SECCOMP */
 
-static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct inode *inode = dentry->d_inode;
 	int error = -EACCES;
@@ -907,7 +907,7 @@
 	error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
 	nd->last_type = LAST_BIND;
 out:
-	return error;
+	return ERR_PTR(error);
 }
 
 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
@@ -1692,11 +1692,11 @@
 	return vfs_readlink(dentry,buffer,buflen,tmp);
 }
 
-static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	char tmp[30];
 	sprintf(tmp, "%d", current->tgid);
-	return vfs_follow_link(nd,tmp);
+	return ERR_PTR(vfs_follow_link(nd,tmp));
 }	
 
 static struct inode_operations proc_self_inode_operations = {
fs/proc/generic.c +2 -2
diff -urN RC13-rc6-git10-base/fs/proc/generic.c current/fs/proc/generic.c
@@ -329,10 +329,10 @@
 	spin_unlock(&proc_inum_lock);
 }
 
-static int proc_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *proc_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	nd_set_link(nd, PDE(dentry->d_inode)->data);
-	return 0;
+	return NULL;
 }
 
 static struct inode_operations proc_link_inode_operations = {
fs/smbfs/symlink.c +3 -3
diff -urN RC13-rc6-git10-base/fs/smbfs/symlink.c current/fs/smbfs/symlink.c
@@ -34,7 +34,7 @@
 	return smb_proc_symlink(server_from_dentry(dentry), dentry, oldname);
 }
 
-static int smb_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *smb_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	char *link = __getname();
 	DEBUG1("followlink of %s/%s\n", DENTRY_PATH(dentry));
@@ -52,10 +52,10 @@
 		}
 	}
 	nd_set_link(nd, link);
-	return 0;
+	return NULL;
 }
 
-static void smb_put_link(struct dentry *dentry, struct nameidata *nd)
+static void smb_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
 {
 	char *s = nd_get_link(nd);
 	if (!IS_ERR(s))
fs/sysv/symlink.c +2 -2
diff -urN RC13-rc6-git10-base/fs/sysv/symlink.c current/fs/sysv/symlink.c
@@ -8,10 +8,10 @@
 #include "sysv.h"
 #include <linux/namei.h>
 
-static int sysv_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *sysv_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	nd_set_link(nd, (char *)SYSV_I(dentry->d_inode)->i_data);
-	return 0;
+	return NULL;
 }
 
 struct inode_operations sysv_fast_symlink_inode_operations = {
fs/ufs/symlink.c +2 -2
diff -urN RC13-rc6-git10-base/fs/ufs/symlink.c current/fs/ufs/symlink.c
@@ -29,11 +29,11 @@
 #include <linux/namei.h>
 #include <linux/ufs_fs.h>
 
-static int ufs_follow_link(struct dentry *dentry, struct nameidata *nd)
+static void *ufs_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct ufs_inode_info *p = UFS_I(dentry->d_inode);
 	nd_set_link(nd, (char*)p->i_u1.i_symlink);
-	return 0;
+	return NULL;
 }
 
 struct inode_operations ufs_fast_symlink_inode_operations = {
fs/xfs/linux-2.6/xfs_iops.c +5 -5
diff -urN RC13-rc6-git10-base/fs/xfs/linux-2.6/xfs_iops.c current/fs/xfs/linux-2.6/xfs_iops.c
@@ -374,7 +374,7 @@
  * we need to be very careful about how much stack we use.
  * uio is kmalloced for this reason...
  */
-STATIC int
+STATIC void *
 linvfs_follow_link(
 	struct dentry		*dentry,
 	struct nameidata	*nd)
@@ -391,14 +391,14 @@
 	link = (char *)kmalloc(MAXNAMELEN+1, GFP_KERNEL);
 	if (!link) {
 		nd_set_link(nd, ERR_PTR(-ENOMEM));
-		return 0;
+		return NULL;
 	}
 
 	uio = (uio_t *)kmalloc(sizeof(uio_t), GFP_KERNEL);
 	if (!uio) {
 		kfree(link);
 		nd_set_link(nd, ERR_PTR(-ENOMEM));
-		return 0;
+		return NULL;
 	}
 
 	vp = LINVFS_GET_VP(dentry->d_inode);
@@ -422,10 +422,10 @@
 	kfree(uio);
 
 	nd_set_link(nd, link);
-	return 0;
+	return NULL;
 }
 
-static void linvfs_put_link(struct dentry *dentry, struct nameidata *nd)
+static void linvfs_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
 {
 	char *s = nd_get_link(nd);
 	if (!IS_ERR(s))
#28 lore raw
In reply to Al Viro
Quoted 3 lines · Al Viro

That looks OK except for
* ncpfs fix is actually missing here

Well, the thing is, with the change to page_follow_link() and page_put_link(), ncpfs should now work fine - it doesn't need any fixing any more.

It was makign an assumption that used to be incorrect, but that assumption became ok with the calling convention change - now the page_follow_link() and page_put_link() functions work fine even if the page cache might get invalidated in between the calls.

So with your patch, things should be fine (and external filesystems will generate warnings on compiles, but should work fine, since the change maintains the ABI even if the API changed - which is pretty unusual).

Linus

#29 lore raw
Quoted 10 lines · Linus Torvalds
Quoted 3 lines · Al Viro

That looks OK except for
* ncpfs fix is actually missing here

Well, the thing is, with the change to page_follow_link() and page_put_link(), ncpfs should now work fine - it doesn't need any fixing any more.

Ah - right, it's using normal methods...

#30 lore raw
In reply to Linus Torvalds

Hi Linus,

Quoted 6 lines · Linus Torvalds
Quoted 2 lines · Anton Altaparmakov

Yes, sure. I have applied your patch to our 2.6.11.4 tree (with the one liner change I emailed you just now) and have kicked off a compile.

Actually, hold on. The original patch had another problem: it returned an uninitialized "page" pointer when page_getlink() failed.

I copied the fix to that problem from your new patch by hand and recompiled the kernel (that way I didn't have to rebuild the modules again). Note I did not apply any of the fs specific changes. I only did the VFS part of your patch that was actually necessary. And I reverted my ncpfs fix so it is now again using the vfs supplied symlink helpers.

Having booted the new kernel and trying nautilus it worked fine accessing the same symlink that failed before, so your patch fixes the ncpfs problem as one would expect but always good to be sure. (-:

btw. It may be an idea to switch smbfs to use the fixed generic symlink functions, too, so it benefits from symlink caching...

Best regards,

Anton

Signature · 4 lines
Anton Altaparmakov <aia21 at cam.ac.uk> (replace at with @)
Unix Support, Computing Service, University of Cambridge, CB2 3QH, UK
Linux NTFS maintainer / IRC: #ntfs on irc.freenode.net
WWW: http://linux-ntfs.sf.net/ & http://www-stu.christs.cam.ac.uk/~aia21/