Skip to content

Commit 3abb1d9

Browse files
committed
kernfs: make kernfs_path*() behave in the style of strlcpy()
kernfs_path*() functions always return the length of the full path but the path content is undefined if the length is larger than the provided buffer. This makes its behavior different from strlcpy() and requires error handling in all its users even when they don't care about truncation. In addition, the implementation can actully be simplified by making it behave properly in strlcpy() style. * Update kernfs_path_from_node_locked() to always fill up the buffer with path. If the buffer is not large enough, the output is truncated and terminated. * kernfs_path() no longer needs error handling. Make it a simple inline wrapper around kernfs_path_from_node(). * sysfs_warn_dup()'s use of kernfs_path() doesn't need error handling. Updated accordingly. * cgroup_path()'s use of kernfs_path() updated to retain the old behavior. Signed-off-by: Tejun Heo <tj@kernel.org> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Acked-by: Serge Hallyn <serge.hallyn@ubuntu.com>
1 parent 0e0b2af commit 3abb1d9

4 files changed

Lines changed: 42 additions & 53 deletions

File tree

‎fs/kernfs/dir.c‎

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,18 @@ static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
110110
* kn_to: /n1/n2/n3 [depth=3]
111111
* result: /../..
112112
*
113-
* return value: length of the string. If greater than buflen,
114-
* then contents of buf are undefined. On error, -1 is returned.
113+
* Returns the length of the full path. If the full length is equal to or
114+
* greater than @buflen, @buf contains the truncated path with the trailing
115+
* '\0'. On error, -errno is returned.
115116
*/
116117
static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
117118
struct kernfs_node *kn_from,
118119
char *buf, size_t buflen)
119120
{
120121
struct kernfs_node *kn, *common;
121122
const char parent_str[] = "/..";
122-
size_t depth_from, depth_to, len = 0, nlen = 0;
123-
char *p;
124-
int i;
123+
size_t depth_from, depth_to, len = 0;
124+
int i, j;
125125

126126
if (!kn_from)
127127
kn_from = kernfs_root(kn_to)->kn;
@@ -131,7 +131,7 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
131131

132132
common = kernfs_common_ancestor(kn_from, kn_to);
133133
if (WARN_ON(!common))
134-
return -1;
134+
return -EINVAL;
135135

136136
depth_to = kernfs_depth(common, kn_to);
137137
depth_from = kernfs_depth(common, kn_from);
@@ -144,22 +144,16 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
144144
len < buflen ? buflen - len : 0);
145145

146146
/* Calculate how many bytes we need for the rest */
147-
for (kn = kn_to; kn != common; kn = kn->parent)
148-
nlen += strlen(kn->name) + 1;
149-
150-
if (len + nlen >= buflen)
151-
return len + nlen;
152-
153-
p = buf + len + nlen;
154-
*p = '\0';
155-
for (kn = kn_to; kn != common; kn = kn->parent) {
156-
size_t tmp = strlen(kn->name);
157-
p -= tmp;
158-
memcpy(p, kn->name, tmp);
159-
*(--p) = '/';
147+
for (i = depth_to - 1; i >= 0; i--) {
148+
for (kn = kn_to, j = 0; j < i; j++)
149+
kn = kn->parent;
150+
len += strlcpy(buf + len, "/",
151+
len < buflen ? buflen - len : 0);
152+
len += strlcpy(buf + len, kn->name,
153+
len < buflen ? buflen - len : 0);
160154
}
161155

162-
return len + nlen;
156+
return len;
163157
}
164158

165159
/**
@@ -220,8 +214,9 @@ size_t kernfs_path_len(struct kernfs_node *kn)
220214
* path (which includes '..'s) as needed to reach from @from to @to is
221215
* returned.
222216
*
223-
* If @buf isn't long enough, the return value will be greater than @buflen
224-
* and @buf contents are undefined.
217+
* Returns the length of the full path. If the full length is equal to or
218+
* greater than @buflen, @buf contains the truncated path with the trailing
219+
* '\0'. On error, -errno is returned.
225220
*/
226221
int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
227222
char *buf, size_t buflen)
@@ -236,28 +231,6 @@ int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
236231
}
237232
EXPORT_SYMBOL_GPL(kernfs_path_from_node);
238233

239-
/**
240-
* kernfs_path - build full path of a given node
241-
* @kn: kernfs_node of interest
242-
* @buf: buffer to copy @kn's name into
243-
* @buflen: size of @buf
244-
*
245-
* Builds and returns the full path of @kn in @buf of @buflen bytes. The
246-
* path is built from the end of @buf so the returned pointer usually
247-
* doesn't match @buf. If @buf isn't long enough, @buf is nul terminated
248-
* and %NULL is returned.
249-
*/
250-
char *kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
251-
{
252-
int ret;
253-
254-
ret = kernfs_path_from_node(kn, NULL, buf, buflen);
255-
if (ret < 0 || ret >= buflen)
256-
return NULL;
257-
return buf;
258-
}
259-
EXPORT_SYMBOL_GPL(kernfs_path);
260-
261234
/**
262235
* pr_cont_kernfs_name - pr_cont name of a kernfs_node
263236
* @kn: kernfs_node of interest

‎fs/sysfs/dir.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ DEFINE_SPINLOCK(sysfs_symlink_target_lock);
2121

2222
void sysfs_warn_dup(struct kernfs_node *parent, const char *name)
2323
{
24-
char *buf, *path = NULL;
24+
char *buf;
2525

2626
buf = kzalloc(PATH_MAX, GFP_KERNEL);
2727
if (buf)
28-
path = kernfs_path(parent, buf, PATH_MAX);
28+
kernfs_path(parent, buf, PATH_MAX);
2929

3030
WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s/%s'\n",
31-
path, name);
31+
buf, name);
3232

3333
kfree(buf);
3434
}

‎include/linux/cgroup.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,12 @@ static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
541541
static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
542542
size_t buflen)
543543
{
544-
return kernfs_path(cgrp->kn, buf, buflen);
544+
int ret;
545+
546+
ret = kernfs_path(cgrp->kn, buf, buflen);
547+
if (ret < 0 || ret >= buflen)
548+
return NULL;
549+
return buf;
545550
}
546551

547552
static inline void pr_cont_cgroup_name(struct cgroup *cgrp)

‎include/linux/kernfs.h‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
272272
size_t kernfs_path_len(struct kernfs_node *kn);
273273
int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
274274
char *buf, size_t buflen);
275-
char *kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen);
276275
void pr_cont_kernfs_name(struct kernfs_node *kn);
277276
void pr_cont_kernfs_path(struct kernfs_node *kn);
278277
struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
@@ -349,10 +348,6 @@ static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
349348
char *buf, size_t buflen)
350349
{ return -ENOSYS; }
351350

352-
static inline char *kernfs_path(struct kernfs_node *kn, char *buf,
353-
size_t buflen)
354-
{ return NULL; }
355-
356351
static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
357352
static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
358353

@@ -441,6 +436,22 @@ static inline void kernfs_init(void) { }
441436

442437
#endif /* CONFIG_KERNFS */
443438

439+
/**
440+
* kernfs_path - build full path of a given node
441+
* @kn: kernfs_node of interest
442+
* @buf: buffer to copy @kn's name into
443+
* @buflen: size of @buf
444+
*
445+
* Builds and returns the full path of @kn in @buf of @buflen bytes. The
446+
* path is built from the end of @buf so the returned pointer usually
447+
* doesn't match @buf. If @buf isn't long enough, @buf is nul terminated
448+
* and %NULL is returned.
449+
*/
450+
static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
451+
{
452+
return kernfs_path_from_node(kn, NULL, buf, buflen);
453+
}
454+
444455
static inline struct kernfs_node *
445456
kernfs_find_and_get(struct kernfs_node *kn, const char *name)
446457
{

0 commit comments

Comments
 (0)