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.
11 #include <linux/nls.h>
17 /* Convert little endian UTF-16 to NLS string. */
18 int ntfs_utf16_to_nls(struct ntfs_sb_info
*sbi
, const __le16
*name
, u32 len
,
23 struct nls_table
*nls
= sbi
->options
->nls
;
25 static_assert(sizeof(wchar_t) == sizeof(__le16
));
29 ret
= utf16s_to_utf8s((wchar_t *)name
, len
, UTF16_LITTLE_ENDIAN
,
43 if (buf_len
< NLS_MAX_CHARSET_SIZE
) {
45 "filename was truncated while converting.");
49 ec
= le16_to_cpu(*name
++);
50 charlen
= nls
->uni2char(ec
, op
, buf_len
);
64 hex_byte_pack(&dump
[0], ec
>> 8);
65 hex_byte_pack(&dump
[2], ec
);
68 ntfs_err(sbi
->sb
, "failed to convert \"%s\" to %s", dump
,
77 #define PLANE_SIZE 0x00010000
79 #define SURROGATE_PAIR 0x0000d800
80 #define SURROGATE_LOW 0x00000400
81 #define SURROGATE_BITS 0x000003ff
85 * put_utf16 - Modified version of put_utf16 from fs/nls/nls_base.c
87 * Function is sparse warnings free.
89 static inline void put_utf16(wchar_t *s
, unsigned int c
,
90 enum utf16_endian endian
)
92 static_assert(sizeof(wchar_t) == sizeof(__le16
));
93 static_assert(sizeof(wchar_t) == sizeof(__be16
));
99 case UTF16_LITTLE_ENDIAN
:
100 *(__le16
*)s
= __cpu_to_le16(c
);
102 case UTF16_BIG_ENDIAN
:
103 *(__be16
*)s
= __cpu_to_be16(c
);
111 * Modified version of 'utf8s_to_utf16s' allows to
112 * detect -ENAMETOOLONG without writing out of expected maximum.
114 static int _utf8s_to_utf16s(const u8
*s
, int inlen
, enum utf16_endian endian
,
115 wchar_t *pwcs
, int maxout
)
122 while (inlen
> 0 && *s
) {
124 size
= utf8_to_utf32(s
, inlen
, &u
);
130 if (u
>= PLANE_SIZE
) {
132 return -ENAMETOOLONG
;
137 ((u
>> 10) & SURROGATE_BITS
),
140 SURROGATE_PAIR
| SURROGATE_LOW
|
141 (u
& SURROGATE_BITS
),
146 return -ENAMETOOLONG
;
148 put_utf16(op
++, u
, endian
);
153 return -ENAMETOOLONG
;
155 put_utf16(op
++, *s
++, endian
);
164 * ntfs_nls_to_utf16 - Convert input string to UTF-16.
166 * @name_len: Input name length.
167 * @uni: Destination memory.
168 * @max_ulen: Destination memory.
169 * @endian: Endian of target UTF-16 string.
171 * This function is called:
172 * - to create NTFS name
173 * - to create symlink
175 * Return: UTF-16 string length or error (if negative).
177 int ntfs_nls_to_utf16(struct ntfs_sb_info
*sbi
, const u8
*name
, u32 name_len
,
178 struct cpu_str
*uni
, u32 max_ulen
,
179 enum utf16_endian endian
)
183 struct nls_table
*nls
= sbi
->options
->nls
;
184 u16
*uname
= uni
->name
;
186 static_assert(sizeof(wchar_t) == sizeof(u16
));
190 ret
= _utf8s_to_utf16s(name
, name_len
, endian
, uname
, max_ulen
);
195 for (ret
= 0, end
= name
+ name_len
; name
< end
; ret
++, name
+= slen
) {
197 return -ENAMETOOLONG
;
199 slen
= nls
->char2uni(name
, end
- name
, uname
+ ret
);
207 if (endian
== UTF16_LITTLE_ENDIAN
) {
211 __cpu_to_le16s(uname
);
216 if (endian
== UTF16_BIG_ENDIAN
) {
220 __cpu_to_be16s(uname
);
231 * dir_search_u - Helper function.
233 struct inode
*dir_search_u(struct inode
*dir
, const struct cpu_str
*uni
,
234 struct ntfs_fnd
*fnd
)
237 struct super_block
*sb
= dir
->i_sb
;
238 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
239 struct ntfs_inode
*ni
= ntfs_i(dir
);
242 struct inode
*inode
= NULL
;
243 struct ntfs_fnd
*fnd_a
= NULL
;
254 err
= indx_find(&ni
->dir
, ni
, NULL
, uni
, 0, sbi
, &diff
, &e
, fnd
);
264 inode
= ntfs_iget5(sb
, &e
->ref
, uni
);
265 if (!IS_ERR(inode
) && is_bad_inode(inode
)) {
272 return err
== -ENOENT
? NULL
: err
? ERR_PTR(err
) : inode
;
276 * returns false if 'ctx' if full
278 static inline bool ntfs_dir_emit(struct ntfs_sb_info
*sbi
,
279 struct ntfs_inode
*ni
, const struct NTFS_DE
*e
,
280 u8
*name
, struct dir_context
*ctx
)
282 const struct ATTR_FILE_NAME
*fname
;
287 fname
= Add2Ptr(e
, sizeof(struct NTFS_DE
));
289 if (fname
->type
== FILE_NAME_DOS
)
292 if (!mi_is_ref(&ni
->mi
, &fname
->home
))
295 ino
= ino_get(&e
->ref
);
297 if (ino
== MFT_REC_ROOT
)
300 /* Skip meta files. Unless option to show metafiles is set. */
301 if (!sbi
->options
->showmeta
&& ntfs_is_meta_file(sbi
, ino
))
304 if (sbi
->options
->nohidden
&& (fname
->dup
.fa
& FILE_ATTRIBUTE_HIDDEN
))
307 name_len
= ntfs_utf16_to_nls(sbi
, fname
->name
, fname
->name_len
, name
,
310 ntfs_warn(sbi
->sb
, "failed to convert name for inode %lx.",
316 * NTFS: symlinks are "dir + reparse" or "file + reparse"
317 * Unfortunately reparse attribute is used for many purposes (several dozens).
318 * It is not possible here to know is this name symlink or not.
319 * To get exactly the type of name we should to open inode (read mft).
320 * getattr for opened file (fstat) correctly returns symlink.
322 dt_type
= (fname
->dup
.fa
& FILE_ATTRIBUTE_DIRECTORY
) ? DT_DIR
: DT_REG
;
325 * It is not reliable to detect the type of name using duplicated information
326 * stored in parent directory.
327 * The only correct way to get the type of name - read MFT record and find ATTR_STD.
328 * The code below is not good idea.
329 * It does additional locks/reads just to get the type of name.
330 * Should we use additional mount option to enable branch below?
332 if (((fname
->dup
.fa
& FILE_ATTRIBUTE_REPARSE_POINT
) ||
333 fname
->dup
.ea_size
) &&
335 struct inode
*inode
= ntfs_iget5(sbi
->sb
, &e
->ref
, NULL
);
336 if (!IS_ERR_OR_NULL(inode
)) {
337 dt_type
= fs_umode_to_dtype(inode
->i_mode
);
342 return dir_emit(ctx
, (s8
*)name
, name_len
, ino
, dt_type
);
346 * ntfs_read_hdr - Helper function for ntfs_readdir().
349 * returns -EINVAL if directory is corrupted.
350 * returns +1 if 'ctx' is full.
352 static int ntfs_read_hdr(struct ntfs_sb_info
*sbi
, struct ntfs_inode
*ni
,
353 const struct INDEX_HDR
*hdr
, u64 vbo
, u64 pos
,
354 u8
*name
, struct dir_context
*ctx
)
356 const struct NTFS_DE
*e
;
358 u32 end
= le32_to_cpu(hdr
->used
);
359 u32 off
= le32_to_cpu(hdr
->de_off
);
361 for (;; off
+= e_size
) {
362 if (off
+ sizeof(struct NTFS_DE
) > end
)
365 e
= Add2Ptr(hdr
, off
);
366 e_size
= le16_to_cpu(e
->size
);
367 if (e_size
< sizeof(struct NTFS_DE
) || off
+ e_size
> end
)
373 /* Skip already enumerated. */
377 if (le16_to_cpu(e
->key_size
) < SIZEOF_ATTRIBUTE_FILENAME
)
380 ctx
->pos
= vbo
+ off
;
382 /* Submit the name to the filldir callback. */
383 if (!ntfs_dir_emit(sbi
, ni
, e
, name
, ctx
)) {
391 * ntfs_readdir - file_operations::iterate_shared
393 * Use non sorted enumeration.
394 * We have an example of broken volume where sorted enumeration
395 * counts each name twice.
397 static int ntfs_readdir(struct file
*file
, struct dir_context
*ctx
)
399 const struct INDEX_ROOT
*root
;
404 struct inode
*dir
= file_inode(file
);
405 struct ntfs_inode
*ni
= ntfs_i(dir
);
406 struct super_block
*sb
= dir
->i_sb
;
407 struct ntfs_sb_info
*sbi
= sb
->s_fs_info
;
408 loff_t i_size
= i_size_read(dir
);
411 struct indx_node
*node
= NULL
;
412 u8 index_bits
= ni
->dir
.index_bits
;
414 /* Name is a buffer of PATH_MAX length. */
415 static_assert(NTFS_NAME_LEN
* 4 < PATH_MAX
);
417 eod
= i_size
+ sbi
->record_size
;
422 if (!dir_emit_dots(file
, ctx
))
425 /* Allocate PATH_MAX bytes. */
430 if (!ni
->mi_loaded
&& ni
->attr_list
.size
) {
432 * Directory inode is locked for read.
433 * Load all subrecords to avoid 'write' access to 'ni' during
437 if (!ni
->mi_loaded
&& ni
->attr_list
.size
) {
438 err
= ni_load_all_mi(ni
);
440 ni
->mi_loaded
= true;
447 root
= indx_get_root(&ni
->dir
, ni
, NULL
, NULL
);
453 if (pos
>= sbi
->record_size
) {
454 bit
= (pos
- sbi
->record_size
) >> index_bits
;
456 err
= ntfs_read_hdr(sbi
, ni
, &root
->ihdr
, 0, pos
, name
, ctx
);
468 vbo
= (u64
)bit
<< index_bits
;
474 err
= indx_used_bit(&ni
->dir
, ni
, &bit
);
478 if (bit
== MINUS_ONE_T
) {
483 vbo
= (u64
)bit
<< index_bits
;
489 err
= indx_read(&ni
->dir
, ni
, bit
<< ni
->dir
.idx2vbn_bits
,
494 err
= ntfs_read_hdr(sbi
, ni
, &node
->index
->ihdr
,
495 vbo
+ sbi
->record_size
, pos
, name
, ctx
);
510 } else if (err
== -ENOENT
) {
513 } else if (err
< 0) {
515 ntfs_inode_err(dir
, "directory corrupted");
522 static int ntfs_dir_count(struct inode
*dir
, bool *is_empty
, size_t *dirs
,
526 struct ntfs_inode
*ni
= ntfs_i(dir
);
527 struct NTFS_DE
*e
= NULL
;
528 struct INDEX_ROOT
*root
;
529 struct INDEX_HDR
*hdr
;
530 const struct ATTR_FILE_NAME
*fname
;
531 u32 e_size
, off
, end
;
532 size_t drs
= 0, fles
= 0, bit
= 0;
533 struct indx_node
*node
= NULL
;
534 size_t max_indx
= i_size_read(&ni
->vfs_inode
) >> ni
->dir
.index_bits
;
539 root
= indx_get_root(&ni
->dir
, ni
, NULL
, NULL
);
546 end
= le32_to_cpu(hdr
->used
);
547 off
= le32_to_cpu(hdr
->de_off
);
549 for (; off
+ sizeof(struct NTFS_DE
) <= end
; off
+= e_size
) {
550 e
= Add2Ptr(hdr
, off
);
551 e_size
= le16_to_cpu(e
->size
);
552 if (e_size
< sizeof(struct NTFS_DE
) ||
553 off
+ e_size
> end
) {
554 /* Looks like corruption. */
561 fname
= de_get_fname(e
);
565 if (fname
->type
== FILE_NAME_DOS
)
574 if (fname
->dup
.fa
& FILE_ATTRIBUTE_DIRECTORY
)
583 err
= indx_used_bit(&ni
->dir
, ni
, &bit
);
587 if (bit
== MINUS_ONE_T
)
593 err
= indx_read(&ni
->dir
, ni
, bit
<< ni
->dir
.idx2vbn_bits
,
598 hdr
= &node
->index
->ihdr
;
612 bool dir_is_empty(struct inode
*dir
)
614 bool is_empty
= false;
616 ntfs_dir_count(dir
, &is_empty
, NULL
, NULL
);
622 const struct file_operations ntfs_dir_operations
= {
623 .llseek
= generic_file_llseek
,
624 .read
= generic_read_dir
,
625 .iterate_shared
= ntfs_readdir
,
626 .fsync
= generic_file_fsync
,
627 .open
= ntfs_file_open
,
628 .unlocked_ioctl
= ntfs_ioctl
,
630 .compat_ioctl
= ntfs_compat_ioctl
,
634 #if IS_ENABLED(CONFIG_NTFS_FS)
635 const struct file_operations ntfs_legacy_dir_operations
= {
636 .llseek
= generic_file_llseek
,
637 .read
= generic_read_dir
,
638 .iterate_shared
= ntfs_readdir
,
639 .open
= ntfs_file_open
,