mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	fs/ntfs3: Add file operations and implementation
This adds file operations and implementation Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
This commit is contained in:
		
							parent
							
								
									3f3b442b5a
								
							
						
					
					
						commit
						4342306f0f
					
				
					 6 changed files with 7178 additions and 0 deletions
				
			
		
							
								
								
									
										596
									
								
								fs/ntfs3/dir.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										596
									
								
								fs/ntfs3/dir.c
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,596 @@
 | 
				
			||||||
 | 
					// SPDX-License-Identifier: GPL-2.0
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 *  directory handling functions for ntfs-based filesystems
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#include <linux/blkdev.h>
 | 
				
			||||||
 | 
					#include <linux/buffer_head.h>
 | 
				
			||||||
 | 
					#include <linux/fs.h>
 | 
				
			||||||
 | 
					#include <linux/iversion.h>
 | 
				
			||||||
 | 
					#include <linux/nls.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "debug.h"
 | 
				
			||||||
 | 
					#include "ntfs.h"
 | 
				
			||||||
 | 
					#include "ntfs_fs.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Convert little endian utf16 to nls string
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int ntfs_utf16_to_nls(struct ntfs_sb_info *sbi, const struct le_str *uni,
 | 
				
			||||||
 | 
							      u8 *buf, int buf_len)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int ret, uni_len, warn;
 | 
				
			||||||
 | 
						const __le16 *ip;
 | 
				
			||||||
 | 
						u8 *op;
 | 
				
			||||||
 | 
						struct nls_table *nls = sbi->options.nls;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						static_assert(sizeof(wchar_t) == sizeof(__le16));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!nls) {
 | 
				
			||||||
 | 
							/* utf16 -> utf8 */
 | 
				
			||||||
 | 
							ret = utf16s_to_utf8s((wchar_t *)uni->name, uni->len,
 | 
				
			||||||
 | 
									      UTF16_LITTLE_ENDIAN, buf, buf_len);
 | 
				
			||||||
 | 
							buf[ret] = '\0';
 | 
				
			||||||
 | 
							return ret;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ip = uni->name;
 | 
				
			||||||
 | 
						op = buf;
 | 
				
			||||||
 | 
						uni_len = uni->len;
 | 
				
			||||||
 | 
						warn = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						while (uni_len--) {
 | 
				
			||||||
 | 
							u16 ec;
 | 
				
			||||||
 | 
							int charlen;
 | 
				
			||||||
 | 
							char dump[5];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (buf_len < NLS_MAX_CHARSET_SIZE) {
 | 
				
			||||||
 | 
								ntfs_warn(sbi->sb,
 | 
				
			||||||
 | 
									  "filename was truncated while converting.");
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ec = le16_to_cpu(*ip++);
 | 
				
			||||||
 | 
							charlen = nls->uni2char(ec, op, buf_len);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (charlen > 0) {
 | 
				
			||||||
 | 
								op += charlen;
 | 
				
			||||||
 | 
								buf_len -= charlen;
 | 
				
			||||||
 | 
								continue;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							*op++ = '_';
 | 
				
			||||||
 | 
							buf_len -= 1;
 | 
				
			||||||
 | 
							if (warn)
 | 
				
			||||||
 | 
								continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							warn = 1;
 | 
				
			||||||
 | 
							hex_byte_pack(&dump[0], ec >> 8);
 | 
				
			||||||
 | 
							hex_byte_pack(&dump[2], ec);
 | 
				
			||||||
 | 
							dump[4] = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ntfs_err(sbi->sb, "failed to convert \"%s\" to %s", dump,
 | 
				
			||||||
 | 
								 nls->charset);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						*op = '\0';
 | 
				
			||||||
 | 
						return op - buf;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// clang-format off
 | 
				
			||||||
 | 
					#define PLANE_SIZE	0x00010000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define SURROGATE_PAIR	0x0000d800
 | 
				
			||||||
 | 
					#define SURROGATE_LOW	0x00000400
 | 
				
			||||||
 | 
					#define SURROGATE_BITS	0x000003ff
 | 
				
			||||||
 | 
					// clang-format on
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * modified version of put_utf16 from fs/nls/nls_base.c
 | 
				
			||||||
 | 
					 * is sparse warnings free
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static inline void put_utf16(wchar_t *s, unsigned int c,
 | 
				
			||||||
 | 
								     enum utf16_endian endian)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						static_assert(sizeof(wchar_t) == sizeof(__le16));
 | 
				
			||||||
 | 
						static_assert(sizeof(wchar_t) == sizeof(__be16));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						switch (endian) {
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							*s = (wchar_t)c;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
						case UTF16_LITTLE_ENDIAN:
 | 
				
			||||||
 | 
							*(__le16 *)s = __cpu_to_le16(c);
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
						case UTF16_BIG_ENDIAN:
 | 
				
			||||||
 | 
							*(__be16 *)s = __cpu_to_be16(c);
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * modified version of 'utf8s_to_utf16s' allows to
 | 
				
			||||||
 | 
					 * detect -ENAMETOOLONG without writing out of expected maximum
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int _utf8s_to_utf16s(const u8 *s, int inlen, enum utf16_endian endian,
 | 
				
			||||||
 | 
								    wchar_t *pwcs, int maxout)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						u16 *op;
 | 
				
			||||||
 | 
						int size;
 | 
				
			||||||
 | 
						unicode_t u;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						op = pwcs;
 | 
				
			||||||
 | 
						while (inlen > 0 && *s) {
 | 
				
			||||||
 | 
							if (*s & 0x80) {
 | 
				
			||||||
 | 
								size = utf8_to_utf32(s, inlen, &u);
 | 
				
			||||||
 | 
								if (size < 0)
 | 
				
			||||||
 | 
									return -EINVAL;
 | 
				
			||||||
 | 
								s += size;
 | 
				
			||||||
 | 
								inlen -= size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (u >= PLANE_SIZE) {
 | 
				
			||||||
 | 
									if (maxout < 2)
 | 
				
			||||||
 | 
										return -ENAMETOOLONG;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									u -= PLANE_SIZE;
 | 
				
			||||||
 | 
									put_utf16(op++,
 | 
				
			||||||
 | 
										  SURROGATE_PAIR |
 | 
				
			||||||
 | 
											  ((u >> 10) & SURROGATE_BITS),
 | 
				
			||||||
 | 
										  endian);
 | 
				
			||||||
 | 
									put_utf16(op++,
 | 
				
			||||||
 | 
										  SURROGATE_PAIR | SURROGATE_LOW |
 | 
				
			||||||
 | 
											  (u & SURROGATE_BITS),
 | 
				
			||||||
 | 
										  endian);
 | 
				
			||||||
 | 
									maxout -= 2;
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									if (maxout < 1)
 | 
				
			||||||
 | 
										return -ENAMETOOLONG;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									put_utf16(op++, u, endian);
 | 
				
			||||||
 | 
									maxout--;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								if (maxout < 1)
 | 
				
			||||||
 | 
									return -ENAMETOOLONG;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								put_utf16(op++, *s++, endian);
 | 
				
			||||||
 | 
								inlen--;
 | 
				
			||||||
 | 
								maxout--;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return op - pwcs;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Convert input string to utf16
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * name, name_len - input name
 | 
				
			||||||
 | 
					 * uni, max_ulen - destination memory
 | 
				
			||||||
 | 
					 * endian - endian of target utf16 string
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This function is called:
 | 
				
			||||||
 | 
					 * - to create ntfs name
 | 
				
			||||||
 | 
					 * - to create symlink
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * returns utf16 string length or error (if negative)
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len,
 | 
				
			||||||
 | 
							      struct cpu_str *uni, u32 max_ulen,
 | 
				
			||||||
 | 
							      enum utf16_endian endian)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int ret, slen;
 | 
				
			||||||
 | 
						const u8 *end;
 | 
				
			||||||
 | 
						struct nls_table *nls = sbi->options.nls;
 | 
				
			||||||
 | 
						u16 *uname = uni->name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						static_assert(sizeof(wchar_t) == sizeof(u16));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!nls) {
 | 
				
			||||||
 | 
							/* utf8 -> utf16 */
 | 
				
			||||||
 | 
							ret = _utf8s_to_utf16s(name, name_len, endian, uname, max_ulen);
 | 
				
			||||||
 | 
							uni->len = ret;
 | 
				
			||||||
 | 
							return ret;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (ret = 0, end = name + name_len; name < end; ret++, name += slen) {
 | 
				
			||||||
 | 
							if (ret >= max_ulen)
 | 
				
			||||||
 | 
								return -ENAMETOOLONG;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							slen = nls->char2uni(name, end - name, uname + ret);
 | 
				
			||||||
 | 
							if (!slen)
 | 
				
			||||||
 | 
								return -EINVAL;
 | 
				
			||||||
 | 
							if (slen < 0)
 | 
				
			||||||
 | 
								return slen;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __BIG_ENDIAN
 | 
				
			||||||
 | 
						if (endian == UTF16_LITTLE_ENDIAN) {
 | 
				
			||||||
 | 
							int i = ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							while (i--) {
 | 
				
			||||||
 | 
								__cpu_to_le16s(uname);
 | 
				
			||||||
 | 
								uname++;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
						if (endian == UTF16_BIG_ENDIAN) {
 | 
				
			||||||
 | 
							int i = ret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							while (i--) {
 | 
				
			||||||
 | 
								__cpu_to_be16s(uname);
 | 
				
			||||||
 | 
								uname++;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						uni->len = ret;
 | 
				
			||||||
 | 
						return ret;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* helper function */
 | 
				
			||||||
 | 
					struct inode *dir_search_u(struct inode *dir, const struct cpu_str *uni,
 | 
				
			||||||
 | 
								   struct ntfs_fnd *fnd)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err = 0;
 | 
				
			||||||
 | 
						struct super_block *sb = dir->i_sb;
 | 
				
			||||||
 | 
						struct ntfs_sb_info *sbi = sb->s_fs_info;
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(dir);
 | 
				
			||||||
 | 
						struct NTFS_DE *e;
 | 
				
			||||||
 | 
						int diff;
 | 
				
			||||||
 | 
						struct inode *inode = NULL;
 | 
				
			||||||
 | 
						struct ntfs_fnd *fnd_a = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!fnd) {
 | 
				
			||||||
 | 
							fnd_a = fnd_get();
 | 
				
			||||||
 | 
							if (!fnd_a) {
 | 
				
			||||||
 | 
								err = -ENOMEM;
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							fnd = fnd_a;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = indx_find(&ni->dir, ni, NULL, uni, 0, sbi, &diff, &e, fnd);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (err)
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (diff) {
 | 
				
			||||||
 | 
							err = -ENOENT;
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						inode = ntfs_iget5(sb, &e->ref, uni);
 | 
				
			||||||
 | 
						if (!IS_ERR(inode) && is_bad_inode(inode)) {
 | 
				
			||||||
 | 
							iput(inode);
 | 
				
			||||||
 | 
							err = -EINVAL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					out:
 | 
				
			||||||
 | 
						fnd_put(fnd_a);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return err == -ENOENT ? NULL : err ? ERR_PTR(err) : inode;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static inline int ntfs_filldir(struct ntfs_sb_info *sbi, struct ntfs_inode *ni,
 | 
				
			||||||
 | 
								       const struct NTFS_DE *e, u8 *name,
 | 
				
			||||||
 | 
								       struct dir_context *ctx)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const struct ATTR_FILE_NAME *fname;
 | 
				
			||||||
 | 
						unsigned long ino;
 | 
				
			||||||
 | 
						int name_len;
 | 
				
			||||||
 | 
						u32 dt_type;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fname = Add2Ptr(e, sizeof(struct NTFS_DE));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (fname->type == FILE_NAME_DOS)
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!mi_is_ref(&ni->mi, &fname->home))
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ino = ino_get(&e->ref);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (ino == MFT_REC_ROOT)
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Skip meta files ( unless option to show metafiles is set ) */
 | 
				
			||||||
 | 
						if (!sbi->options.showmeta && ntfs_is_meta_file(sbi, ino))
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (sbi->options.nohidden && (fname->dup.fa & FILE_ATTRIBUTE_HIDDEN))
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						name_len = ntfs_utf16_to_nls(sbi, (struct le_str *)&fname->name_len,
 | 
				
			||||||
 | 
									     name, PATH_MAX);
 | 
				
			||||||
 | 
						if (name_len <= 0) {
 | 
				
			||||||
 | 
							ntfs_warn(sbi->sb, "failed to convert name for inode %lx.",
 | 
				
			||||||
 | 
								  ino);
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dt_type = (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return !dir_emit(ctx, (s8 *)name, name_len, ino, dt_type);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * ntfs_read_hdr
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * helper function 'ntfs_readdir'
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int ntfs_read_hdr(struct ntfs_sb_info *sbi, struct ntfs_inode *ni,
 | 
				
			||||||
 | 
								 const struct INDEX_HDR *hdr, u64 vbo, u64 pos,
 | 
				
			||||||
 | 
								 u8 *name, struct dir_context *ctx)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
						const struct NTFS_DE *e;
 | 
				
			||||||
 | 
						u32 e_size;
 | 
				
			||||||
 | 
						u32 end = le32_to_cpu(hdr->used);
 | 
				
			||||||
 | 
						u32 off = le32_to_cpu(hdr->de_off);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (;; off += e_size) {
 | 
				
			||||||
 | 
							if (off + sizeof(struct NTFS_DE) > end)
 | 
				
			||||||
 | 
								return -1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							e = Add2Ptr(hdr, off);
 | 
				
			||||||
 | 
							e_size = le16_to_cpu(e->size);
 | 
				
			||||||
 | 
							if (e_size < sizeof(struct NTFS_DE) || off + e_size > end)
 | 
				
			||||||
 | 
								return -1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (de_is_last(e))
 | 
				
			||||||
 | 
								return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							/* Skip already enumerated*/
 | 
				
			||||||
 | 
							if (vbo + off < pos)
 | 
				
			||||||
 | 
								continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (le16_to_cpu(e->key_size) < SIZEOF_ATTRIBUTE_FILENAME)
 | 
				
			||||||
 | 
								return -1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							ctx->pos = vbo + off;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							/* Submit the name to the filldir callback. */
 | 
				
			||||||
 | 
							err = ntfs_filldir(sbi, ni, e, name, ctx);
 | 
				
			||||||
 | 
							if (err)
 | 
				
			||||||
 | 
								return err;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * file_operations::iterate_shared
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Use non sorted enumeration.
 | 
				
			||||||
 | 
					 * We have an example of broken volume where sorted enumeration
 | 
				
			||||||
 | 
					 * counts each name twice
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int ntfs_readdir(struct file *file, struct dir_context *ctx)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const struct INDEX_ROOT *root;
 | 
				
			||||||
 | 
						u64 vbo;
 | 
				
			||||||
 | 
						size_t bit;
 | 
				
			||||||
 | 
						loff_t eod;
 | 
				
			||||||
 | 
						int err = 0;
 | 
				
			||||||
 | 
						struct inode *dir = file_inode(file);
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(dir);
 | 
				
			||||||
 | 
						struct super_block *sb = dir->i_sb;
 | 
				
			||||||
 | 
						struct ntfs_sb_info *sbi = sb->s_fs_info;
 | 
				
			||||||
 | 
						loff_t i_size = i_size_read(dir);
 | 
				
			||||||
 | 
						u32 pos = ctx->pos;
 | 
				
			||||||
 | 
						u8 *name = NULL;
 | 
				
			||||||
 | 
						struct indx_node *node = NULL;
 | 
				
			||||||
 | 
						u8 index_bits = ni->dir.index_bits;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* name is a buffer of PATH_MAX length */
 | 
				
			||||||
 | 
						static_assert(NTFS_NAME_LEN * 4 < PATH_MAX);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						eod = i_size + sbi->record_size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (pos >= eod)
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!dir_emit_dots(file, ctx))
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* allocate PATH_MAX bytes */
 | 
				
			||||||
 | 
						name = __getname();
 | 
				
			||||||
 | 
						if (!name)
 | 
				
			||||||
 | 
							return -ENOMEM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!ni->mi_loaded && ni->attr_list.size) {
 | 
				
			||||||
 | 
							/*
 | 
				
			||||||
 | 
							 * directory inode is locked for read
 | 
				
			||||||
 | 
							 * load all subrecords to avoid 'write' access to 'ni' during
 | 
				
			||||||
 | 
							 * directory reading
 | 
				
			||||||
 | 
							 */
 | 
				
			||||||
 | 
							ni_lock(ni);
 | 
				
			||||||
 | 
							if (!ni->mi_loaded && ni->attr_list.size) {
 | 
				
			||||||
 | 
								err = ni_load_all_mi(ni);
 | 
				
			||||||
 | 
								if (!err)
 | 
				
			||||||
 | 
									ni->mi_loaded = true;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							ni_unlock(ni);
 | 
				
			||||||
 | 
							if (err)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						root = indx_get_root(&ni->dir, ni, NULL, NULL);
 | 
				
			||||||
 | 
						if (!root) {
 | 
				
			||||||
 | 
							err = -EINVAL;
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (pos >= sbi->record_size) {
 | 
				
			||||||
 | 
							bit = (pos - sbi->record_size) >> index_bits;
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							err = ntfs_read_hdr(sbi, ni, &root->ihdr, 0, pos, name, ctx);
 | 
				
			||||||
 | 
							if (err)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
							bit = 0;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!i_size) {
 | 
				
			||||||
 | 
							ctx->pos = eod;
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (;;) {
 | 
				
			||||||
 | 
							vbo = (u64)bit << index_bits;
 | 
				
			||||||
 | 
							if (vbo >= i_size) {
 | 
				
			||||||
 | 
								ctx->pos = eod;
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = indx_used_bit(&ni->dir, ni, &bit);
 | 
				
			||||||
 | 
							if (err)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (bit == MINUS_ONE_T) {
 | 
				
			||||||
 | 
								ctx->pos = eod;
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							vbo = (u64)bit << index_bits;
 | 
				
			||||||
 | 
							if (vbo >= i_size) {
 | 
				
			||||||
 | 
								ntfs_inode_err(dir, "Looks like your dir is corrupt");
 | 
				
			||||||
 | 
								err = -EINVAL;
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits,
 | 
				
			||||||
 | 
									&node);
 | 
				
			||||||
 | 
							if (err)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = ntfs_read_hdr(sbi, ni, &node->index->ihdr,
 | 
				
			||||||
 | 
									    vbo + sbi->record_size, pos, name, ctx);
 | 
				
			||||||
 | 
							if (err)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							bit += 1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					out:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						__putname(name);
 | 
				
			||||||
 | 
						put_indx_node(node);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (err == -ENOENT) {
 | 
				
			||||||
 | 
							err = 0;
 | 
				
			||||||
 | 
							ctx->pos = pos;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return err;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int ntfs_dir_count(struct inode *dir, bool *is_empty, size_t *dirs,
 | 
				
			||||||
 | 
								  size_t *files)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err = 0;
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(dir);
 | 
				
			||||||
 | 
						struct NTFS_DE *e = NULL;
 | 
				
			||||||
 | 
						struct INDEX_ROOT *root;
 | 
				
			||||||
 | 
						struct INDEX_HDR *hdr;
 | 
				
			||||||
 | 
						const struct ATTR_FILE_NAME *fname;
 | 
				
			||||||
 | 
						u32 e_size, off, end;
 | 
				
			||||||
 | 
						u64 vbo = 0;
 | 
				
			||||||
 | 
						size_t drs = 0, fles = 0, bit = 0;
 | 
				
			||||||
 | 
						loff_t i_size = ni->vfs_inode.i_size;
 | 
				
			||||||
 | 
						struct indx_node *node = NULL;
 | 
				
			||||||
 | 
						u8 index_bits = ni->dir.index_bits;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (is_empty)
 | 
				
			||||||
 | 
							*is_empty = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						root = indx_get_root(&ni->dir, ni, NULL, NULL);
 | 
				
			||||||
 | 
						if (!root)
 | 
				
			||||||
 | 
							return -EINVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						hdr = &root->ihdr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (;;) {
 | 
				
			||||||
 | 
							end = le32_to_cpu(hdr->used);
 | 
				
			||||||
 | 
							off = le32_to_cpu(hdr->de_off);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for (; off + sizeof(struct NTFS_DE) <= end; off += e_size) {
 | 
				
			||||||
 | 
								e = Add2Ptr(hdr, off);
 | 
				
			||||||
 | 
								e_size = le16_to_cpu(e->size);
 | 
				
			||||||
 | 
								if (e_size < sizeof(struct NTFS_DE) ||
 | 
				
			||||||
 | 
								    off + e_size > end)
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (de_is_last(e))
 | 
				
			||||||
 | 
									break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								fname = de_get_fname(e);
 | 
				
			||||||
 | 
								if (!fname)
 | 
				
			||||||
 | 
									continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (fname->type == FILE_NAME_DOS)
 | 
				
			||||||
 | 
									continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (is_empty) {
 | 
				
			||||||
 | 
									*is_empty = false;
 | 
				
			||||||
 | 
									if (!dirs && !files)
 | 
				
			||||||
 | 
										goto out;
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY)
 | 
				
			||||||
 | 
									drs += 1;
 | 
				
			||||||
 | 
								else
 | 
				
			||||||
 | 
									fles += 1;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (vbo >= i_size)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = indx_used_bit(&ni->dir, ni, &bit);
 | 
				
			||||||
 | 
							if (err)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (bit == MINUS_ONE_T)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							vbo = (u64)bit << index_bits;
 | 
				
			||||||
 | 
							if (vbo >= i_size)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits,
 | 
				
			||||||
 | 
									&node);
 | 
				
			||||||
 | 
							if (err)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							hdr = &node->index->ihdr;
 | 
				
			||||||
 | 
							bit += 1;
 | 
				
			||||||
 | 
							vbo = (u64)bit << ni->dir.idx2vbn_bits;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					out:
 | 
				
			||||||
 | 
						put_indx_node(node);
 | 
				
			||||||
 | 
						if (dirs)
 | 
				
			||||||
 | 
							*dirs = drs;
 | 
				
			||||||
 | 
						if (files)
 | 
				
			||||||
 | 
							*files = fles;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return err;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool dir_is_empty(struct inode *dir)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						bool is_empty = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ntfs_dir_count(dir, &is_empty, NULL, NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return is_empty;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// clang-format off
 | 
				
			||||||
 | 
					const struct file_operations ntfs_dir_operations = {
 | 
				
			||||||
 | 
						.llseek		= generic_file_llseek,
 | 
				
			||||||
 | 
						.read		= generic_read_dir,
 | 
				
			||||||
 | 
						.iterate_shared	= ntfs_readdir,
 | 
				
			||||||
 | 
						.fsync		= generic_file_fsync,
 | 
				
			||||||
 | 
						.open		= ntfs_file_open,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					// clang-format on
 | 
				
			||||||
							
								
								
									
										1252
									
								
								fs/ntfs3/file.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1252
									
								
								fs/ntfs3/file.c
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										3071
									
								
								fs/ntfs3/frecord.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3071
									
								
								fs/ntfs3/frecord.c
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										539
									
								
								fs/ntfs3/namei.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										539
									
								
								fs/ntfs3/namei.c
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,539 @@
 | 
				
			||||||
 | 
					// SPDX-License-Identifier: GPL-2.0
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <linux/blkdev.h>
 | 
				
			||||||
 | 
					#include <linux/buffer_head.h>
 | 
				
			||||||
 | 
					#include <linux/fs.h>
 | 
				
			||||||
 | 
					#include <linux/iversion.h>
 | 
				
			||||||
 | 
					#include <linux/namei.h>
 | 
				
			||||||
 | 
					#include <linux/nls.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "debug.h"
 | 
				
			||||||
 | 
					#include "ntfs.h"
 | 
				
			||||||
 | 
					#include "ntfs_fs.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * fill_name_de
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * formats NTFS_DE in 'buf'
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
 | 
				
			||||||
 | 
							 const struct cpu_str *uni)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
						struct NTFS_DE *e = buf;
 | 
				
			||||||
 | 
						u16 data_size;
 | 
				
			||||||
 | 
						struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifndef CONFIG_NTFS3_64BIT_CLUSTER
 | 
				
			||||||
 | 
						e->ref.high = fname->home.high = 0;
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
						if (uni) {
 | 
				
			||||||
 | 
					#ifdef __BIG_ENDIAN
 | 
				
			||||||
 | 
							int ulen = uni->len;
 | 
				
			||||||
 | 
							__le16 *uname = fname->name;
 | 
				
			||||||
 | 
							const u16 *name_cpu = uni->name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							while (ulen--)
 | 
				
			||||||
 | 
								*uname++ = cpu_to_le16(*name_cpu++);
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
							memcpy(fname->name, uni->name, uni->len * sizeof(u16));
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
							fname->name_len = uni->len;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							/* Convert input string to unicode */
 | 
				
			||||||
 | 
							err = ntfs_nls_to_utf16(sbi, name->name, name->len,
 | 
				
			||||||
 | 
										(struct cpu_str *)&fname->name_len,
 | 
				
			||||||
 | 
										NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN);
 | 
				
			||||||
 | 
							if (err < 0)
 | 
				
			||||||
 | 
								return err;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fname->type = FILE_NAME_POSIX;
 | 
				
			||||||
 | 
						data_size = fname_full_size(fname);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						e->size = cpu_to_le16(QuadAlign(data_size) + sizeof(struct NTFS_DE));
 | 
				
			||||||
 | 
						e->key_size = cpu_to_le16(data_size);
 | 
				
			||||||
 | 
						e->flags = 0;
 | 
				
			||||||
 | 
						e->res = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * ntfs_lookup
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * inode_operations::lookup
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
 | 
				
			||||||
 | 
									  u32 flags)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(dir);
 | 
				
			||||||
 | 
						struct cpu_str *uni = __getname();
 | 
				
			||||||
 | 
						struct inode *inode;
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!uni)
 | 
				
			||||||
 | 
							inode = ERR_PTR(-ENOMEM);
 | 
				
			||||||
 | 
						else {
 | 
				
			||||||
 | 
							err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
 | 
				
			||||||
 | 
										dentry->d_name.len, uni, NTFS_NAME_LEN,
 | 
				
			||||||
 | 
										UTF16_HOST_ENDIAN);
 | 
				
			||||||
 | 
							if (err < 0)
 | 
				
			||||||
 | 
								inode = ERR_PTR(err);
 | 
				
			||||||
 | 
							else {
 | 
				
			||||||
 | 
								ni_lock(ni);
 | 
				
			||||||
 | 
								inode = dir_search_u(dir, uni, NULL);
 | 
				
			||||||
 | 
								ni_unlock(ni);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							__putname(uni);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return d_splice_alias(inode, dentry);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * ntfs_create
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * inode_operations::create
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int ntfs_create(struct user_namespace *mnt_userns, struct inode *dir,
 | 
				
			||||||
 | 
							       struct dentry *dentry, umode_t mode, bool excl)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(dir);
 | 
				
			||||||
 | 
						struct inode *inode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_lock_dir(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFREG | mode,
 | 
				
			||||||
 | 
									  0, NULL, 0, NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_unlock(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return IS_ERR(inode) ? PTR_ERR(inode) : 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * ntfs_mknod
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * inode_operations::mknod
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int ntfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
 | 
				
			||||||
 | 
							      struct dentry *dentry, umode_t mode, dev_t rdev)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(dir);
 | 
				
			||||||
 | 
						struct inode *inode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_lock_dir(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, mode, rdev,
 | 
				
			||||||
 | 
									  NULL, 0, NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_unlock(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return IS_ERR(inode) ? PTR_ERR(inode) : 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * ntfs_link
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * inode_operations::link
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
						struct inode *inode = d_inode(ode);
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(inode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (S_ISDIR(inode->i_mode))
 | 
				
			||||||
 | 
							return -EPERM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (inode->i_nlink >= NTFS_LINK_MAX)
 | 
				
			||||||
 | 
							return -EMLINK;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_lock_dir(ntfs_i(dir));
 | 
				
			||||||
 | 
						if (inode != dir)
 | 
				
			||||||
 | 
							ni_lock(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						dir->i_ctime = dir->i_mtime = inode->i_ctime = current_time(inode);
 | 
				
			||||||
 | 
						inc_nlink(inode);
 | 
				
			||||||
 | 
						ihold(inode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = ntfs_link_inode(inode, de);
 | 
				
			||||||
 | 
						if (!err) {
 | 
				
			||||||
 | 
							mark_inode_dirty(inode);
 | 
				
			||||||
 | 
							mark_inode_dirty(dir);
 | 
				
			||||||
 | 
							d_instantiate(de, inode);
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							drop_nlink(inode);
 | 
				
			||||||
 | 
							iput(inode);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (inode != dir)
 | 
				
			||||||
 | 
							ni_unlock(ni);
 | 
				
			||||||
 | 
						ni_unlock(ntfs_i(dir));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return err;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * ntfs_unlink
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * inode_operations::unlink
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(dir);
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_lock_dir(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = ntfs_unlink_inode(dir, dentry);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_unlock(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return err;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * ntfs_symlink
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * inode_operations::symlink
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int ntfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
 | 
				
			||||||
 | 
								struct dentry *dentry, const char *symname)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						u32 size = strlen(symname);
 | 
				
			||||||
 | 
						struct inode *inode;
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(dir);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_lock_dir(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFLNK | 0777,
 | 
				
			||||||
 | 
									  0, symname, size, NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_unlock(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return IS_ERR(inode) ? PTR_ERR(inode) : 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * ntfs_mkdir
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * inode_operations::mkdir
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int ntfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
 | 
				
			||||||
 | 
							      struct dentry *dentry, umode_t mode)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct inode *inode;
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(dir);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_lock_dir(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFDIR | mode,
 | 
				
			||||||
 | 
									  0, NULL, 0, NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_unlock(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return IS_ERR(inode) ? PTR_ERR(inode) : 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * ntfs_rmdir
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * inode_operations::rm_dir
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(dir);
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_lock_dir(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = ntfs_unlink_inode(dir, dentry);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_unlock(ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return err;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * ntfs_rename
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * inode_operations::rename
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static int ntfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
 | 
				
			||||||
 | 
							       struct dentry *old_dentry, struct inode *new_dir,
 | 
				
			||||||
 | 
							       struct dentry *new_dentry, u32 flags)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
						struct super_block *sb = old_dir->i_sb;
 | 
				
			||||||
 | 
						struct ntfs_sb_info *sbi = sb->s_fs_info;
 | 
				
			||||||
 | 
						struct ntfs_inode *old_dir_ni = ntfs_i(old_dir);
 | 
				
			||||||
 | 
						struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
 | 
				
			||||||
 | 
						struct ntfs_inode *old_ni;
 | 
				
			||||||
 | 
						struct ATTR_FILE_NAME *old_name, *new_name, *fname;
 | 
				
			||||||
 | 
						u8 name_type;
 | 
				
			||||||
 | 
						bool is_same;
 | 
				
			||||||
 | 
						struct inode *old_inode, *new_inode;
 | 
				
			||||||
 | 
						struct NTFS_DE *old_de, *new_de;
 | 
				
			||||||
 | 
						struct ATTRIB *attr;
 | 
				
			||||||
 | 
						struct ATTR_LIST_ENTRY *le;
 | 
				
			||||||
 | 
						u16 new_de_key_size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
 | 
				
			||||||
 | 
						static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
 | 
				
			||||||
 | 
							      1024);
 | 
				
			||||||
 | 
						static_assert(PATH_MAX >= 4 * 1024);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (flags & ~RENAME_NOREPLACE)
 | 
				
			||||||
 | 
							return -EINVAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						old_inode = d_inode(old_dentry);
 | 
				
			||||||
 | 
						new_inode = d_inode(new_dentry);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						old_ni = ntfs_i(old_inode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						is_same = old_dentry->d_name.len == new_dentry->d_name.len &&
 | 
				
			||||||
 | 
							  !memcmp(old_dentry->d_name.name, new_dentry->d_name.name,
 | 
				
			||||||
 | 
								  old_dentry->d_name.len);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (is_same && old_dir == new_dir) {
 | 
				
			||||||
 | 
							/* Nothing to do */
 | 
				
			||||||
 | 
							err = 0;
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (ntfs_is_meta_file(sbi, old_inode->i_ino)) {
 | 
				
			||||||
 | 
							err = -EINVAL;
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (new_inode) {
 | 
				
			||||||
 | 
							/*target name exists. unlink it*/
 | 
				
			||||||
 | 
							dget(new_dentry);
 | 
				
			||||||
 | 
							ni_lock_dir(new_dir_ni);
 | 
				
			||||||
 | 
							err = ntfs_unlink_inode(new_dir, new_dentry);
 | 
				
			||||||
 | 
							ni_unlock(new_dir_ni);
 | 
				
			||||||
 | 
							dput(new_dentry);
 | 
				
			||||||
 | 
							if (err)
 | 
				
			||||||
 | 
								goto out;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* allocate PATH_MAX bytes */
 | 
				
			||||||
 | 
						old_de = __getname();
 | 
				
			||||||
 | 
						if (!old_de) {
 | 
				
			||||||
 | 
							err = -ENOMEM;
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = fill_name_de(sbi, old_de, &old_dentry->d_name, NULL);
 | 
				
			||||||
 | 
						if (err < 0)
 | 
				
			||||||
 | 
							goto out1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						old_name = (struct ATTR_FILE_NAME *)(old_de + 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (is_same) {
 | 
				
			||||||
 | 
							new_de = old_de;
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							new_de = Add2Ptr(old_de, 1024);
 | 
				
			||||||
 | 
							err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
 | 
				
			||||||
 | 
							if (err < 0)
 | 
				
			||||||
 | 
								goto out1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ni_lock_dir(old_dir_ni);
 | 
				
			||||||
 | 
						ni_lock(old_ni);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mi_get_ref(&old_dir_ni->mi, &old_name->home);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*get pointer to file_name in mft*/
 | 
				
			||||||
 | 
						fname = ni_fname_name(old_ni, (struct cpu_str *)&old_name->name_len,
 | 
				
			||||||
 | 
								      &old_name->home, &le);
 | 
				
			||||||
 | 
						if (!fname) {
 | 
				
			||||||
 | 
							err = -EINVAL;
 | 
				
			||||||
 | 
							goto out2;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Copy fname info from record into new fname */
 | 
				
			||||||
 | 
						new_name = (struct ATTR_FILE_NAME *)(new_de + 1);
 | 
				
			||||||
 | 
						memcpy(&new_name->dup, &fname->dup, sizeof(fname->dup));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						name_type = paired_name(fname->type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* remove first name from directory */
 | 
				
			||||||
 | 
						err = indx_delete_entry(&old_dir_ni->dir, old_dir_ni, old_de + 1,
 | 
				
			||||||
 | 
									le16_to_cpu(old_de->key_size), sbi);
 | 
				
			||||||
 | 
						if (err)
 | 
				
			||||||
 | 
							goto out3;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* remove first name from mft */
 | 
				
			||||||
 | 
						err = ni_remove_attr_le(old_ni, attr_from_name(fname), le);
 | 
				
			||||||
 | 
						if (err)
 | 
				
			||||||
 | 
							goto out4;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						le16_add_cpu(&old_ni->mi.mrec->hard_links, -1);
 | 
				
			||||||
 | 
						old_ni->mi.dirty = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (name_type != FILE_NAME_POSIX) {
 | 
				
			||||||
 | 
							/* get paired name */
 | 
				
			||||||
 | 
							fname = ni_fname_type(old_ni, name_type, &le);
 | 
				
			||||||
 | 
							if (fname) {
 | 
				
			||||||
 | 
								/* remove second name from directory */
 | 
				
			||||||
 | 
								err = indx_delete_entry(&old_dir_ni->dir, old_dir_ni,
 | 
				
			||||||
 | 
											fname, fname_full_size(fname),
 | 
				
			||||||
 | 
											sbi);
 | 
				
			||||||
 | 
								if (err)
 | 
				
			||||||
 | 
									goto out5;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								/* remove second name from mft */
 | 
				
			||||||
 | 
								err = ni_remove_attr_le(old_ni, attr_from_name(fname),
 | 
				
			||||||
 | 
											le);
 | 
				
			||||||
 | 
								if (err)
 | 
				
			||||||
 | 
									goto out6;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								le16_add_cpu(&old_ni->mi.mrec->hard_links, -1);
 | 
				
			||||||
 | 
								old_ni->mi.dirty = true;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Add new name */
 | 
				
			||||||
 | 
						mi_get_ref(&old_ni->mi, &new_de->ref);
 | 
				
			||||||
 | 
						mi_get_ref(&ntfs_i(new_dir)->mi, &new_name->home);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						new_de_key_size = le16_to_cpu(new_de->key_size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* insert new name in mft */
 | 
				
			||||||
 | 
						err = ni_insert_resident(old_ni, new_de_key_size, ATTR_NAME, NULL, 0,
 | 
				
			||||||
 | 
									 &attr, NULL);
 | 
				
			||||||
 | 
						if (err)
 | 
				
			||||||
 | 
							goto out7;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						attr->res.flags = RESIDENT_FLAG_INDEXED;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), new_name, new_de_key_size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						le16_add_cpu(&old_ni->mi.mrec->hard_links, 1);
 | 
				
			||||||
 | 
						old_ni->mi.dirty = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* insert new name in directory */
 | 
				
			||||||
 | 
						err = indx_insert_entry(&new_dir_ni->dir, new_dir_ni, new_de, sbi,
 | 
				
			||||||
 | 
									NULL);
 | 
				
			||||||
 | 
						if (err)
 | 
				
			||||||
 | 
							goto out8;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (IS_DIRSYNC(new_dir))
 | 
				
			||||||
 | 
							err = ntfs_sync_inode(old_inode);
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							mark_inode_dirty(old_inode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
 | 
				
			||||||
 | 
						if (IS_DIRSYNC(old_dir))
 | 
				
			||||||
 | 
							(void)ntfs_sync_inode(old_dir);
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							mark_inode_dirty(old_dir);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (old_dir != new_dir) {
 | 
				
			||||||
 | 
							new_dir->i_mtime = new_dir->i_ctime = old_dir->i_ctime;
 | 
				
			||||||
 | 
							mark_inode_dirty(new_dir);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (old_inode) {
 | 
				
			||||||
 | 
							old_inode->i_ctime = old_dir->i_ctime;
 | 
				
			||||||
 | 
							mark_inode_dirty(old_inode);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = 0;
 | 
				
			||||||
 | 
						/* normal way */
 | 
				
			||||||
 | 
						goto out2;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					out8:
 | 
				
			||||||
 | 
						/* undo
 | 
				
			||||||
 | 
						 * ni_insert_resident(old_ni, new_de_key_size, ATTR_NAME, NULL, 0,
 | 
				
			||||||
 | 
						 *			 &attr, NULL);
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						mi_remove_attr(&old_ni->mi, attr);
 | 
				
			||||||
 | 
					out7:
 | 
				
			||||||
 | 
						/* undo
 | 
				
			||||||
 | 
						 * ni_remove_attr_le(old_ni, attr_from_name(fname), le);
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
					out6:
 | 
				
			||||||
 | 
						/* undo
 | 
				
			||||||
 | 
						 * indx_delete_entry(&old_dir_ni->dir, old_dir_ni,
 | 
				
			||||||
 | 
						 *					fname, fname_full_size(fname),
 | 
				
			||||||
 | 
						 *					sbi);
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
					out5:
 | 
				
			||||||
 | 
						/* undo
 | 
				
			||||||
 | 
						 * ni_remove_attr_le(old_ni, attr_from_name(fname), le);
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
					out4:
 | 
				
			||||||
 | 
						/* undo:
 | 
				
			||||||
 | 
						 * indx_delete_entry(&old_dir_ni->dir, old_dir_ni, old_de + 1,
 | 
				
			||||||
 | 
						 *			old_de->key_size, NULL);
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
					out3:
 | 
				
			||||||
 | 
					out2:
 | 
				
			||||||
 | 
						ni_unlock(old_ni);
 | 
				
			||||||
 | 
						ni_unlock(old_dir_ni);
 | 
				
			||||||
 | 
					out1:
 | 
				
			||||||
 | 
						__putname(old_de);
 | 
				
			||||||
 | 
					out:
 | 
				
			||||||
 | 
						return err;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct dentry *ntfs3_get_parent(struct dentry *child)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct inode *inode = d_inode(child);
 | 
				
			||||||
 | 
						struct ntfs_inode *ni = ntfs_i(inode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						struct ATTR_LIST_ENTRY *le = NULL;
 | 
				
			||||||
 | 
						struct ATTRIB *attr = NULL;
 | 
				
			||||||
 | 
						struct ATTR_FILE_NAME *fname;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
 | 
				
			||||||
 | 
									    NULL))) {
 | 
				
			||||||
 | 
							fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
 | 
				
			||||||
 | 
							if (!fname)
 | 
				
			||||||
 | 
								continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return d_obtain_alias(
 | 
				
			||||||
 | 
								ntfs_iget5(inode->i_sb, &fname->home, NULL));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return ERR_PTR(-ENOENT);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// clang-format off
 | 
				
			||||||
 | 
					const struct inode_operations ntfs_dir_inode_operations = {
 | 
				
			||||||
 | 
						.lookup		= ntfs_lookup,
 | 
				
			||||||
 | 
						.create		= ntfs_create,
 | 
				
			||||||
 | 
						.link		= ntfs_link,
 | 
				
			||||||
 | 
						.unlink		= ntfs_unlink,
 | 
				
			||||||
 | 
						.symlink	= ntfs_symlink,
 | 
				
			||||||
 | 
						.mkdir		= ntfs_mkdir,
 | 
				
			||||||
 | 
						.rmdir		= ntfs_rmdir,
 | 
				
			||||||
 | 
						.mknod		= ntfs_mknod,
 | 
				
			||||||
 | 
						.rename		= ntfs_rename,
 | 
				
			||||||
 | 
						.permission	= ntfs_permission,
 | 
				
			||||||
 | 
						.get_acl	= ntfs_get_acl,
 | 
				
			||||||
 | 
						.set_acl	= ntfs_set_acl,
 | 
				
			||||||
 | 
						.setattr	= ntfs3_setattr,
 | 
				
			||||||
 | 
						.getattr	= ntfs_getattr,
 | 
				
			||||||
 | 
						.listxattr	= ntfs_listxattr,
 | 
				
			||||||
 | 
						.fiemap		= ntfs_fiemap,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const struct inode_operations ntfs_special_inode_operations = {
 | 
				
			||||||
 | 
						.setattr	= ntfs3_setattr,
 | 
				
			||||||
 | 
						.getattr	= ntfs_getattr,
 | 
				
			||||||
 | 
						.listxattr	= ntfs_listxattr,
 | 
				
			||||||
 | 
						.get_acl	= ntfs_get_acl,
 | 
				
			||||||
 | 
						.set_acl	= ntfs_set_acl,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					// clang-format on
 | 
				
			||||||
							
								
								
									
										609
									
								
								fs/ntfs3/record.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										609
									
								
								fs/ntfs3/record.c
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,609 @@
 | 
				
			||||||
 | 
					// SPDX-License-Identifier: GPL-2.0
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <linux/blkdev.h>
 | 
				
			||||||
 | 
					#include <linux/buffer_head.h>
 | 
				
			||||||
 | 
					#include <linux/fs.h>
 | 
				
			||||||
 | 
					#include <linux/nls.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "debug.h"
 | 
				
			||||||
 | 
					#include "ntfs.h"
 | 
				
			||||||
 | 
					#include "ntfs_fs.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static inline int compare_attr(const struct ATTRIB *left, enum ATTR_TYPE type,
 | 
				
			||||||
 | 
								       const __le16 *name, u8 name_len,
 | 
				
			||||||
 | 
								       const u16 *upcase)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						/* First, compare the type codes: */
 | 
				
			||||||
 | 
						int diff = le32_to_cpu(left->type) - le32_to_cpu(type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (diff)
 | 
				
			||||||
 | 
							return diff;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * They have the same type code, so we have to compare the names.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						return ntfs_cmp_names(attr_name(left), left->name_len, name, name_len,
 | 
				
			||||||
 | 
								      upcase, true);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * mi_new_attt_id
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * returns unused attribute id that is less than mrec->next_attr_id
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					static __le16 mi_new_attt_id(struct mft_inode *mi)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						u16 free_id, max_id, t16;
 | 
				
			||||||
 | 
						struct MFT_REC *rec = mi->mrec;
 | 
				
			||||||
 | 
						struct ATTRIB *attr;
 | 
				
			||||||
 | 
						__le16 id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						id = rec->next_attr_id;
 | 
				
			||||||
 | 
						free_id = le16_to_cpu(id);
 | 
				
			||||||
 | 
						if (free_id < 0x7FFF) {
 | 
				
			||||||
 | 
							rec->next_attr_id = cpu_to_le16(free_id + 1);
 | 
				
			||||||
 | 
							return id;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* One record can store up to 1024/24 ~= 42 attributes */
 | 
				
			||||||
 | 
						free_id = 0;
 | 
				
			||||||
 | 
						max_id = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						attr = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (;;) {
 | 
				
			||||||
 | 
							attr = mi_enum_attr(mi, attr);
 | 
				
			||||||
 | 
							if (!attr) {
 | 
				
			||||||
 | 
								rec->next_attr_id = cpu_to_le16(max_id + 1);
 | 
				
			||||||
 | 
								mi->dirty = true;
 | 
				
			||||||
 | 
								return cpu_to_le16(free_id);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							t16 = le16_to_cpu(attr->id);
 | 
				
			||||||
 | 
							if (t16 == free_id) {
 | 
				
			||||||
 | 
								free_id += 1;
 | 
				
			||||||
 | 
								attr = NULL;
 | 
				
			||||||
 | 
							} else if (max_id < t16)
 | 
				
			||||||
 | 
								max_id = t16;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int mi_get(struct ntfs_sb_info *sbi, CLST rno, struct mft_inode **mi)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
						struct mft_inode *m = ntfs_zalloc(sizeof(struct mft_inode));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!m)
 | 
				
			||||||
 | 
							return -ENOMEM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = mi_init(m, sbi, rno);
 | 
				
			||||||
 | 
						if (err) {
 | 
				
			||||||
 | 
							ntfs_free(m);
 | 
				
			||||||
 | 
							return err;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = mi_read(m, false);
 | 
				
			||||||
 | 
						if (err) {
 | 
				
			||||||
 | 
							mi_put(m);
 | 
				
			||||||
 | 
							return err;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						*mi = m;
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void mi_put(struct mft_inode *mi)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						mi_clear(mi);
 | 
				
			||||||
 | 
						ntfs_free(mi);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int mi_init(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						mi->sbi = sbi;
 | 
				
			||||||
 | 
						mi->rno = rno;
 | 
				
			||||||
 | 
						mi->mrec = ntfs_malloc(sbi->record_size);
 | 
				
			||||||
 | 
						if (!mi->mrec)
 | 
				
			||||||
 | 
							return -ENOMEM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * mi_read
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * reads MFT data
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					int mi_read(struct mft_inode *mi, bool is_mft)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
						struct MFT_REC *rec = mi->mrec;
 | 
				
			||||||
 | 
						struct ntfs_sb_info *sbi = mi->sbi;
 | 
				
			||||||
 | 
						u32 bpr = sbi->record_size;
 | 
				
			||||||
 | 
						u64 vbo = (u64)mi->rno << sbi->record_bits;
 | 
				
			||||||
 | 
						struct ntfs_inode *mft_ni = sbi->mft.ni;
 | 
				
			||||||
 | 
						struct runs_tree *run = mft_ni ? &mft_ni->file.run : NULL;
 | 
				
			||||||
 | 
						struct rw_semaphore *rw_lock = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (is_mounted(sbi)) {
 | 
				
			||||||
 | 
							if (!is_mft) {
 | 
				
			||||||
 | 
								rw_lock = &mft_ni->file.run_lock;
 | 
				
			||||||
 | 
								down_read(rw_lock);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = ntfs_read_bh(sbi, run, vbo, &rec->rhdr, bpr, &mi->nb);
 | 
				
			||||||
 | 
						if (rw_lock)
 | 
				
			||||||
 | 
							up_read(rw_lock);
 | 
				
			||||||
 | 
						if (!err)
 | 
				
			||||||
 | 
							goto ok;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (err == -E_NTFS_FIXUP) {
 | 
				
			||||||
 | 
							mi->dirty = true;
 | 
				
			||||||
 | 
							goto ok;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (err != -ENOENT)
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (rw_lock) {
 | 
				
			||||||
 | 
							ni_lock(mft_ni);
 | 
				
			||||||
 | 
							down_write(rw_lock);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						err = attr_load_runs_vcn(mft_ni, ATTR_DATA, NULL, 0, &mft_ni->file.run,
 | 
				
			||||||
 | 
									 vbo >> sbi->cluster_bits);
 | 
				
			||||||
 | 
						if (rw_lock) {
 | 
				
			||||||
 | 
							up_write(rw_lock);
 | 
				
			||||||
 | 
							ni_unlock(mft_ni);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if (err)
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (rw_lock)
 | 
				
			||||||
 | 
							down_read(rw_lock);
 | 
				
			||||||
 | 
						err = ntfs_read_bh(sbi, run, vbo, &rec->rhdr, bpr, &mi->nb);
 | 
				
			||||||
 | 
						if (rw_lock)
 | 
				
			||||||
 | 
							up_read(rw_lock);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (err == -E_NTFS_FIXUP) {
 | 
				
			||||||
 | 
							mi->dirty = true;
 | 
				
			||||||
 | 
							goto ok;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if (err)
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ok:
 | 
				
			||||||
 | 
						/* check field 'total' only here */
 | 
				
			||||||
 | 
						if (le32_to_cpu(rec->total) != bpr) {
 | 
				
			||||||
 | 
							err = -EINVAL;
 | 
				
			||||||
 | 
							goto out;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					out:
 | 
				
			||||||
 | 
						return err;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						const struct MFT_REC *rec = mi->mrec;
 | 
				
			||||||
 | 
						u32 used = le32_to_cpu(rec->used);
 | 
				
			||||||
 | 
						u32 t32, off, asize;
 | 
				
			||||||
 | 
						u16 t16;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!attr) {
 | 
				
			||||||
 | 
							u32 total = le32_to_cpu(rec->total);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							off = le16_to_cpu(rec->attr_off);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (used > total)
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (off >= used || off < MFTRECORD_FIXUP_OFFSET_1 ||
 | 
				
			||||||
 | 
							    !IsDwordAligned(off)) {
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							/* Skip non-resident records */
 | 
				
			||||||
 | 
							if (!is_rec_inuse(rec))
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							attr = Add2Ptr(rec, off);
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							/* Check if input attr inside record */
 | 
				
			||||||
 | 
							off = PtrOffset(rec, attr);
 | 
				
			||||||
 | 
							if (off >= used)
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							asize = le32_to_cpu(attr->size);
 | 
				
			||||||
 | 
							if (asize < SIZEOF_RESIDENT) {
 | 
				
			||||||
 | 
								/* Impossible 'cause we should not return such attribute */
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							attr = Add2Ptr(attr, asize);
 | 
				
			||||||
 | 
							off += asize;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						asize = le32_to_cpu(attr->size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Can we use the first field (attr->type) */
 | 
				
			||||||
 | 
						if (off + 8 > used) {
 | 
				
			||||||
 | 
							static_assert(QuadAlign(sizeof(enum ATTR_TYPE)) == 8);
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (attr->type == ATTR_END) {
 | 
				
			||||||
 | 
							/* end of enumeration */
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* 0x100 is last known attribute for now*/
 | 
				
			||||||
 | 
						t32 = le32_to_cpu(attr->type);
 | 
				
			||||||
 | 
						if ((t32 & 0xf) || (t32 > 0x100))
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Check boundary */
 | 
				
			||||||
 | 
						if (off + asize > used)
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Check size of attribute */
 | 
				
			||||||
 | 
						if (!attr->non_res) {
 | 
				
			||||||
 | 
							if (asize < SIZEOF_RESIDENT)
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							t16 = le16_to_cpu(attr->res.data_off);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (t16 > asize)
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							t32 = le32_to_cpu(attr->res.data_size);
 | 
				
			||||||
 | 
							if (t16 + t32 > asize)
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return attr;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Check some nonresident fields */
 | 
				
			||||||
 | 
						if (attr->name_len &&
 | 
				
			||||||
 | 
						    le16_to_cpu(attr->name_off) + sizeof(short) * attr->name_len >
 | 
				
			||||||
 | 
							    le16_to_cpu(attr->nres.run_off)) {
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (attr->nres.svcn || !is_attr_ext(attr)) {
 | 
				
			||||||
 | 
							if (asize + 8 < SIZEOF_NONRESIDENT)
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (attr->nres.c_unit)
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
						} else if (asize + 8 < SIZEOF_NONRESIDENT_EX)
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return attr;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * mi_find_attr
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * finds the attribute by type and name and id
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
 | 
				
			||||||
 | 
								    enum ATTR_TYPE type, const __le16 *name,
 | 
				
			||||||
 | 
								    size_t name_len, const __le16 *id)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						u32 type_in = le32_to_cpu(type);
 | 
				
			||||||
 | 
						u32 atype;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					next_attr:
 | 
				
			||||||
 | 
						attr = mi_enum_attr(mi, attr);
 | 
				
			||||||
 | 
						if (!attr)
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						atype = le32_to_cpu(attr->type);
 | 
				
			||||||
 | 
						if (atype > type_in)
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (atype < type_in)
 | 
				
			||||||
 | 
							goto next_attr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (attr->name_len != name_len)
 | 
				
			||||||
 | 
							goto next_attr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (name_len && memcmp(attr_name(attr), name, name_len * sizeof(short)))
 | 
				
			||||||
 | 
							goto next_attr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (id && *id != attr->id)
 | 
				
			||||||
 | 
							goto next_attr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return attr;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int mi_write(struct mft_inode *mi, int wait)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct MFT_REC *rec;
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
						struct ntfs_sb_info *sbi;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!mi->dirty)
 | 
				
			||||||
 | 
							return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						sbi = mi->sbi;
 | 
				
			||||||
 | 
						rec = mi->mrec;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = ntfs_write_bh(sbi, &rec->rhdr, &mi->nb, wait);
 | 
				
			||||||
 | 
						if (err)
 | 
				
			||||||
 | 
							return err;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (mi->rno < sbi->mft.recs_mirr)
 | 
				
			||||||
 | 
							sbi->flags |= NTFS_FLAGS_MFTMIRR;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mi->dirty = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
 | 
				
			||||||
 | 
							  __le16 flags, bool is_mft)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err;
 | 
				
			||||||
 | 
						u16 seq = 1;
 | 
				
			||||||
 | 
						struct MFT_REC *rec;
 | 
				
			||||||
 | 
						u64 vbo = (u64)rno << sbi->record_bits;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err = mi_init(mi, sbi, rno);
 | 
				
			||||||
 | 
						if (err)
 | 
				
			||||||
 | 
							return err;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rec = mi->mrec;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (rno == MFT_REC_MFT) {
 | 
				
			||||||
 | 
							;
 | 
				
			||||||
 | 
						} else if (rno < MFT_REC_FREE) {
 | 
				
			||||||
 | 
							seq = rno;
 | 
				
			||||||
 | 
						} else if (rno >= sbi->mft.used) {
 | 
				
			||||||
 | 
							;
 | 
				
			||||||
 | 
						} else if (mi_read(mi, is_mft)) {
 | 
				
			||||||
 | 
							;
 | 
				
			||||||
 | 
						} else if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
 | 
				
			||||||
 | 
							/* Record is reused. Update its sequence number */
 | 
				
			||||||
 | 
							seq = le16_to_cpu(rec->seq) + 1;
 | 
				
			||||||
 | 
							if (!seq)
 | 
				
			||||||
 | 
								seq = 1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						memcpy(rec, sbi->new_rec, sbi->record_size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rec->seq = cpu_to_le16(seq);
 | 
				
			||||||
 | 
						rec->flags = RECORD_FLAG_IN_USE | flags;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mi->dirty = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!mi->nb.nbufs) {
 | 
				
			||||||
 | 
							struct ntfs_inode *ni = sbi->mft.ni;
 | 
				
			||||||
 | 
							bool lock = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (is_mounted(sbi) && !is_mft) {
 | 
				
			||||||
 | 
								down_read(&ni->file.run_lock);
 | 
				
			||||||
 | 
								lock = true;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							err = ntfs_get_bh(sbi, &ni->file.run, vbo, sbi->record_size,
 | 
				
			||||||
 | 
									  &mi->nb);
 | 
				
			||||||
 | 
							if (lock)
 | 
				
			||||||
 | 
								up_read(&ni->file.run_lock);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return err;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * mi_mark_free
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * marks record as unused and marks it as free in bitmap
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					void mi_mark_free(struct mft_inode *mi)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						CLST rno = mi->rno;
 | 
				
			||||||
 | 
						struct ntfs_sb_info *sbi = mi->sbi;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (rno >= MFT_REC_RESERVED && rno < MFT_REC_FREE) {
 | 
				
			||||||
 | 
							ntfs_clear_mft_tail(sbi, rno, rno + 1);
 | 
				
			||||||
 | 
							mi->dirty = false;
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (mi->mrec) {
 | 
				
			||||||
 | 
							clear_rec_inuse(mi->mrec);
 | 
				
			||||||
 | 
							mi->dirty = true;
 | 
				
			||||||
 | 
							mi_write(mi, 0);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						ntfs_mark_rec_free(sbi, rno);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * mi_insert_attr
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * reserves space for new attribute
 | 
				
			||||||
 | 
					 * returns not full constructed attribute or NULL if not possible to create
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					struct ATTRIB *mi_insert_attr(struct mft_inode *mi, enum ATTR_TYPE type,
 | 
				
			||||||
 | 
								      const __le16 *name, u8 name_len, u32 asize,
 | 
				
			||||||
 | 
								      u16 name_off)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						size_t tail;
 | 
				
			||||||
 | 
						struct ATTRIB *attr;
 | 
				
			||||||
 | 
						__le16 id;
 | 
				
			||||||
 | 
						struct MFT_REC *rec = mi->mrec;
 | 
				
			||||||
 | 
						struct ntfs_sb_info *sbi = mi->sbi;
 | 
				
			||||||
 | 
						u32 used = le32_to_cpu(rec->used);
 | 
				
			||||||
 | 
						const u16 *upcase = sbi->upcase;
 | 
				
			||||||
 | 
						int diff;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Can we insert mi attribute? */
 | 
				
			||||||
 | 
						if (used + asize > mi->sbi->record_size)
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * Scan through the list of attributes to find the point
 | 
				
			||||||
 | 
						 * at which we should insert it.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						attr = NULL;
 | 
				
			||||||
 | 
						while ((attr = mi_enum_attr(mi, attr))) {
 | 
				
			||||||
 | 
							diff = compare_attr(attr, type, name, name_len, upcase);
 | 
				
			||||||
 | 
							if (diff > 0)
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							if (diff < 0)
 | 
				
			||||||
 | 
								continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (!is_attr_indexed(attr))
 | 
				
			||||||
 | 
								return NULL;
 | 
				
			||||||
 | 
							break;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!attr) {
 | 
				
			||||||
 | 
							tail = 8; /* not used, just to suppress warning */
 | 
				
			||||||
 | 
							attr = Add2Ptr(rec, used - 8);
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							tail = used - PtrOffset(rec, attr);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						id = mi_new_attt_id(mi);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						memmove(Add2Ptr(attr, asize), attr, tail);
 | 
				
			||||||
 | 
						memset(attr, 0, asize);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						attr->type = type;
 | 
				
			||||||
 | 
						attr->size = cpu_to_le32(asize);
 | 
				
			||||||
 | 
						attr->name_len = name_len;
 | 
				
			||||||
 | 
						attr->name_off = cpu_to_le16(name_off);
 | 
				
			||||||
 | 
						attr->id = id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						memmove(Add2Ptr(attr, name_off), name, name_len * sizeof(short));
 | 
				
			||||||
 | 
						rec->used = cpu_to_le32(used + asize);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mi->dirty = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return attr;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * mi_remove_attr
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * removes the attribute from record
 | 
				
			||||||
 | 
					 * NOTE: The source attr will point to next attribute
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					bool mi_remove_attr(struct mft_inode *mi, struct ATTRIB *attr)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct MFT_REC *rec = mi->mrec;
 | 
				
			||||||
 | 
						u32 aoff = PtrOffset(rec, attr);
 | 
				
			||||||
 | 
						u32 used = le32_to_cpu(rec->used);
 | 
				
			||||||
 | 
						u32 asize = le32_to_cpu(attr->size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (aoff + asize > used)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						used -= asize;
 | 
				
			||||||
 | 
						memmove(attr, Add2Ptr(attr, asize), used - aoff);
 | 
				
			||||||
 | 
						rec->used = cpu_to_le32(used);
 | 
				
			||||||
 | 
						mi->dirty = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return true;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* bytes = "new attribute size" - "old attribute size" */
 | 
				
			||||||
 | 
					bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct MFT_REC *rec = mi->mrec;
 | 
				
			||||||
 | 
						u32 aoff = PtrOffset(rec, attr);
 | 
				
			||||||
 | 
						u32 total, used = le32_to_cpu(rec->used);
 | 
				
			||||||
 | 
						u32 nsize, asize = le32_to_cpu(attr->size);
 | 
				
			||||||
 | 
						u32 rsize = le32_to_cpu(attr->res.data_size);
 | 
				
			||||||
 | 
						int tail = (int)(used - aoff - asize);
 | 
				
			||||||
 | 
						int dsize;
 | 
				
			||||||
 | 
						char *next;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (tail < 0 || aoff >= used)
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!bytes)
 | 
				
			||||||
 | 
							return true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						total = le32_to_cpu(rec->total);
 | 
				
			||||||
 | 
						next = Add2Ptr(attr, asize);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (bytes > 0) {
 | 
				
			||||||
 | 
							dsize = QuadAlign(bytes);
 | 
				
			||||||
 | 
							if (used + dsize > total)
 | 
				
			||||||
 | 
								return false;
 | 
				
			||||||
 | 
							nsize = asize + dsize;
 | 
				
			||||||
 | 
							// move tail
 | 
				
			||||||
 | 
							memmove(next + dsize, next, tail);
 | 
				
			||||||
 | 
							memset(next, 0, dsize);
 | 
				
			||||||
 | 
							used += dsize;
 | 
				
			||||||
 | 
							rsize += dsize;
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							dsize = QuadAlign(-bytes);
 | 
				
			||||||
 | 
							if (dsize > asize)
 | 
				
			||||||
 | 
								return false;
 | 
				
			||||||
 | 
							nsize = asize - dsize;
 | 
				
			||||||
 | 
							memmove(next - dsize, next, tail);
 | 
				
			||||||
 | 
							used -= dsize;
 | 
				
			||||||
 | 
							rsize -= dsize;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rec->used = cpu_to_le32(used);
 | 
				
			||||||
 | 
						attr->size = cpu_to_le32(nsize);
 | 
				
			||||||
 | 
						if (!attr->non_res)
 | 
				
			||||||
 | 
							attr->res.data_size = cpu_to_le32(rsize);
 | 
				
			||||||
 | 
						mi->dirty = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return true;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
 | 
				
			||||||
 | 
							 struct runs_tree *run, CLST len)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int err = 0;
 | 
				
			||||||
 | 
						struct ntfs_sb_info *sbi = mi->sbi;
 | 
				
			||||||
 | 
						u32 new_run_size;
 | 
				
			||||||
 | 
						CLST plen;
 | 
				
			||||||
 | 
						struct MFT_REC *rec = mi->mrec;
 | 
				
			||||||
 | 
						CLST svcn = le64_to_cpu(attr->nres.svcn);
 | 
				
			||||||
 | 
						u32 used = le32_to_cpu(rec->used);
 | 
				
			||||||
 | 
						u32 aoff = PtrOffset(rec, attr);
 | 
				
			||||||
 | 
						u32 asize = le32_to_cpu(attr->size);
 | 
				
			||||||
 | 
						char *next = Add2Ptr(attr, asize);
 | 
				
			||||||
 | 
						u16 run_off = le16_to_cpu(attr->nres.run_off);
 | 
				
			||||||
 | 
						u32 run_size = asize - run_off;
 | 
				
			||||||
 | 
						u32 tail = used - aoff - asize;
 | 
				
			||||||
 | 
						u32 dsize = sbi->record_size - used;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Make a maximum gap in current record */
 | 
				
			||||||
 | 
						memmove(next + dsize, next, tail);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* Pack as much as possible */
 | 
				
			||||||
 | 
						err = run_pack(run, svcn, len, Add2Ptr(attr, run_off), run_size + dsize,
 | 
				
			||||||
 | 
							       &plen);
 | 
				
			||||||
 | 
						if (err < 0) {
 | 
				
			||||||
 | 
							memmove(next, next + dsize, tail);
 | 
				
			||||||
 | 
							return err;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						new_run_size = QuadAlign(err);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						memmove(next + new_run_size - run_size, next + dsize, tail);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						attr->size = cpu_to_le32(asize + new_run_size - run_size);
 | 
				
			||||||
 | 
						attr->nres.evcn = cpu_to_le64(svcn + plen - 1);
 | 
				
			||||||
 | 
						rec->used = cpu_to_le32(used + new_run_size - run_size);
 | 
				
			||||||
 | 
						mi->dirty = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										1111
									
								
								fs/ntfs3/run.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1111
									
								
								fs/ntfs3/run.c
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
		Loading…
	
		Reference in a new issue