1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
6 * Directory handling functions for NTFS-based filesystems.
10 #include <linux/blkdev.h>
11 #include <linux/buffer_head.h>
13 #include <linux/iversion.h>
14 #include <linux/nls.h>
20 /* Convert little endian UTF-16 to NLS string. */
21 int ntfs_utf16_to_nls(struct ntfs_sb_info *sbi, const struct le_str *uni,
24 int ret, uni_len, warn;
27 struct nls_table *nls = sbi->options.nls;
29 static_assert(sizeof(wchar_t) == sizeof(__le16));
33 ret = utf16s_to_utf8s((wchar_t *)uni->name, uni->len,
34 UTF16_LITTLE_ENDIAN, buf, buf_len);
49 if (buf_len < NLS_MAX_CHARSET_SIZE) {
51 "filename was truncated while converting.");
55 ec = le16_to_cpu(*ip++);
56 charlen = nls->uni2char(ec, op, buf_len);
70 hex_byte_pack(&dump[0], ec >> 8);
71 hex_byte_pack(&dump[2], ec);
74 ntfs_err(sbi->sb, "failed to convert \"%s\" to %s", dump,
83 #define PLANE_SIZE 0x00010000
85 #define SURROGATE_PAIR 0x0000d800
86 #define SURROGATE_LOW 0x00000400
87 #define SURROGATE_BITS 0x000003ff
91 * put_utf16 - Modified version of put_utf16 from fs/nls/nls_base.c
93 * Function is sparse warnings free.
95 static inline void put_utf16(wchar_t *s, unsigned int c,
96 enum utf16_endian endian)
98 static_assert(sizeof(wchar_t) == sizeof(__le16));
99 static_assert(sizeof(wchar_t) == sizeof(__be16));
105 case UTF16_LITTLE_ENDIAN:
106 *(__le16 *)s = __cpu_to_le16(c);
108 case UTF16_BIG_ENDIAN:
109 *(__be16 *)s = __cpu_to_be16(c);
117 * Modified version of 'utf8s_to_utf16s' allows to
118 * detect -ENAMETOOLONG without writing out of expected maximum.
120 static int _utf8s_to_utf16s(const u8 *s, int inlen, enum utf16_endian endian,
121 wchar_t *pwcs, int maxout)
128 while (inlen > 0 && *s) {
130 size = utf8_to_utf32(s, inlen, &u);
136 if (u >= PLANE_SIZE) {
138 return -ENAMETOOLONG;
143 ((u >> 10) & SURROGATE_BITS),
146 SURROGATE_PAIR | SURROGATE_LOW |
147 (u & SURROGATE_BITS),
152 return -ENAMETOOLONG;
154 put_utf16(op++, u, endian);
159 return -ENAMETOOLONG;
161 put_utf16(op++, *s++, endian);
170 * ntfs_nls_to_utf16 - Convert input string to UTF-16.
172 * @name_len: Input name length.
173 * @uni: Destination memory.
174 * @max_ulen: Destination memory.
175 * @endian: Endian of target UTF-16 string.
177 * This function is called:
178 * - to create NTFS name
179 * - to create symlink
181 * Return: UTF-16 string length or error (if negative).
183 int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len,
184 struct cpu_str *uni, u32 max_ulen,
185 enum utf16_endian endian)
189 struct nls_table *nls = sbi->options.nls;
190 u16 *uname = uni->name;
192 static_assert(sizeof(wchar_t) == sizeof(u16));
196 ret = _utf8s_to_utf16s(name, name_len, endian, uname, max_ulen);
201 for (ret = 0, end = name + name_len; name < end; ret++, name += slen) {
203 return -ENAMETOOLONG;
205 slen = nls->char2uni(name, end - name, uname + ret);
213 if (endian == UTF16_LITTLE_ENDIAN) {
217 __cpu_to_le16s(uname);
222 if (endian == UTF16_BIG_ENDIAN) {
226 __cpu_to_be16s(uname);
237 * dir_search_u - Helper function.
239 struct inode *dir_search_u(struct inode *dir, const struct cpu_str *uni,
240 struct ntfs_fnd *fnd)
243 struct super_block *sb = dir->i_sb;
244 struct ntfs_sb_info *sbi = sb->s_fs_info;
245 struct ntfs_inode *ni = ntfs_i(dir);
248 struct inode *inode = NULL;
249 struct ntfs_fnd *fnd_a = NULL;
260 err = indx_find(&ni->dir, ni, NULL, uni, 0, sbi, &diff, &e, fnd);
270 inode = ntfs_iget5(sb, &e->ref, uni);
271 if (!IS_ERR(inode) && is_bad_inode(inode)) {
278 return err == -ENOENT ? NULL : err ? ERR_PTR(err) : inode;
281 static inline int ntfs_filldir(struct ntfs_sb_info *sbi, struct ntfs_inode *ni,
282 const struct NTFS_DE *e, u8 *name,
283 struct dir_context *ctx)
285 const struct ATTR_FILE_NAME *fname;
290 fname = Add2Ptr(e, sizeof(struct NTFS_DE));
292 if (fname->type == FILE_NAME_DOS)
295 if (!mi_is_ref(&ni->mi, &fname->home))
298 ino = ino_get(&e->ref);
300 if (ino == MFT_REC_ROOT)
303 /* Skip meta files. Unless option to show metafiles is set. */
304 if (!sbi->options.showmeta && ntfs_is_meta_file(sbi, ino))
307 if (sbi->options.nohidden && (fname->dup.fa & FILE_ATTRIBUTE_HIDDEN))
310 name_len = ntfs_utf16_to_nls(sbi, (struct le_str *)&fname->name_len,
313 ntfs_warn(sbi->sb, "failed to convert name for inode %lx.",
318 dt_type = (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG;
320 return !dir_emit(ctx, (s8 *)name, name_len, ino, dt_type);
324 * ntfs_read_hdr - Helper function for ntfs_readdir().
326 static int ntfs_read_hdr(struct ntfs_sb_info *sbi, struct ntfs_inode *ni,
327 const struct INDEX_HDR *hdr, u64 vbo, u64 pos,
328 u8 *name, struct dir_context *ctx)
331 const struct NTFS_DE *e;
333 u32 end = le32_to_cpu(hdr->used);
334 u32 off = le32_to_cpu(hdr->de_off);
336 for (;; off += e_size) {
337 if (off + sizeof(struct NTFS_DE) > end)
340 e = Add2Ptr(hdr, off);
341 e_size = le16_to_cpu(e->size);
342 if (e_size < sizeof(struct NTFS_DE) || off + e_size > end)
348 /* Skip already enumerated. */
352 if (le16_to_cpu(e->key_size) < SIZEOF_ATTRIBUTE_FILENAME)
355 ctx->pos = vbo + off;
357 /* Submit the name to the filldir callback. */
358 err = ntfs_filldir(sbi, ni, e, name, ctx);
365 * ntfs_readdir - file_operations::iterate_shared
367 * Use non sorted enumeration.
368 * We have an example of broken volume where sorted enumeration
369 * counts each name twice.
371 static int ntfs_readdir(struct file *file, struct dir_context *ctx)
373 const struct INDEX_ROOT *root;
378 struct inode *dir = file_inode(file);
379 struct ntfs_inode *ni = ntfs_i(dir);
380 struct super_block *sb = dir->i_sb;
381 struct ntfs_sb_info *sbi = sb->s_fs_info;
382 loff_t i_size = i_size_read(dir);
385 struct indx_node *node = NULL;
386 u8 index_bits = ni->dir.index_bits;
388 /* Name is a buffer of PATH_MAX length. */
389 static_assert(NTFS_NAME_LEN * 4 < PATH_MAX);
391 eod = i_size + sbi->record_size;
396 if (!dir_emit_dots(file, ctx))
399 /* Allocate PATH_MAX bytes. */
404 if (!ni->mi_loaded && ni->attr_list.size) {
406 * Directory inode is locked for read.
407 * Load all subrecords to avoid 'write' access to 'ni' during
411 if (!ni->mi_loaded && ni->attr_list.size) {
412 err = ni_load_all_mi(ni);
414 ni->mi_loaded = true;
421 root = indx_get_root(&ni->dir, ni, NULL, NULL);
427 if (pos >= sbi->record_size) {
428 bit = (pos - sbi->record_size) >> index_bits;
430 err = ntfs_read_hdr(sbi, ni, &root->ihdr, 0, pos, name, ctx);
442 vbo = (u64)bit << index_bits;
448 err = indx_used_bit(&ni->dir, ni, &bit);
452 if (bit == MINUS_ONE_T) {
457 vbo = (u64)bit << index_bits;
459 ntfs_inode_err(dir, "Looks like your dir is corrupt");
464 err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits,
469 err = ntfs_read_hdr(sbi, ni, &node->index->ihdr,
470 vbo + sbi->record_size, pos, name, ctx);
482 if (err == -ENOENT) {
490 static int ntfs_dir_count(struct inode *dir, bool *is_empty, size_t *dirs,
494 struct ntfs_inode *ni = ntfs_i(dir);
495 struct NTFS_DE *e = NULL;
496 struct INDEX_ROOT *root;
497 struct INDEX_HDR *hdr;
498 const struct ATTR_FILE_NAME *fname;
499 u32 e_size, off, end;
501 size_t drs = 0, fles = 0, bit = 0;
502 loff_t i_size = ni->vfs_inode.i_size;
503 struct indx_node *node = NULL;
504 u8 index_bits = ni->dir.index_bits;
509 root = indx_get_root(&ni->dir, ni, NULL, NULL);
516 end = le32_to_cpu(hdr->used);
517 off = le32_to_cpu(hdr->de_off);
519 for (; off + sizeof(struct NTFS_DE) <= end; off += e_size) {
520 e = Add2Ptr(hdr, off);
521 e_size = le16_to_cpu(e->size);
522 if (e_size < sizeof(struct NTFS_DE) ||
529 fname = de_get_fname(e);
533 if (fname->type == FILE_NAME_DOS)
542 if (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY)
551 err = indx_used_bit(&ni->dir, ni, &bit);
555 if (bit == MINUS_ONE_T)
558 vbo = (u64)bit << index_bits;
562 err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits,
567 hdr = &node->index->ihdr;
569 vbo = (u64)bit << ni->dir.idx2vbn_bits;
582 bool dir_is_empty(struct inode *dir)
584 bool is_empty = false;
586 ntfs_dir_count(dir, &is_empty, NULL, NULL);
592 const struct file_operations ntfs_dir_operations = {
593 .llseek = generic_file_llseek,
594 .read = generic_read_dir,
595 .iterate_shared = ntfs_readdir,
596 .fsync = generic_file_fsync,
597 .open = ntfs_file_open,