xref: /openbmc/linux/fs/ocfs2/symlink.c (revision 5e8d780d)
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  *  linux/cluster/ssi/cfs/symlink.c
5  *
6  *	This program is free software; you can redistribute it and/or
7  *	modify it under the terms of the GNU General Public License as
8  *	published by the Free Software Foundation; either version 2 of
9  *	the License, or (at your option) any later version.
10  *
11  *	This program is distributed in the hope that it will be useful,
12  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *	MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE
14  *	or NON INFRINGEMENT.  See the GNU General Public License for more
15  *	details.
16  *
17  * 	You should have received a copy of the GNU General Public License
18  * 	along with this program; if not, write to the Free Software
19  * 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  *	Questions/Comments/Bugfixes to ssic-linux-devel@lists.sourceforge.net
22  *
23  *  Copyright (C) 1992  Rick Sladkey
24  *
25  *  Optimization changes Copyright (C) 1994 Florian La Roche
26  *
27  *  Jun 7 1999, cache symlink lookups in the page cache.  -DaveM
28  *
29  *  Portions Copyright (C) 2001 Compaq Computer Corporation
30  *
31  *  ocfs2 symlink handling code.
32  *
33  *  Copyright (C) 2004, 2005 Oracle.
34  *
35  */
36 
37 #include <linux/fs.h>
38 #include <linux/types.h>
39 #include <linux/slab.h>
40 #include <linux/pagemap.h>
41 #include <linux/utsname.h>
42 
43 #define MLOG_MASK_PREFIX ML_NAMEI
44 #include <cluster/masklog.h>
45 
46 #include "ocfs2.h"
47 
48 #include "alloc.h"
49 #include "file.h"
50 #include "inode.h"
51 #include "journal.h"
52 #include "symlink.h"
53 
54 #include "buffer_head_io.h"
55 
56 static char *ocfs2_page_getlink(struct dentry * dentry,
57 				struct page **ppage);
58 static char *ocfs2_fast_symlink_getlink(struct inode *inode,
59 					struct buffer_head **bh);
60 
61 /* get the link contents into pagecache */
62 static char *ocfs2_page_getlink(struct dentry * dentry,
63 				struct page **ppage)
64 {
65 	struct page * page;
66 	struct address_space *mapping = dentry->d_inode->i_mapping;
67 	page = read_mapping_page(mapping, 0, NULL);
68 	if (IS_ERR(page))
69 		goto sync_fail;
70 	wait_on_page_locked(page);
71 	if (!PageUptodate(page))
72 		goto async_fail;
73 	*ppage = page;
74 	return kmap(page);
75 
76 async_fail:
77 	page_cache_release(page);
78 	return ERR_PTR(-EIO);
79 
80 sync_fail:
81 	return (char*)page;
82 }
83 
84 static char *ocfs2_fast_symlink_getlink(struct inode *inode,
85 					struct buffer_head **bh)
86 {
87 	int status;
88 	char *link = NULL;
89 	struct ocfs2_dinode *fe;
90 
91 	mlog_entry_void();
92 
93 	status = ocfs2_read_block(OCFS2_SB(inode->i_sb),
94 				  OCFS2_I(inode)->ip_blkno,
95 				  bh,
96 				  OCFS2_BH_CACHED,
97 				  inode);
98 	if (status < 0) {
99 		mlog_errno(status);
100 		link = ERR_PTR(status);
101 		goto bail;
102 	}
103 
104 	fe = (struct ocfs2_dinode *) (*bh)->b_data;
105 	link = (char *) fe->id2.i_symlink;
106 bail:
107 	mlog_exit(status);
108 
109 	return link;
110 }
111 
112 static int ocfs2_readlink(struct dentry *dentry,
113 			  char __user *buffer,
114 			  int buflen)
115 {
116 	int ret;
117 	char *link;
118 	struct buffer_head *bh = NULL;
119 	struct inode *inode = dentry->d_inode;
120 
121 	mlog_entry_void();
122 
123 	link = ocfs2_fast_symlink_getlink(inode, &bh);
124 	if (IS_ERR(link)) {
125 		ret = PTR_ERR(link);
126 		goto out;
127 	}
128 
129 	ret = vfs_readlink(dentry, buffer, buflen, link);
130 
131 	brelse(bh);
132 out:
133 	mlog_exit(ret);
134 	return ret;
135 }
136 
137 static void *ocfs2_follow_link(struct dentry *dentry,
138 			       struct nameidata *nd)
139 {
140 	int status;
141 	char *link;
142 	struct inode *inode = dentry->d_inode;
143 	struct page *page = NULL;
144 	struct buffer_head *bh = NULL;
145 
146 	if (ocfs2_inode_is_fast_symlink(inode))
147 		link = ocfs2_fast_symlink_getlink(inode, &bh);
148 	else
149 		link = ocfs2_page_getlink(dentry, &page);
150 	if (IS_ERR(link)) {
151 		status = PTR_ERR(link);
152 		mlog_errno(status);
153 		goto bail;
154 	}
155 
156 	status = vfs_follow_link(nd, link);
157 	if (status && status != -ENOENT)
158 		mlog_errno(status);
159 bail:
160 	if (page) {
161 		kunmap(page);
162 		page_cache_release(page);
163 	}
164 	if (bh)
165 		brelse(bh);
166 
167 	return ERR_PTR(status);
168 }
169 
170 struct inode_operations ocfs2_symlink_inode_operations = {
171 	.readlink	= page_readlink,
172 	.follow_link	= ocfs2_follow_link,
173 	.getattr	= ocfs2_getattr,
174 };
175 struct inode_operations ocfs2_fast_symlink_inode_operations = {
176 	.readlink	= ocfs2_readlink,
177 	.follow_link	= ocfs2_follow_link,
178 	.getattr	= ocfs2_getattr,
179 };
180