xref: /openbmc/linux/fs/d_path.c (revision df67cb4c)
17a5cf791SAl Viro /* SPDX-License-Identifier: GPL-2.0 */
27a5cf791SAl Viro #include <linux/syscalls.h>
37a5cf791SAl Viro #include <linux/export.h>
47a5cf791SAl Viro #include <linux/uaccess.h>
57a5cf791SAl Viro #include <linux/fs_struct.h>
67a5cf791SAl Viro #include <linux/fs.h>
77a5cf791SAl Viro #include <linux/slab.h>
87a5cf791SAl Viro #include <linux/prefetch.h>
97a5cf791SAl Viro #include "mount.h"
10*df67cb4cSArnd Bergmann #include "internal.h"
117a5cf791SAl Viro 
12ad08ae58SAl Viro struct prepend_buffer {
13ad08ae58SAl Viro 	char *buf;
14ad08ae58SAl Viro 	int len;
15ad08ae58SAl Viro };
16ad08ae58SAl Viro #define DECLARE_BUFFER(__name, __buf, __len) \
17ad08ae58SAl Viro 	struct prepend_buffer __name = {.buf = __buf + __len, .len = __len}
18ad08ae58SAl Viro 
extract_string(struct prepend_buffer * p)19ad08ae58SAl Viro static char *extract_string(struct prepend_buffer *p)
207a5cf791SAl Viro {
21ad08ae58SAl Viro 	if (likely(p->len >= 0))
22ad08ae58SAl Viro 		return p->buf;
23ad08ae58SAl Viro 	return ERR_PTR(-ENAMETOOLONG);
24ad08ae58SAl Viro }
25ad08ae58SAl Viro 
prepend_char(struct prepend_buffer * p,unsigned char c)26b0cfcdd9SLinus Torvalds static bool prepend_char(struct prepend_buffer *p, unsigned char c)
27ad08ae58SAl Viro {
28b0cfcdd9SLinus Torvalds 	if (likely(p->len > 0)) {
29b0cfcdd9SLinus Torvalds 		p->len--;
30b0cfcdd9SLinus Torvalds 		*--p->buf = c;
31b0cfcdd9SLinus Torvalds 		return true;
32d8548232SAl Viro 	}
33b0cfcdd9SLinus Torvalds 	p->len = -1;
34b0cfcdd9SLinus Torvalds 	return false;
35b0cfcdd9SLinus Torvalds }
36b0cfcdd9SLinus Torvalds 
37b0cfcdd9SLinus Torvalds /*
38c4c84511SAl Viro  * The source of the prepend data can be an optimistic load
39b0cfcdd9SLinus Torvalds  * of a dentry name and length. And because we don't hold any
40b0cfcdd9SLinus Torvalds  * locks, the length and the pointer to the name may not be
41b0cfcdd9SLinus Torvalds  * in sync if a concurrent rename happens, and the kernel
42b0cfcdd9SLinus Torvalds  * copy might fault as a result.
43b0cfcdd9SLinus Torvalds  *
44b0cfcdd9SLinus Torvalds  * The end result will correct itself when we check the
45b0cfcdd9SLinus Torvalds  * rename sequence count, but we need to be able to handle
46b0cfcdd9SLinus Torvalds  * the fault gracefully.
47b0cfcdd9SLinus Torvalds  */
prepend_copy(void * dst,const void * src,int len)48b0cfcdd9SLinus Torvalds static bool prepend_copy(void *dst, const void *src, int len)
49b0cfcdd9SLinus Torvalds {
50b0cfcdd9SLinus Torvalds 	if (unlikely(copy_from_kernel_nofault(dst, src, len))) {
51b0cfcdd9SLinus Torvalds 		memset(dst, 'x', len);
52b0cfcdd9SLinus Torvalds 		return false;
53b0cfcdd9SLinus Torvalds 	}
54b0cfcdd9SLinus Torvalds 	return true;
55b0cfcdd9SLinus Torvalds }
56b0cfcdd9SLinus Torvalds 
prepend(struct prepend_buffer * p,const char * str,int namelen)57b0cfcdd9SLinus Torvalds static bool prepend(struct prepend_buffer *p, const char *str, int namelen)
58b0cfcdd9SLinus Torvalds {
59b0cfcdd9SLinus Torvalds 	// Already overflowed?
60b0cfcdd9SLinus Torvalds 	if (p->len < 0)
61b0cfcdd9SLinus Torvalds 		return false;
62b0cfcdd9SLinus Torvalds 
63b0cfcdd9SLinus Torvalds 	// Will overflow?
64b0cfcdd9SLinus Torvalds 	if (p->len < namelen) {
65b0cfcdd9SLinus Torvalds 		// Fill as much as possible from the end of the name
66b0cfcdd9SLinus Torvalds 		str += namelen - p->len;
67b0cfcdd9SLinus Torvalds 		p->buf -= p->len;
68b0cfcdd9SLinus Torvalds 		prepend_copy(p->buf, str, p->len);
69b0cfcdd9SLinus Torvalds 		p->len = -1;
70b0cfcdd9SLinus Torvalds 		return false;
71b0cfcdd9SLinus Torvalds 	}
72b0cfcdd9SLinus Torvalds 
73b0cfcdd9SLinus Torvalds 	// Fits fully
74b0cfcdd9SLinus Torvalds 	p->len -= namelen;
75b0cfcdd9SLinus Torvalds 	p->buf -= namelen;
76b0cfcdd9SLinus Torvalds 	return prepend_copy(p->buf, str, namelen);
777a5cf791SAl Viro }
787a5cf791SAl Viro 
797a5cf791SAl Viro /**
807a5cf791SAl Viro  * prepend_name - prepend a pathname in front of current buffer pointer
81d41b6035SJia He  * @p: prepend buffer which contains buffer pointer and allocated length
827a5cf791SAl Viro  * @name: name string and length qstr structure
837a5cf791SAl Viro  *
847a5cf791SAl Viro  * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to
857a5cf791SAl Viro  * make sure that either the old or the new name pointer and length are
867a5cf791SAl Viro  * fetched. However, there may be mismatch between length and pointer.
87b0cfcdd9SLinus Torvalds  * But since the length cannot be trusted, we need to copy the name very
88b0cfcdd9SLinus Torvalds  * carefully when doing the prepend_copy(). It also prepends "/" at
897a5cf791SAl Viro  * the beginning of the name. The sequence number check at the caller will
907a5cf791SAl Viro  * retry it again when a d_move() does happen. So any garbage in the buffer
917a5cf791SAl Viro  * due to mismatched pointer and length will be discarded.
927a5cf791SAl Viro  *
93b0cfcdd9SLinus Torvalds  * Load acquire is needed to make sure that we see the new name data even
94b0cfcdd9SLinus Torvalds  * if we might get the length wrong.
957a5cf791SAl Viro  */
prepend_name(struct prepend_buffer * p,const struct qstr * name)96ad08ae58SAl Viro static bool prepend_name(struct prepend_buffer *p, const struct qstr *name)
977a5cf791SAl Viro {
987a5cf791SAl Viro 	const char *dname = smp_load_acquire(&name->name); /* ^^^ */
997a5cf791SAl Viro 	u32 dlen = READ_ONCE(name->len);
1007a5cf791SAl Viro 
101b0cfcdd9SLinus Torvalds 	return prepend(p, dname, dlen) && prepend_char(p, '/');
1027a5cf791SAl Viro }
1037a5cf791SAl Viro 
__prepend_path(const struct dentry * dentry,const struct mount * mnt,const struct path * root,struct prepend_buffer * p)104008673ffSAl Viro static int __prepend_path(const struct dentry *dentry, const struct mount *mnt,
105008673ffSAl Viro 			  const struct path *root, struct prepend_buffer *p)
106008673ffSAl Viro {
107008673ffSAl Viro 	while (dentry != root->dentry || &mnt->mnt != root->mnt) {
108008673ffSAl Viro 		const struct dentry *parent = READ_ONCE(dentry->d_parent);
109008673ffSAl Viro 
110008673ffSAl Viro 		if (dentry == mnt->mnt.mnt_root) {
111008673ffSAl Viro 			struct mount *m = READ_ONCE(mnt->mnt_parent);
112008673ffSAl Viro 			struct mnt_namespace *mnt_ns;
113008673ffSAl Viro 
114008673ffSAl Viro 			if (likely(mnt != m)) {
115008673ffSAl Viro 				dentry = READ_ONCE(mnt->mnt_mountpoint);
116008673ffSAl Viro 				mnt = m;
117008673ffSAl Viro 				continue;
118008673ffSAl Viro 			}
119008673ffSAl Viro 			/* Global root */
120008673ffSAl Viro 			mnt_ns = READ_ONCE(mnt->mnt_ns);
121008673ffSAl Viro 			/* open-coded is_mounted() to use local mnt_ns */
122008673ffSAl Viro 			if (!IS_ERR_OR_NULL(mnt_ns) && !is_anon_ns(mnt_ns))
123008673ffSAl Viro 				return 1;	// absolute root
124008673ffSAl Viro 			else
125008673ffSAl Viro 				return 2;	// detached or not attached yet
126008673ffSAl Viro 		}
127008673ffSAl Viro 
128008673ffSAl Viro 		if (unlikely(dentry == parent))
129008673ffSAl Viro 			/* Escaped? */
130008673ffSAl Viro 			return 3;
131008673ffSAl Viro 
132008673ffSAl Viro 		prefetch(parent);
133008673ffSAl Viro 		if (!prepend_name(p, &dentry->d_name))
134008673ffSAl Viro 			break;
135008673ffSAl Viro 		dentry = parent;
136008673ffSAl Viro 	}
137008673ffSAl Viro 	return 0;
138008673ffSAl Viro }
139008673ffSAl Viro 
1407a5cf791SAl Viro /**
1417a5cf791SAl Viro  * prepend_path - Prepend path string to a buffer
1427a5cf791SAl Viro  * @path: the dentry/vfsmount to report
1437a5cf791SAl Viro  * @root: root vfsmnt/dentry
144d41b6035SJia He  * @p: prepend buffer which contains buffer pointer and allocated length
1457a5cf791SAl Viro  *
1467a5cf791SAl Viro  * The function will first try to write out the pathname without taking any
1477a5cf791SAl Viro  * lock other than the RCU read lock to make sure that dentries won't go away.
1487a5cf791SAl Viro  * It only checks the sequence number of the global rename_lock as any change
1497a5cf791SAl Viro  * in the dentry's d_seq will be preceded by changes in the rename_lock
1507a5cf791SAl Viro  * sequence number. If the sequence number had been changed, it will restart
1517a5cf791SAl Viro  * the whole pathname back-tracing sequence again by taking the rename_lock.
1527a5cf791SAl Viro  * In this case, there is no need to take the RCU read lock as the recursive
1537a5cf791SAl Viro  * parent pointer references will keep the dentry chain alive as long as no
1547a5cf791SAl Viro  * rename operation is performed.
1557a5cf791SAl Viro  */
prepend_path(const struct path * path,const struct path * root,struct prepend_buffer * p)1567a5cf791SAl Viro static int prepend_path(const struct path *path,
1577a5cf791SAl Viro 			const struct path *root,
158ad08ae58SAl Viro 			struct prepend_buffer *p)
1597a5cf791SAl Viro {
1607a5cf791SAl Viro 	unsigned seq, m_seq = 0;
161ad08ae58SAl Viro 	struct prepend_buffer b;
162008673ffSAl Viro 	int error;
1637a5cf791SAl Viro 
1647a5cf791SAl Viro 	rcu_read_lock();
1657a5cf791SAl Viro restart_mnt:
1667a5cf791SAl Viro 	read_seqbegin_or_lock(&mount_lock, &m_seq);
1677a5cf791SAl Viro 	seq = 0;
1687a5cf791SAl Viro 	rcu_read_lock();
1697a5cf791SAl Viro restart:
170ad08ae58SAl Viro 	b = *p;
1717a5cf791SAl Viro 	read_seqbegin_or_lock(&rename_lock, &seq);
172008673ffSAl Viro 	error = __prepend_path(path->dentry, real_mount(path->mnt), root, &b);
1737a5cf791SAl Viro 	if (!(seq & 1))
1747a5cf791SAl Viro 		rcu_read_unlock();
1757a5cf791SAl Viro 	if (need_seqretry(&rename_lock, seq)) {
1767a5cf791SAl Viro 		seq = 1;
1777a5cf791SAl Viro 		goto restart;
1787a5cf791SAl Viro 	}
1797a5cf791SAl Viro 	done_seqretry(&rename_lock, seq);
1807a5cf791SAl Viro 
1817a5cf791SAl Viro 	if (!(m_seq & 1))
1827a5cf791SAl Viro 		rcu_read_unlock();
1837a5cf791SAl Viro 	if (need_seqretry(&mount_lock, m_seq)) {
1847a5cf791SAl Viro 		m_seq = 1;
1857a5cf791SAl Viro 		goto restart_mnt;
1867a5cf791SAl Viro 	}
1877a5cf791SAl Viro 	done_seqretry(&mount_lock, m_seq);
1887a5cf791SAl Viro 
1892dac0ad1SAl Viro 	if (unlikely(error == 3))
1902dac0ad1SAl Viro 		b = *p;
1912dac0ad1SAl Viro 
192ad08ae58SAl Viro 	if (b.len == p->len)
193b0cfcdd9SLinus Torvalds 		prepend_char(&b, '/');
19401a4428eSAl Viro 
195ad08ae58SAl Viro 	*p = b;
1967a5cf791SAl Viro 	return error;
1977a5cf791SAl Viro }
1987a5cf791SAl Viro 
1997a5cf791SAl Viro /**
2007a5cf791SAl Viro  * __d_path - return the path of a dentry
2017a5cf791SAl Viro  * @path: the dentry/vfsmount to report
2027a5cf791SAl Viro  * @root: root vfsmnt/dentry
2037a5cf791SAl Viro  * @buf: buffer to return value in
2047a5cf791SAl Viro  * @buflen: buffer length
2057a5cf791SAl Viro  *
2067a5cf791SAl Viro  * Convert a dentry into an ASCII path name.
2077a5cf791SAl Viro  *
2087a5cf791SAl Viro  * Returns a pointer into the buffer or an error code if the
2097a5cf791SAl Viro  * path was too long.
2107a5cf791SAl Viro  *
2117a5cf791SAl Viro  * "buflen" should be positive.
2127a5cf791SAl Viro  *
2137a5cf791SAl Viro  * If the path is not reachable from the supplied root, return %NULL.
2147a5cf791SAl Viro  */
__d_path(const struct path * path,const struct path * root,char * buf,int buflen)2157a5cf791SAl Viro char *__d_path(const struct path *path,
2167a5cf791SAl Viro 	       const struct path *root,
2177a5cf791SAl Viro 	       char *buf, int buflen)
2187a5cf791SAl Viro {
219ad08ae58SAl Viro 	DECLARE_BUFFER(b, buf, buflen);
2207a5cf791SAl Viro 
221b0cfcdd9SLinus Torvalds 	prepend_char(&b, 0);
222cf4febc1SAl Viro 	if (unlikely(prepend_path(path, root, &b) > 0))
2237a5cf791SAl Viro 		return NULL;
224ad08ae58SAl Viro 	return extract_string(&b);
2257a5cf791SAl Viro }
2267a5cf791SAl Viro 
d_absolute_path(const struct path * path,char * buf,int buflen)2277a5cf791SAl Viro char *d_absolute_path(const struct path *path,
2287a5cf791SAl Viro 	       char *buf, int buflen)
2297a5cf791SAl Viro {
2307a5cf791SAl Viro 	struct path root = {};
231ad08ae58SAl Viro 	DECLARE_BUFFER(b, buf, buflen);
2327a5cf791SAl Viro 
233b0cfcdd9SLinus Torvalds 	prepend_char(&b, 0);
234cf4febc1SAl Viro 	if (unlikely(prepend_path(path, &root, &b) > 1))
23501a4428eSAl Viro 		return ERR_PTR(-EINVAL);
236ad08ae58SAl Viro 	return extract_string(&b);
2377a5cf791SAl Viro }
2387a5cf791SAl Viro 
get_fs_root_rcu(struct fs_struct * fs,struct path * root)2397a5cf791SAl Viro static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
2407a5cf791SAl Viro {
2417a5cf791SAl Viro 	unsigned seq;
2427a5cf791SAl Viro 
2437a5cf791SAl Viro 	do {
2447a5cf791SAl Viro 		seq = read_seqcount_begin(&fs->seq);
2457a5cf791SAl Viro 		*root = fs->root;
2467a5cf791SAl Viro 	} while (read_seqcount_retry(&fs->seq, seq));
2477a5cf791SAl Viro }
2487a5cf791SAl Viro 
2497a5cf791SAl Viro /**
2507a5cf791SAl Viro  * d_path - return the path of a dentry
2517a5cf791SAl Viro  * @path: path to report
2527a5cf791SAl Viro  * @buf: buffer to return value in
2537a5cf791SAl Viro  * @buflen: buffer length
2547a5cf791SAl Viro  *
2557a5cf791SAl Viro  * Convert a dentry into an ASCII path name. If the entry has been deleted
2567a5cf791SAl Viro  * the string " (deleted)" is appended. Note that this is ambiguous.
2577a5cf791SAl Viro  *
2587a5cf791SAl Viro  * Returns a pointer into the buffer or an error code if the path was
2597a5cf791SAl Viro  * too long. Note: Callers should use the returned pointer, not the passed
2607a5cf791SAl Viro  * in buffer, to use the name! The implementation often starts at an offset
2617a5cf791SAl Viro  * into the buffer, and may leave 0 bytes at the start.
2627a5cf791SAl Viro  *
2637a5cf791SAl Viro  * "buflen" should be positive.
2647a5cf791SAl Viro  */
d_path(const struct path * path,char * buf,int buflen)2657a5cf791SAl Viro char *d_path(const struct path *path, char *buf, int buflen)
2667a5cf791SAl Viro {
267ad08ae58SAl Viro 	DECLARE_BUFFER(b, buf, buflen);
2687a5cf791SAl Viro 	struct path root;
2697a5cf791SAl Viro 
2707a5cf791SAl Viro 	/*
2717a5cf791SAl Viro 	 * We have various synthetic filesystems that never get mounted.  On
2727a5cf791SAl Viro 	 * these filesystems dentries are never used for lookup purposes, and
2737a5cf791SAl Viro 	 * thus don't need to be hashed.  They also don't need a name until a
2747a5cf791SAl Viro 	 * user wants to identify the object in /proc/pid/fd/.  The little hack
2757a5cf791SAl Viro 	 * below allows us to generate a name for these objects on demand:
2767a5cf791SAl Viro 	 *
2777a5cf791SAl Viro 	 * Some pseudo inodes are mountable.  When they are mounted
2787a5cf791SAl Viro 	 * path->dentry == path->mnt->mnt_root.  In that case don't call d_dname
2797a5cf791SAl Viro 	 * and instead have d_path return the mounted path.
2807a5cf791SAl Viro 	 */
2817a5cf791SAl Viro 	if (path->dentry->d_op && path->dentry->d_op->d_dname &&
2827a5cf791SAl Viro 	    (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
2837a5cf791SAl Viro 		return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2847a5cf791SAl Viro 
2857a5cf791SAl Viro 	rcu_read_lock();
2867a5cf791SAl Viro 	get_fs_root_rcu(current->fs, &root);
2879024348fSAl Viro 	if (unlikely(d_unlinked(path->dentry)))
288ad08ae58SAl Viro 		prepend(&b, " (deleted)", 11);
2899024348fSAl Viro 	else
290b0cfcdd9SLinus Torvalds 		prepend_char(&b, 0);
291ad08ae58SAl Viro 	prepend_path(path, &root, &b);
2927a5cf791SAl Viro 	rcu_read_unlock();
2937a5cf791SAl Viro 
294ad08ae58SAl Viro 	return extract_string(&b);
2957a5cf791SAl Viro }
2967a5cf791SAl Viro EXPORT_SYMBOL(d_path);
2977a5cf791SAl Viro 
2987a5cf791SAl Viro /*
2997a5cf791SAl Viro  * Helper function for dentry_operations.d_dname() members
3007a5cf791SAl Viro  */
dynamic_dname(char * buffer,int buflen,const char * fmt,...)3010f60d288SAl Viro char *dynamic_dname(char *buffer, int buflen, const char *fmt, ...)
3027a5cf791SAl Viro {
3037a5cf791SAl Viro 	va_list args;
3047a5cf791SAl Viro 	char temp[64];
3057a5cf791SAl Viro 	int sz;
3067a5cf791SAl Viro 
3077a5cf791SAl Viro 	va_start(args, fmt);
3087a5cf791SAl Viro 	sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
3097a5cf791SAl Viro 	va_end(args);
3107a5cf791SAl Viro 
3117a5cf791SAl Viro 	if (sz > sizeof(temp) || sz > buflen)
3127a5cf791SAl Viro 		return ERR_PTR(-ENAMETOOLONG);
3137a5cf791SAl Viro 
3147a5cf791SAl Viro 	buffer += buflen - sz;
3157a5cf791SAl Viro 	return memcpy(buffer, temp, sz);
3167a5cf791SAl Viro }
3177a5cf791SAl Viro 
simple_dname(struct dentry * dentry,char * buffer,int buflen)3187a5cf791SAl Viro char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
3197a5cf791SAl Viro {
320ad08ae58SAl Viro 	DECLARE_BUFFER(b, buffer, buflen);
3217a5cf791SAl Viro 	/* these dentries are never renamed, so d_lock is not needed */
322ad08ae58SAl Viro 	prepend(&b, " (deleted)", 11);
323ad08ae58SAl Viro 	prepend(&b, dentry->d_name.name, dentry->d_name.len);
324b0cfcdd9SLinus Torvalds 	prepend_char(&b, '/');
325ad08ae58SAl Viro 	return extract_string(&b);
3267a5cf791SAl Viro }
3277a5cf791SAl Viro 
3287a5cf791SAl Viro /*
3297a5cf791SAl Viro  * Write full pathname from the root of the filesystem into the buffer.
3307a5cf791SAl Viro  */
__dentry_path(const struct dentry * d,struct prepend_buffer * p)331ad08ae58SAl Viro static char *__dentry_path(const struct dentry *d, struct prepend_buffer *p)
3327a5cf791SAl Viro {
333a2bbe664SAl Viro 	const struct dentry *dentry;
334ad08ae58SAl Viro 	struct prepend_buffer b;
335ad08ae58SAl Viro 	int seq = 0;
3367a5cf791SAl Viro 
3377a5cf791SAl Viro 	rcu_read_lock();
3387a5cf791SAl Viro restart:
3397a5cf791SAl Viro 	dentry = d;
340ad08ae58SAl Viro 	b = *p;
3417a5cf791SAl Viro 	read_seqbegin_or_lock(&rename_lock, &seq);
3427a5cf791SAl Viro 	while (!IS_ROOT(dentry)) {
343a2bbe664SAl Viro 		const struct dentry *parent = dentry->d_parent;
3447a5cf791SAl Viro 
3457a5cf791SAl Viro 		prefetch(parent);
346ad08ae58SAl Viro 		if (!prepend_name(&b, &dentry->d_name))
3477a5cf791SAl Viro 			break;
3487a5cf791SAl Viro 		dentry = parent;
3497a5cf791SAl Viro 	}
3507a5cf791SAl Viro 	if (!(seq & 1))
3517a5cf791SAl Viro 		rcu_read_unlock();
3527a5cf791SAl Viro 	if (need_seqretry(&rename_lock, seq)) {
3537a5cf791SAl Viro 		seq = 1;
3547a5cf791SAl Viro 		goto restart;
3557a5cf791SAl Viro 	}
3567a5cf791SAl Viro 	done_seqretry(&rename_lock, seq);
357ad08ae58SAl Viro 	if (b.len == p->len)
358b0cfcdd9SLinus Torvalds 		prepend_char(&b, '/');
359ad08ae58SAl Viro 	return extract_string(&b);
3607a5cf791SAl Viro }
3617a5cf791SAl Viro 
dentry_path_raw(const struct dentry * dentry,char * buf,int buflen)362a2bbe664SAl Viro char *dentry_path_raw(const struct dentry *dentry, char *buf, int buflen)
3637a5cf791SAl Viro {
364ad08ae58SAl Viro 	DECLARE_BUFFER(b, buf, buflen);
365ad08ae58SAl Viro 
366b0cfcdd9SLinus Torvalds 	prepend_char(&b, 0);
367ad08ae58SAl Viro 	return __dentry_path(dentry, &b);
3687a5cf791SAl Viro }
3697a5cf791SAl Viro EXPORT_SYMBOL(dentry_path_raw);
3707a5cf791SAl Viro 
dentry_path(const struct dentry * dentry,char * buf,int buflen)371a2bbe664SAl Viro char *dentry_path(const struct dentry *dentry, char *buf, int buflen)
3727a5cf791SAl Viro {
373ad08ae58SAl Viro 	DECLARE_BUFFER(b, buf, buflen);
3747a5cf791SAl Viro 
3753a291c97SAl Viro 	if (unlikely(d_unlinked(dentry)))
376ad08ae58SAl Viro 		prepend(&b, "//deleted", 10);
3773a291c97SAl Viro 	else
378b0cfcdd9SLinus Torvalds 		prepend_char(&b, 0);
379ad08ae58SAl Viro 	return __dentry_path(dentry, &b);
3807a5cf791SAl Viro }
3817a5cf791SAl Viro 
get_fs_root_and_pwd_rcu(struct fs_struct * fs,struct path * root,struct path * pwd)3827a5cf791SAl Viro static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
3837a5cf791SAl Viro 				    struct path *pwd)
3847a5cf791SAl Viro {
3857a5cf791SAl Viro 	unsigned seq;
3867a5cf791SAl Viro 
3877a5cf791SAl Viro 	do {
3887a5cf791SAl Viro 		seq = read_seqcount_begin(&fs->seq);
3897a5cf791SAl Viro 		*root = fs->root;
3907a5cf791SAl Viro 		*pwd = fs->pwd;
3917a5cf791SAl Viro 	} while (read_seqcount_retry(&fs->seq, seq));
3927a5cf791SAl Viro }
3937a5cf791SAl Viro 
3947a5cf791SAl Viro /*
3957a5cf791SAl Viro  * NOTE! The user-level library version returns a
3967a5cf791SAl Viro  * character pointer. The kernel system call just
3977a5cf791SAl Viro  * returns the length of the buffer filled (which
3987a5cf791SAl Viro  * includes the ending '\0' character), or a negative
3997a5cf791SAl Viro  * error value. So libc would do something like
4007a5cf791SAl Viro  *
4017a5cf791SAl Viro  *	char *getcwd(char * buf, size_t size)
4027a5cf791SAl Viro  *	{
4037a5cf791SAl Viro  *		int retval;
4047a5cf791SAl Viro  *
4057a5cf791SAl Viro  *		retval = sys_getcwd(buf, size);
4067a5cf791SAl Viro  *		if (retval >= 0)
4077a5cf791SAl Viro  *			return buf;
4087a5cf791SAl Viro  *		errno = -retval;
4097a5cf791SAl Viro  *		return NULL;
4107a5cf791SAl Viro  *	}
4117a5cf791SAl Viro  */
SYSCALL_DEFINE2(getcwd,char __user *,buf,unsigned long,size)4127a5cf791SAl Viro SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
4137a5cf791SAl Viro {
4147a5cf791SAl Viro 	int error;
4157a5cf791SAl Viro 	struct path pwd, root;
4167a5cf791SAl Viro 	char *page = __getname();
4177a5cf791SAl Viro 
4187a5cf791SAl Viro 	if (!page)
4197a5cf791SAl Viro 		return -ENOMEM;
4207a5cf791SAl Viro 
4217a5cf791SAl Viro 	rcu_read_lock();
4227a5cf791SAl Viro 	get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
4237a5cf791SAl Viro 
424e4b27553SAl Viro 	if (unlikely(d_unlinked(pwd.dentry))) {
425e4b27553SAl Viro 		rcu_read_unlock();
4267a5cf791SAl Viro 		error = -ENOENT;
427e4b27553SAl Viro 	} else {
428e4b27553SAl Viro 		unsigned len;
429ad08ae58SAl Viro 		DECLARE_BUFFER(b, page, PATH_MAX);
4307a5cf791SAl Viro 
431b0cfcdd9SLinus Torvalds 		prepend_char(&b, 0);
432cf4febc1SAl Viro 		if (unlikely(prepend_path(&pwd, &root, &b) > 0))
433ad08ae58SAl Viro 			prepend(&b, "(unreachable)", 13);
4347a5cf791SAl Viro 		rcu_read_unlock();
4357a5cf791SAl Viro 
436ad08ae58SAl Viro 		len = PATH_MAX - b.len;
437e4b27553SAl Viro 		if (unlikely(len > PATH_MAX))
438e4b27553SAl Viro 			error = -ENAMETOOLONG;
439e4b27553SAl Viro 		else if (unlikely(len > size))
440e4b27553SAl Viro 			error = -ERANGE;
441e4b27553SAl Viro 		else if (copy_to_user(buf, b.buf, len))
4427a5cf791SAl Viro 			error = -EFAULT;
443e4b27553SAl Viro 		else
444e4b27553SAl Viro 			error = len;
4457a5cf791SAl Viro 	}
4467a5cf791SAl Viro 	__putname(page);
4477a5cf791SAl Viro 	return error;
4487a5cf791SAl Viro }
449