]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/kernfs/symlink.c
Documentation: start documenting driver design patterns
[mirror_ubuntu-artful-kernel.git] / fs / kernfs / symlink.c
CommitLineData
b8441ed2
TH
1/*
2 * fs/kernfs/symlink.c - kernfs symlink implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
2072f1af
TH
10
11#include <linux/fs.h>
12#include <linux/gfp.h>
13#include <linux/namei.h>
14
15#include "kernfs-internal.h"
16
17/**
18 * kernfs_create_link - create a symlink
19 * @parent: directory to create the symlink in
20 * @name: name of the symlink
21 * @target: target node for the symlink to point to
22 *
23 * Returns the created node on success, ERR_PTR() value on error.
24 */
25struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
26 const char *name,
27 struct sysfs_dirent *target)
28{
29 struct sysfs_dirent *sd;
30 struct sysfs_addrm_cxt acxt;
31 int error;
32
bc755553
TH
33 sd = sysfs_new_dirent(kernfs_root(parent), name, S_IFLNK|S_IRWXUGO,
34 SYSFS_KOBJ_LINK);
2072f1af
TH
35 if (!sd)
36 return ERR_PTR(-ENOMEM);
37
ac9bba03 38 if (kernfs_ns_enabled(parent))
2072f1af
TH
39 sd->s_ns = target->s_ns;
40 sd->s_symlink.target_sd = target;
41 kernfs_get(target); /* ref owned by symlink */
42
43 sysfs_addrm_start(&acxt);
44 error = sysfs_add_one(&acxt, sd, parent);
45 sysfs_addrm_finish(&acxt);
46
47 if (!error)
48 return sd;
49
50 kernfs_put(sd);
51 return ERR_PTR(error);
52}
53
54static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
55 struct sysfs_dirent *target_sd, char *path)
56{
57 struct sysfs_dirent *base, *sd;
58 char *s = path;
59 int len = 0;
60
61 /* go up to the root, stop at the base */
62 base = parent_sd;
63 while (base->s_parent) {
64 sd = target_sd->s_parent;
65 while (sd->s_parent && base != sd)
66 sd = sd->s_parent;
67
68 if (base == sd)
69 break;
70
71 strcpy(s, "../");
72 s += 3;
73 base = base->s_parent;
74 }
75
76 /* determine end of target string for reverse fillup */
77 sd = target_sd;
78 while (sd->s_parent && sd != base) {
79 len += strlen(sd->s_name) + 1;
80 sd = sd->s_parent;
81 }
82
83 /* check limits */
84 if (len < 2)
85 return -EINVAL;
86 len--;
87 if ((s - path) + len > PATH_MAX)
88 return -ENAMETOOLONG;
89
90 /* reverse fillup of target string from target to base */
91 sd = target_sd;
92 while (sd->s_parent && sd != base) {
93 int slen = strlen(sd->s_name);
94
95 len -= slen;
96 strncpy(s + len, sd->s_name, slen);
97 if (len)
98 s[--len] = '/';
99
100 sd = sd->s_parent;
101 }
102
103 return 0;
104}
105
106static int sysfs_getlink(struct dentry *dentry, char *path)
107{
108 struct sysfs_dirent *sd = dentry->d_fsdata;
109 struct sysfs_dirent *parent_sd = sd->s_parent;
110 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
111 int error;
112
113 mutex_lock(&sysfs_mutex);
114 error = sysfs_get_target_path(parent_sd, target_sd, path);
115 mutex_unlock(&sysfs_mutex);
116
117 return error;
118}
119
120static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
121{
122 int error = -ENOMEM;
123 unsigned long page = get_zeroed_page(GFP_KERNEL);
124 if (page) {
125 error = sysfs_getlink(dentry, (char *) page);
126 if (error < 0)
127 free_page((unsigned long)page);
128 }
129 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
130 return NULL;
131}
132
133static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
134 void *cookie)
135{
136 char *page = nd_get_link(nd);
137 if (!IS_ERR(page))
138 free_page((unsigned long)page);
139}
140
141const struct inode_operations sysfs_symlink_inode_operations = {
142 .setxattr = sysfs_setxattr,
2322392b
TH
143 .removexattr = sysfs_removexattr,
144 .getxattr = sysfs_getxattr,
145 .listxattr = sysfs_listxattr,
2072f1af
TH
146 .readlink = generic_readlink,
147 .follow_link = sysfs_follow_link,
148 .put_link = sysfs_put_link,
149 .setattr = sysfs_setattr,
150 .getattr = sysfs_getattr,
151 .permission = sysfs_permission,
152};