d_path: lift -ENAMETOOLONG handling into callers of prepend_path()

The only negative value ever returned by prepend_path() is -ENAMETOOLONG
and callers can recognize that situation (overflow) by looking at the
sign of buflen.  Lift that into the callers; we already have the
same logics (buf if buflen is non-negative, ERR_PTR(-ENAMETOOLONG) otherwise)
in several places and that'll become a new primitive several commits down
the road.

Make prepend_path() return 0 instead of -ENAMETOOLONG.  That makes for
saner calling conventions (0/1/2/3/-ENAMETOOLONG is obnoxious) and
callers actually get simpler, especially once the aforementioned
primitive gets added.

In prepend_path() itself we switch prepending the / (in case of
empty path) to use of prepend() - no need to open-code that, compiler
will do the right thing.  It's exactly the same logics as in
__dentry_path().

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
This commit is contained in:
Al Viro 2021-05-17 22:29:03 -04:00
Родитель d8548232ea
Коммит 01a4428ee7
1 изменённых файлов: 11 добавлений и 28 удалений

Просмотреть файл

@ -127,8 +127,7 @@ restart:
} }
parent = dentry->d_parent; parent = dentry->d_parent;
prefetch(parent); prefetch(parent);
error = prepend_name(&bptr, &blen, &dentry->d_name); if (unlikely(prepend_name(&bptr, &blen, &dentry->d_name) < 0))
if (error)
break; break;
dentry = parent; dentry = parent;
@ -149,12 +148,9 @@ restart:
} }
done_seqretry(&mount_lock, m_seq); done_seqretry(&mount_lock, m_seq);
if (error >= 0 && bptr == *buffer) { if (blen == *buflen)
if (--blen < 0) prepend(&bptr, &blen, "/", 1);
error = -ENAMETOOLONG;
else
*--bptr = '/';
}
*buffer = bptr; *buffer = bptr;
*buflen = blen; *buflen = blen;
return error; return error;
@ -181,16 +177,11 @@ char *__d_path(const struct path *path,
char *buf, int buflen) char *buf, int buflen)
{ {
char *res = buf + buflen; char *res = buf + buflen;
int error;
prepend(&res, &buflen, "", 1); prepend(&res, &buflen, "", 1);
error = prepend_path(path, root, &res, &buflen); if (prepend_path(path, root, &res, &buflen) > 0)
if (error < 0)
return ERR_PTR(error);
if (error > 0)
return NULL; return NULL;
return res; return buflen >= 0 ? res : ERR_PTR(-ENAMETOOLONG);
} }
char *d_absolute_path(const struct path *path, char *d_absolute_path(const struct path *path,
@ -198,16 +189,11 @@ char *d_absolute_path(const struct path *path,
{ {
struct path root = {}; struct path root = {};
char *res = buf + buflen; char *res = buf + buflen;
int error;
prepend(&res, &buflen, "", 1); prepend(&res, &buflen, "", 1);
error = prepend_path(path, &root, &res, &buflen); if (prepend_path(path, &root, &res, &buflen) > 1)
return ERR_PTR(-EINVAL);
if (error > 1) return buflen >= 0 ? res : ERR_PTR(-ENAMETOOLONG);
error = -EINVAL;
if (error < 0)
return ERR_PTR(error);
return res;
} }
static void get_fs_root_rcu(struct fs_struct *fs, struct path *root) static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
@ -240,7 +226,6 @@ char *d_path(const struct path *path, char *buf, int buflen)
{ {
char *res = buf + buflen; char *res = buf + buflen;
struct path root; struct path root;
int error;
/* /*
* We have various synthetic filesystems that never get mounted. On * We have various synthetic filesystems that never get mounted. On
@ -263,12 +248,10 @@ char *d_path(const struct path *path, char *buf, int buflen)
prepend(&res, &buflen, " (deleted)", 11); prepend(&res, &buflen, " (deleted)", 11);
else else
prepend(&res, &buflen, "", 1); prepend(&res, &buflen, "", 1);
error = prepend_path(path, &root, &res, &buflen); prepend_path(path, &root, &res, &buflen);
rcu_read_unlock(); rcu_read_unlock();
if (error < 0) return buflen >= 0 ? res : ERR_PTR(-ENAMETOOLONG);
res = ERR_PTR(error);
return res;
} }
EXPORT_SYMBOL(d_path); EXPORT_SYMBOL(d_path);