Commit 680baacb authored by Al Viro's avatar Al Viro
Browse files

new ->follow_link() and ->put_link() calling conventions



a) instead of storing the symlink body (via nd_set_link()) and returning
an opaque pointer later passed to ->put_link(), ->follow_link() _stores_
that opaque pointer (into void * passed by address by caller) and returns
the symlink body.  Returning ERR_PTR() on error, NULL on jump (procfs magic
symlinks) and pointer to symlink body for normal symlinks.  Stored pointer
is ignored in all cases except the last one.

Storing NULL for opaque pointer (or not storing it at all) means no call
of ->put_link().

b) the body used to be passed to ->put_link() implicitly (via nameidata).
Now only the opaque pointer is.  In the cases when we used the symlink body
to free stuff, ->follow_link() now should store it as opaque pointer in addition
to returning it.
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 46afd6f6
......@@ -50,8 +50,8 @@ prototypes:
int (*rename2) (struct inode *, struct dentry *,
struct inode *, struct dentry *, unsigned int);
int (*readlink) (struct dentry *, char __user *,int);
void * (*follow_link) (struct dentry *, struct nameidata *);
void (*put_link) (struct dentry *, struct nameidata *, void *);
const char *(*follow_link) (struct dentry *, void **, struct nameidata *);
void (*put_link) (struct dentry *, void *);
void (*truncate) (struct inode *);
int (*permission) (struct inode *, int, unsigned int);
int (*get_acl)(struct inode *, int);
......
......@@ -350,8 +350,8 @@ struct inode_operations {
int (*rename2) (struct inode *, struct dentry *,
struct inode *, struct dentry *, unsigned int);
int (*readlink) (struct dentry *, char __user *,int);
void * (*follow_link) (struct dentry *, struct nameidata *);
void (*put_link) (struct dentry *, struct nameidata *, void *);
const char *(*follow_link) (struct dentry *, void **, struct nameidata *);
void (*put_link) (struct dentry *, void *);
int (*permission) (struct inode *, int);
int (*get_acl)(struct inode *, int);
int (*setattr) (struct dentry *, struct iattr *);
......
......@@ -118,7 +118,7 @@ failed:
return rc;
}
static void *ll_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *ll_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
struct inode *inode = d_inode(dentry);
struct ptlrpc_request *request = NULL;
......@@ -140,18 +140,17 @@ static void *ll_follow_link(struct dentry *dentry, struct nameidata *nd)
}
if (rc) {
ptlrpc_req_finished(request);
request = NULL;
symname = ERR_PTR(rc);
return ERR_PTR(rc);
}
nd_set_link(nd, symname);
/* symname may contain a pointer to the request message buffer,
* we delay request releasing until ll_put_link then.
*/
return request;
*cookie = request;
return symname;
}
static void ll_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
static void ll_put_link(struct dentry *dentry, void *cookie)
{
ptlrpc_req_finished(cookie);
}
......
......@@ -1230,11 +1230,12 @@ ino_t v9fs_qid2ino(struct p9_qid *qid)
*
*/
static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *v9fs_vfs_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
struct v9fs_session_info *v9ses = v9fs_dentry2v9ses(dentry);
struct p9_fid *fid = v9fs_fid_lookup(dentry);
struct p9_wstat *st;
char *res;
p9_debug(P9_DEBUG_VFS, "%pd\n", dentry);
......@@ -1253,14 +1254,14 @@ static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
kfree(st);
return ERR_PTR(-EINVAL);
}
if (strlen(st->extension) >= PATH_MAX)
st->extension[PATH_MAX - 1] = '\0';
nd_set_link(nd, st->extension);
res = st->extension;
st->extension = NULL;
if (strlen(res) >= PATH_MAX)
res[PATH_MAX - 1] = '\0';
p9stat_free(st);
kfree(st);
return NULL;
return *cookie = res;
}
/**
......
......@@ -909,8 +909,8 @@ error:
*
*/
static void *
v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
static const char *
v9fs_vfs_follow_link_dotl(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
struct p9_fid *fid = v9fs_fid_lookup(dentry);
char *target;
......@@ -923,8 +923,7 @@ v9fs_vfs_follow_link_dotl(struct dentry *dentry, struct nameidata *nd)
retval = p9_client_readlink(fid, &target);
if (retval)
return ERR_PTR(retval);
nd_set_link(nd, target);
return NULL;
return *cookie = target;
}
int v9fs_refresh_inode_dotl(struct p9_fid *fid, struct inode *inode)
......
......@@ -12,14 +12,13 @@
#include "autofs_i.h"
static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *autofs4_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
struct autofs_info *ino = autofs4_dentry_ino(dentry);
if (ino && !autofs4_oz_mode(sbi))
ino->last_used = jiffies;
nd_set_link(nd, d_inode(dentry)->i_private);
return NULL;
return d_inode(dentry)->i_private;
}
const struct inode_operations autofs4_symlink_inode_operations = {
......
......@@ -42,7 +42,7 @@ static struct inode *befs_iget(struct super_block *, unsigned long);
static struct inode *befs_alloc_inode(struct super_block *sb);
static void befs_destroy_inode(struct inode *inode);
static void befs_destroy_inodecache(void);
static void *befs_follow_link(struct dentry *, struct nameidata *);
static const char *befs_follow_link(struct dentry *, void **, struct nameidata *nd);
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,
......@@ -463,8 +463,8 @@ befs_destroy_inodecache(void)
* The data stream become link name. Unless the LONG_SYMLINK
* flag is set.
*/
static void *
befs_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *
befs_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
struct super_block *sb = dentry->d_sb;
struct befs_inode_info *befs_ino = BEFS_I(d_inode(dentry));
......@@ -474,23 +474,20 @@ befs_follow_link(struct dentry *dentry, struct nameidata *nd)
if (len == 0) {
befs_error(sb, "Long symlink with illegal length");
link = ERR_PTR(-EIO);
} else {
befs_debug(sb, "Follow long symlink");
link = kmalloc(len, GFP_NOFS);
if (!link) {
link = ERR_PTR(-ENOMEM);
} else if (befs_read_lsymlink(sb, data, link, len) != len) {
kfree(link);
befs_error(sb, "Failed to read entire long symlink");
link = ERR_PTR(-EIO);
} else {
link[len - 1] = '\0';
}
return ERR_PTR(-EIO);
}
nd_set_link(nd, link);
return NULL;
befs_debug(sb, "Follow long symlink");
link = kmalloc(len, GFP_NOFS);
if (!link)
return ERR_PTR(-ENOMEM);
if (befs_read_lsymlink(sb, data, link, len) != len) {
kfree(link);
befs_error(sb, "Failed to read entire long symlink");
return ERR_PTR(-EIO);
}
link[len - 1] = '\0';
return *cookie = link;
}
/*
......
......@@ -120,7 +120,7 @@ extern struct vfsmount *cifs_dfs_d_automount(struct path *path);
#endif
/* Functions related to symlinks */
extern void *cifs_follow_link(struct dentry *direntry, struct nameidata *nd);
extern const char *cifs_follow_link(struct dentry *direntry, void **cookie, struct nameidata *nd);
extern int cifs_readlink(struct dentry *direntry, char __user *buffer,
int buflen);
extern int cifs_symlink(struct inode *inode, struct dentry *direntry,
......
......@@ -626,8 +626,8 @@ cifs_hl_exit:
return rc;
}
void *
cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
const char *
cifs_follow_link(struct dentry *direntry, void **cookie, struct nameidata *nd)
{
struct inode *inode = d_inode(direntry);
int rc = -ENOMEM;
......@@ -643,16 +643,18 @@ cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
tlink = cifs_sb_tlink(cifs_sb);
if (IS_ERR(tlink)) {
rc = PTR_ERR(tlink);
tlink = NULL;
goto out;
free_xid(xid);
return ERR_CAST(tlink);
}
tcon = tlink_tcon(tlink);
server = tcon->ses->server;
full_path = build_path_from_dentry(direntry);
if (!full_path)
goto out;
if (!full_path) {
free_xid(xid);
cifs_put_tlink(tlink);
return ERR_PTR(-ENOMEM);
}
cifs_dbg(FYI, "Full path: %s inode = 0x%p\n", full_path, inode);
......@@ -670,17 +672,13 @@ cifs_follow_link(struct dentry *direntry, struct nameidata *nd)
&target_path, cifs_sb);
kfree(full_path);
out:
free_xid(xid);
cifs_put_tlink(tlink);
if (rc != 0) {
kfree(target_path);
target_path = ERR_PTR(rc);
return ERR_PTR(rc);
}
free_xid(xid);
if (tlink)
cifs_put_tlink(tlink);
nd_set_link(nd, target_path);
return NULL;
return *cookie = target_path;
}
int
......
......@@ -279,30 +279,26 @@ static int configfs_getlink(struct dentry *dentry, char * path)
}
static void *configfs_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *configfs_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
int error = -ENOMEM;
unsigned long page = get_zeroed_page(GFP_KERNEL);
int error;
if (page) {
error = configfs_getlink(dentry, (char *)page);
if (!error) {
nd_set_link(nd, (char *)page);
return (void *)page;
}
if (!page)
return ERR_PTR(-ENOMEM);
error = configfs_getlink(dentry, (char *)page);
if (!error) {
return *cookie = (void *)page;
}
nd_set_link(nd, ERR_PTR(error));
return NULL;
free_page(page);
return ERR_PTR(error);
}
static void configfs_put_link(struct dentry *dentry, struct nameidata *nd,
void *cookie)
static void configfs_put_link(struct dentry *dentry, void *cookie)
{
if (cookie) {
unsigned long page = (unsigned long)cookie;
free_page(page);
}
free_page((unsigned long)cookie);
}
const struct inode_operations configfs_symlink_inode_operations = {
......
......@@ -675,18 +675,16 @@ out:
return rc ? ERR_PTR(rc) : buf;
}
static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *ecryptfs_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
size_t len;
char *buf = ecryptfs_readlink_lower(dentry, &len);
if (IS_ERR(buf))
goto out;
return buf;
fsstack_copy_attr_atime(d_inode(dentry),
d_inode(ecryptfs_dentry_to_lower(dentry)));
buf[len] = '\0';
out:
nd_set_link(nd, buf);
return NULL;
return *cookie = buf;
}
/**
......
......@@ -23,7 +23,7 @@
#include "xattr.h"
#ifdef CONFIG_EXT4_FS_ENCRYPTION
static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *ext4_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
struct page *cpage = NULL;
char *caddr, *paddr = NULL;
......@@ -37,7 +37,7 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
ctx = ext4_get_fname_crypto_ctx(inode, inode->i_sb->s_blocksize);
if (IS_ERR(ctx))
return ctx;
return ERR_CAST(ctx);
if (ext4_inode_is_fast_symlink(inode)) {
caddr = (char *) EXT4_I(inode)->i_data;
......@@ -46,7 +46,7 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
cpage = read_mapping_page(inode->i_mapping, 0, NULL);
if (IS_ERR(cpage)) {
ext4_put_fname_crypto_ctx(&ctx);
return cpage;
return ERR_CAST(cpage);
}
caddr = kmap(cpage);
caddr[size] = 0;
......@@ -77,13 +77,12 @@ static void *ext4_follow_link(struct dentry *dentry, struct nameidata *nd)
/* Null-terminate the name */
if (res <= plen)
paddr[res] = '\0';
nd_set_link(nd, paddr);
ext4_put_fname_crypto_ctx(&ctx);
if (cpage) {
kunmap(cpage);
page_cache_release(cpage);
}
return NULL;
return *cookie = paddr;
errout:
ext4_put_fname_crypto_ctx(&ctx);
if (cpage) {
......
......@@ -296,19 +296,15 @@ fail:
return err;
}
static void *f2fs_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *f2fs_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
struct page *page = page_follow_link_light(dentry, nd);
if (IS_ERR_OR_NULL(page))
return page;
/* this is broken symlink case */
if (*nd_get_link(nd) == 0) {
page_put_link(dentry, nd, page);
return ERR_PTR(-ENOENT);
const char *link = page_follow_link_light(dentry, cookie, nd);
if (!IS_ERR(link) && !*link) {
/* this is broken symlink case */
page_put_link(dentry, *cookie);
link = ERR_PTR(-ENOENT);
}
return page;
return link;
}
static int f2fs_symlink(struct inode *dir, struct dentry *dentry,
......
......@@ -1365,7 +1365,7 @@ static int fuse_readdir(struct file *file, struct dir_context *ctx)
return err;
}
static char *read_link(struct dentry *dentry)
static const char *fuse_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
struct inode *inode = d_inode(dentry);
struct fuse_conn *fc = get_fuse_conn(inode);
......@@ -1389,26 +1389,15 @@ static char *read_link(struct dentry *dentry)
link = ERR_PTR(ret);
} else {
link[ret] = '\0';
*cookie = link;
}
fuse_invalidate_atime(inode);
return link;
}
static void free_link(char *link)
static void fuse_put_link(struct dentry *dentry, void *cookie)
{
if (!IS_ERR(link))
free_page((unsigned long) link);
}
static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
{
nd_set_link(nd, read_link(dentry));
return NULL;
}
static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
{
free_link(nd_get_link(nd));
free_page((unsigned long) cookie);
}
static int fuse_dir_open(struct inode *inode, struct file *file)
......
......@@ -1548,7 +1548,7 @@ out:
* Returns: 0 on success or error code
*/
static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *gfs2_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
struct gfs2_inode *ip = GFS2_I(d_inode(dentry));
struct gfs2_holder i_gh;
......@@ -1561,8 +1561,7 @@ static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
error = gfs2_glock_nq(&i_gh);
if (error) {
gfs2_holder_uninit(&i_gh);
nd_set_link(nd, ERR_PTR(error));
return NULL;
return ERR_PTR(error);
}
size = (unsigned int)i_size_read(&ip->i_inode);
......@@ -1586,8 +1585,9 @@ static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
brelse(dibh);
out:
gfs2_glock_dq_uninit(&i_gh);
nd_set_link(nd, buf);
return NULL;
if (!IS_ERR(buf))
*cookie = buf;
return buf;
}
/**
......
......@@ -892,7 +892,7 @@ static const struct inode_operations hostfs_dir_iops = {
.setattr = hostfs_setattr,
};
static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *hostfs_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
char *link = __getname();
if (link) {
......@@ -906,21 +906,18 @@ static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
}
if (err < 0) {
__putname(link);
link = ERR_PTR(err);
return ERR_PTR(err);
}
} else {
link = ERR_PTR(-ENOMEM);
return ERR_PTR(-ENOMEM);
}
nd_set_link(nd, link);
return NULL;
return *cookie = link;
}
static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
static void hostfs_put_link(struct dentry *dentry, void *cookie)
{
char *s = nd_get_link(nd);
if (!IS_ERR(s))
__putname(s);
__putname(cookie);
}
static const struct inode_operations hostfs_link_iops = {
......
......@@ -642,20 +642,19 @@ static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
buflen);
}
static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *hppfs_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
struct dentry *proc_dentry = HPPFS_I(d_inode(dentry))->proc_dentry;
return d_inode(proc_dentry)->i_op->follow_link(proc_dentry, nd);
return d_inode(proc_dentry)->i_op->follow_link(proc_dentry, cookie, nd);
}
static void hppfs_put_link(struct dentry *dentry, struct nameidata *nd,
void *cookie)
static void hppfs_put_link(struct dentry *dentry, void *cookie)
{
struct dentry *proc_dentry = HPPFS_I(d_inode(dentry))->proc_dentry;
if (d_inode(proc_dentry)->i_op->put_link)
d_inode(proc_dentry)->i_op->put_link(proc_dentry, nd, cookie);
d_inode(proc_dentry)->i_op->put_link(proc_dentry, cookie);
}
static const struct inode_operations hppfs_dir_iops = {
......
......@@ -112,25 +112,23 @@ static int kernfs_getlink(struct dentry *dentry, char *path)
return error;
}
static void *kernfs_iop_follow_link(struct dentry *dentry, struct nameidata *nd)
static const char *kernfs_iop_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
int error = -ENOMEM;
unsigned long page = get_zeroed_page(GFP_KERNEL);
if (page) {
error = kernfs_getlink(dentry, (char *) page);
if (error < 0)
free_page((unsigned long)page);
if (!page)
return ERR_PTR(-ENOMEM);
error = kernfs_getlink(dentry, (char *)page);
if (unlikely(error < 0)) {
free_page((unsigned long)page);
return ERR_PTR(error);
}
nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
return NULL;
return *cookie = (char *)page;
}
static void kernfs_iop_put_link(struct dentry *dentry, struct nameidata *nd,
void *cookie)
static void kernfs_iop_put_link(struct dentry *dentry, void *cookie)
{
char *page = nd_get_link(nd);
if (!IS_ERR(page))
free_page((unsigned long)page);
free_page((unsigned long)cookie);
}
const struct inode_operations kernfs_symlink_iops = {
......
......@@ -1024,12 +1024,9 @@ int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
}
EXPORT_SYMBOL(noop_fsync);
void kfree_put_link(struct dentry *dentry, struct nameidata *nd,
void *cookie)
void kfree_put_link(struct dentry *dentry, void *cookie)
{
char *s = nd_get_link(nd);
if (!IS_ERR(s))
kfree(s);
kfree(cookie);
}
EXPORT_SYMBOL(kfree_put_link);
......@@ -1094,10 +1091,9 @@ simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
}
EXPORT_SYMBOL(simple_nosetlease);
void *simple_follow_link(struct dentry *dentry, struct nameidata *nd)
const char *simple_follow_link(struct dentry *dentry, void **cookie, struct nameidata *nd)
{
nd_set_link(nd, d_inode(dentry)->i_link);
return NULL;
return d_inode(dentry)->i_link;
}
EXPORT_SYMBOL(simple_follow_link);
......
......@@ -502,7 +502,6 @@ struct nameidata {
int last_type;
unsigned depth;
struct file *base;
char *saved_names[MAX_NESTED_LINKS + 1];
};
/*
......@@ -713,23 +712,11 @@ void nd_jump_link(struct nameidata *nd, struct path *path)
nd->flags |= LOOKUP_JUMPED;
}
void nd_set_link(struct nameidata *nd, char *path)
{
nd->saved_names[nd->depth] = path;
}
EXPORT_SYMBOL(nd_set_link);
char *nd_get_link(struct nameidata *nd)
{
return nd->saved_names[nd->depth];
}
EXPORT_SYMBOL(nd_get_link);
static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)