1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/syscalls.h>
3 #include <linux/export.h>
4 #include <linux/uaccess.h>
5 #include <linux/fs_struct.h>
7 #include <linux/slab.h>
8 #include <linux/prefetch.h>
11 static int prepend(char **buffer
, int *buflen
, const char *str
, int namelen
)
17 memcpy(*buffer
, str
, namelen
);
22 * prepend_name - prepend a pathname in front of current buffer pointer
23 * @buffer: buffer pointer
24 * @buflen: allocated length of the buffer
25 * @name: name string and length qstr structure
27 * With RCU path tracing, it may race with d_move(). Use READ_ONCE() to
28 * make sure that either the old or the new name pointer and length are
29 * fetched. However, there may be mismatch between length and pointer.
30 * The length cannot be trusted, we need to copy it byte-by-byte until
31 * the length is reached or a null byte is found. It also prepends "/" at
32 * the beginning of the name. The sequence number check at the caller will
33 * retry it again when a d_move() does happen. So any garbage in the buffer
34 * due to mismatched pointer and length will be discarded.
36 * Load acquire is needed to make sure that we see that terminating NUL.
38 static int prepend_name(char **buffer
, int *buflen
, const struct qstr
*name
)
40 const char *dname
= smp_load_acquire(&name
->name
); /* ^^^ */
41 u32 dlen
= READ_ONCE(name
->len
);
47 p
= *buffer
-= dlen
+ 1;
59 * prepend_path - Prepend path string to a buffer
60 * @path: the dentry/vfsmount to report
61 * @root: root vfsmnt/dentry
62 * @buffer: pointer to the end of the buffer
63 * @buflen: pointer to buffer length
65 * The function will first try to write out the pathname without taking any
66 * lock other than the RCU read lock to make sure that dentries won't go away.
67 * It only checks the sequence number of the global rename_lock as any change
68 * in the dentry's d_seq will be preceded by changes in the rename_lock
69 * sequence number. If the sequence number had been changed, it will restart
70 * the whole pathname back-tracing sequence again by taking the rename_lock.
71 * In this case, there is no need to take the RCU read lock as the recursive
72 * parent pointer references will keep the dentry chain alive as long as no
73 * rename operation is performed.
75 static int prepend_path(const struct path
*path
,
76 const struct path
*root
,
77 char **buffer
, int *buflen
)
79 struct dentry
*dentry
;
80 struct vfsmount
*vfsmnt
;
83 unsigned seq
, m_seq
= 0;
89 read_seqbegin_or_lock(&mount_lock
, &m_seq
);
96 dentry
= path
->dentry
;
98 mnt
= real_mount(vfsmnt
);
99 read_seqbegin_or_lock(&rename_lock
, &seq
);
100 while (dentry
!= root
->dentry
|| vfsmnt
!= root
->mnt
) {
101 struct dentry
* parent
;
103 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
104 struct mount
*parent
= READ_ONCE(mnt
->mnt_parent
);
106 if (dentry
!= vfsmnt
->mnt_root
) {
114 dentry
= READ_ONCE(mnt
->mnt_mountpoint
);
120 error
= is_mounted(vfsmnt
) ? 1 : 2;
123 parent
= dentry
->d_parent
;
125 error
= prepend_name(&bptr
, &blen
, &dentry
->d_name
);
133 if (need_seqretry(&rename_lock
, seq
)) {
137 done_seqretry(&rename_lock
, seq
);
141 if (need_seqretry(&mount_lock
, m_seq
)) {
145 done_seqretry(&mount_lock
, m_seq
);
147 if (error
>= 0 && bptr
== *buffer
) {
149 error
= -ENAMETOOLONG
;
159 * __d_path - return the path of a dentry
160 * @path: the dentry/vfsmount to report
161 * @root: root vfsmnt/dentry
162 * @buf: buffer to return value in
163 * @buflen: buffer length
165 * Convert a dentry into an ASCII path name.
167 * Returns a pointer into the buffer or an error code if the
170 * "buflen" should be positive.
172 * If the path is not reachable from the supplied root, return %NULL.
174 char *__d_path(const struct path
*path
,
175 const struct path
*root
,
176 char *buf
, int buflen
)
178 char *res
= buf
+ buflen
;
181 prepend(&res
, &buflen
, "\0", 1);
182 error
= prepend_path(path
, root
, &res
, &buflen
);
185 return ERR_PTR(error
);
191 char *d_absolute_path(const struct path
*path
,
192 char *buf
, int buflen
)
194 struct path root
= {};
195 char *res
= buf
+ buflen
;
198 prepend(&res
, &buflen
, "\0", 1);
199 error
= prepend_path(path
, &root
, &res
, &buflen
);
204 return ERR_PTR(error
);
209 * same as __d_path but appends "(deleted)" for unlinked files.
211 static int path_with_deleted(const struct path
*path
,
212 const struct path
*root
,
213 char **buf
, int *buflen
)
215 prepend(buf
, buflen
, "\0", 1);
216 if (d_unlinked(path
->dentry
)) {
217 int error
= prepend(buf
, buflen
, " (deleted)", 10);
222 return prepend_path(path
, root
, buf
, buflen
);
225 static int prepend_unreachable(char **buffer
, int *buflen
)
227 return prepend(buffer
, buflen
, "(unreachable)", 13);
230 static void get_fs_root_rcu(struct fs_struct
*fs
, struct path
*root
)
235 seq
= read_seqcount_begin(&fs
->seq
);
237 } while (read_seqcount_retry(&fs
->seq
, seq
));
241 * d_path - return the path of a dentry
242 * @path: path to report
243 * @buf: buffer to return value in
244 * @buflen: buffer length
246 * Convert a dentry into an ASCII path name. If the entry has been deleted
247 * the string " (deleted)" is appended. Note that this is ambiguous.
249 * Returns a pointer into the buffer or an error code if the path was
250 * too long. Note: Callers should use the returned pointer, not the passed
251 * in buffer, to use the name! The implementation often starts at an offset
252 * into the buffer, and may leave 0 bytes at the start.
254 * "buflen" should be positive.
256 char *d_path(const struct path
*path
, char *buf
, int buflen
)
258 char *res
= buf
+ buflen
;
263 * We have various synthetic filesystems that never get mounted. On
264 * these filesystems dentries are never used for lookup purposes, and
265 * thus don't need to be hashed. They also don't need a name until a
266 * user wants to identify the object in /proc/pid/fd/. The little hack
267 * below allows us to generate a name for these objects on demand:
269 * Some pseudo inodes are mountable. When they are mounted
270 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
271 * and instead have d_path return the mounted path.
273 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
&&
274 (!IS_ROOT(path
->dentry
) || path
->dentry
!= path
->mnt
->mnt_root
))
275 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
278 get_fs_root_rcu(current
->fs
, &root
);
279 error
= path_with_deleted(path
, &root
, &res
, &buflen
);
283 res
= ERR_PTR(error
);
286 EXPORT_SYMBOL(d_path
);
289 * Helper function for dentry_operations.d_dname() members
291 char *dynamic_dname(struct dentry
*dentry
, char *buffer
, int buflen
,
292 const char *fmt
, ...)
299 sz
= vsnprintf(temp
, sizeof(temp
), fmt
, args
) + 1;
302 if (sz
> sizeof(temp
) || sz
> buflen
)
303 return ERR_PTR(-ENAMETOOLONG
);
305 buffer
+= buflen
- sz
;
306 return memcpy(buffer
, temp
, sz
);
309 char *simple_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
311 char *end
= buffer
+ buflen
;
312 /* these dentries are never renamed, so d_lock is not needed */
313 if (prepend(&end
, &buflen
, " (deleted)", 11) ||
314 prepend(&end
, &buflen
, dentry
->d_name
.name
, dentry
->d_name
.len
) ||
315 prepend(&end
, &buflen
, "/", 1))
316 end
= ERR_PTR(-ENAMETOOLONG
);
319 EXPORT_SYMBOL(simple_dname
);
322 * Write full pathname from the root of the filesystem into the buffer.
324 static char *__dentry_path(struct dentry
*d
, char *buf
, int buflen
)
326 struct dentry
*dentry
;
339 prepend(&end
, &len
, "\0", 1);
343 read_seqbegin_or_lock(&rename_lock
, &seq
);
344 while (!IS_ROOT(dentry
)) {
345 struct dentry
*parent
= dentry
->d_parent
;
348 error
= prepend_name(&end
, &len
, &dentry
->d_name
);
357 if (need_seqretry(&rename_lock
, seq
)) {
361 done_seqretry(&rename_lock
, seq
);
366 return ERR_PTR(-ENAMETOOLONG
);
369 char *dentry_path_raw(struct dentry
*dentry
, char *buf
, int buflen
)
371 return __dentry_path(dentry
, buf
, buflen
);
373 EXPORT_SYMBOL(dentry_path_raw
);
375 char *dentry_path(struct dentry
*dentry
, char *buf
, int buflen
)
380 if (d_unlinked(dentry
)) {
382 if (prepend(&p
, &buflen
, "//deleted", 10) != 0)
386 retval
= __dentry_path(dentry
, buf
, buflen
);
387 if (!IS_ERR(retval
) && p
)
388 *p
= '/'; /* restore '/' overriden with '\0' */
391 return ERR_PTR(-ENAMETOOLONG
);
394 static void get_fs_root_and_pwd_rcu(struct fs_struct
*fs
, struct path
*root
,
400 seq
= read_seqcount_begin(&fs
->seq
);
403 } while (read_seqcount_retry(&fs
->seq
, seq
));
407 * NOTE! The user-level library version returns a
408 * character pointer. The kernel system call just
409 * returns the length of the buffer filled (which
410 * includes the ending '\0' character), or a negative
411 * error value. So libc would do something like
413 * char *getcwd(char * buf, size_t size)
417 * retval = sys_getcwd(buf, size);
424 SYSCALL_DEFINE2(getcwd
, char __user
*, buf
, unsigned long, size
)
427 struct path pwd
, root
;
428 char *page
= __getname();
434 get_fs_root_and_pwd_rcu(current
->fs
, &root
, &pwd
);
437 if (!d_unlinked(pwd
.dentry
)) {
439 char *cwd
= page
+ PATH_MAX
;
440 int buflen
= PATH_MAX
;
442 prepend(&cwd
, &buflen
, "\0", 1);
443 error
= prepend_path(&pwd
, &root
, &cwd
, &buflen
);
449 /* Unreachable from current root */
451 error
= prepend_unreachable(&cwd
, &buflen
);
457 len
= PATH_MAX
+ page
- cwd
;
460 if (copy_to_user(buf
, cwd
, len
))