Skip to content
This repository has been archived by the owner on Feb 26, 2020. It is now read-only.

Introduced spl_dirname function. #649

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion module/spl/spl-vnode.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,56 @@ spl_basename(const char *s, const char **str, int *len)
*len = end + 1;
}

/*
* spl_dirname() takes a NULL-terminated string s as input containing a path.
* Returns pointer to a string containing the directory name or NULL
* on allocation failure. Returned pointer must be freed after use.
*/

static char *
spl_dirname(const char *name)
{
size_t i;

char *dirname = kmalloc(strlen(name), kmem_flags_convert(KM_SLEEP));
if (!dirname)
return dirname;

if (!name || !*name) {
dirname[0] = '.';
dirname[1] = '\0';
return dirname;
}

i = strlen(name) - 1;

while (i && name[i--] != '/');

if (i == 0) {
dirname[0] = '/';
dirname[1] = '\0';
return dirname;
}

++i;

dirname[i] = '\0';
while(i) {
--i;
dirname[i] = name[i];
}

return dirname;
}


static struct dentry *
spl_kern_path_locked(const char *name, struct path *path)
{
struct path parent;
struct dentry *dentry;
const char *basename;
char *dirname;
int len;
int rc;

Expand All @@ -347,10 +391,16 @@ spl_kern_path_locked(const char *name, struct path *path)
if (len == 1 || basename[1] == '.')
return (ERR_PTR(-EACCES));

rc = kern_path(name, LOOKUP_PARENT, &parent);
dirname = spl_dirname(name);
if (!dirname)
return (ERR_PTR(-ENOMEM));

rc = kern_path(dirname, LOOKUP_DIR, &parent);
if (rc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dirname will be leaked in this error path.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. You're right the kfree should be put before the if condition.

return (ERR_PTR(rc));

kfree(dirname);

/* use I_MUTEX_PARENT because vfs_unlink needs it */
spl_inode_lock_nested(parent.dentry->d_inode, I_MUTEX_PARENT);

Expand Down