Skip to content

Commit 42c3732

Browse files
committed
fs: Create a generic is_dot_dotdot() utility
De-duplicate the same functionality in several places by hoisting the is_dot_dotdot() utility function into linux/fs.h. Suggested-by: Amir Goldstein <amir73il@gmail.com> Reviewed-by: Jeff Layton <jlayton@kernel.org> Reviewed-by: Amir Goldstein <amir73il@gmail.com> Acked-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
1 parent 9473c44 commit 42c3732

File tree

6 files changed

+14
-42
lines changed

6 files changed

+14
-42
lines changed

fs/crypto/fname.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,7 @@ struct fscrypt_nokey_name {
7474

7575
static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
7676
{
77-
if (str->len == 1 && str->name[0] == '.')
78-
return true;
79-
80-
if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
81-
return true;
82-
83-
return false;
77+
return is_dot_dotdot(str->name, str->len);
8478
}
8579

8680
/**

fs/ecryptfs/crypto.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,16 +1949,6 @@ int ecryptfs_encrypt_and_encode_filename(
19491949
return rc;
19501950
}
19511951

1952-
static bool is_dot_dotdot(const char *name, size_t name_size)
1953-
{
1954-
if (name_size == 1 && name[0] == '.')
1955-
return true;
1956-
else if (name_size == 2 && name[0] == '.' && name[1] == '.')
1957-
return true;
1958-
1959-
return false;
1960-
}
1961-
19621952
/**
19631953
* ecryptfs_decode_and_decrypt_filename - converts the encoded cipher text name to decoded plaintext
19641954
* @plaintext_name: The plaintext name

fs/exportfs/expfs.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,6 @@ struct getdents_callback {
244244
int sequence; /* sequence counter */
245245
};
246246

247-
/* Copied from lookup_one_common() */
248-
static inline bool is_dot_dotdot(const char *name, size_t len)
249-
{
250-
if (unlikely(name[0] == '.')) {
251-
if (len < 2 || (len == 2 && name[1] == '.'))
252-
return true;
253-
}
254-
return false;
255-
}
256-
257247
/*
258248
* A rather strange filldir function to capture
259249
* the name matching the specified inode number.

fs/f2fs/f2fs.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3368,17 +3368,6 @@ static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
33683368
return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
33693369
}
33703370

3371-
static inline bool is_dot_dotdot(const u8 *name, size_t len)
3372-
{
3373-
if (len == 1 && name[0] == '.')
3374-
return true;
3375-
3376-
if (len == 2 && name[0] == '.' && name[1] == '.')
3377-
return true;
3378-
3379-
return false;
3380-
}
3381-
33823371
static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
33833372
size_t size, gfp_t flags)
33843373
{

fs/namei.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,10 +2667,8 @@ static int lookup_one_common(struct mnt_idmap *idmap,
26672667
if (!len)
26682668
return -EACCES;
26692669

2670-
if (unlikely(name[0] == '.')) {
2671-
if (len < 2 || (len == 2 && name[1] == '.'))
2672-
return -EACCES;
2673-
}
2670+
if (is_dot_dotdot(name, len))
2671+
return -EACCES;
26742672

26752673
while (len--) {
26762674
unsigned int c = *(const unsigned char *)name++;

include/linux/fs.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,6 +2846,17 @@ extern bool path_is_under(const struct path *, const struct path *);
28462846

28472847
extern char *file_path(struct file *, char *, int);
28482848

2849+
/**
2850+
* is_dot_dotdot - returns true only if @name is "." or ".."
2851+
* @name: file name to check
2852+
* @len: length of file name, in bytes
2853+
*/
2854+
static inline bool is_dot_dotdot(const char *name, size_t len)
2855+
{
2856+
return len && unlikely(name[0] == '.') &&
2857+
(len == 1 || (len == 2 && name[1] == '.'));
2858+
}
2859+
28492860
#include <linux/err.h>
28502861

28512862
/* needed for stackable file system support */

0 commit comments

Comments
 (0)