1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
10 #include <linux/ctype.h>
11 #include <linux/posix_acl.h>
18 * fill_name_de - Format NTFS_DE in @buf.
20 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
21 const struct cpu_str *uni)
24 struct NTFS_DE *e = buf;
26 struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
28 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
29 e->ref.high = fname->home.high = 0;
34 __le16 *uname = fname->name;
35 const u16 *name_cpu = uni->name;
38 *uname++ = cpu_to_le16(*name_cpu++);
40 memcpy(fname->name, uni->name, uni->len * sizeof(u16));
42 fname->name_len = uni->len;
45 /* Convert input string to unicode. */
46 err = ntfs_nls_to_utf16(sbi, name->name, name->len,
47 (struct cpu_str *)&fname->name_len,
48 NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN);
53 fname->type = FILE_NAME_POSIX;
54 data_size = fname_full_size(fname);
56 e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
57 e->key_size = cpu_to_le16(data_size);
65 * ntfs_lookup - inode_operations::lookup
67 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
70 struct ntfs_inode *ni = ntfs_i(dir);
71 struct cpu_str *uni = __getname();
76 inode = ERR_PTR(-ENOMEM);
78 err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
79 dentry->d_name.len, uni, NTFS_NAME_LEN,
85 inode = dir_search_u(dir, uni, NULL);
92 * Check for a null pointer
93 * If the MFT record of ntfs inode is not a base record, inode->i_op can be NULL.
94 * This causes null pointer dereference in d_splice_alias().
96 if (!IS_ERR_OR_NULL(inode) && !inode->i_op) {
98 inode = ERR_PTR(-EINVAL);
101 return d_splice_alias(inode, dentry);
105 * ntfs_create - inode_operations::create
107 static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
108 struct dentry *dentry, umode_t mode, bool excl)
110 return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0,
115 * ntfs_mknod - inode_operations::mknod
117 static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
118 struct dentry *dentry, umode_t mode, dev_t rdev)
120 return ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0,
125 * ntfs_link - inode_operations::link
127 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
130 struct inode *inode = d_inode(ode);
131 struct ntfs_inode *ni = ntfs_i(inode);
133 if (S_ISDIR(inode->i_mode))
136 if (inode->i_nlink >= NTFS_LINK_MAX)
139 ni_lock_dir(ntfs_i(dir));
146 err = ntfs_link_inode(inode, de);
149 inode_set_ctime_current(inode);
150 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
151 mark_inode_dirty(inode);
152 mark_inode_dirty(dir);
153 d_instantiate(de, inode);
161 ni_unlock(ntfs_i(dir));
167 * ntfs_unlink - inode_operations::unlink
169 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
171 struct ntfs_inode *ni = ntfs_i(dir);
174 if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
179 err = ntfs_unlink_inode(dir, dentry);
187 * ntfs_symlink - inode_operations::symlink
189 static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
190 struct dentry *dentry, const char *symname)
192 u32 size = strlen(symname);
194 if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
197 return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0,
198 symname, size, NULL);
202 * ntfs_mkdir- inode_operations::mkdir
204 static int ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
205 struct dentry *dentry, umode_t mode)
207 return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFDIR | mode, 0,
212 * ntfs_rmdir - inode_operations::rmdir
214 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
216 struct ntfs_inode *ni = ntfs_i(dir);
219 if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
224 err = ntfs_unlink_inode(dir, dentry);
232 * ntfs_rename - inode_operations::rename
234 static int ntfs_rename(struct mnt_idmap *idmap, struct inode *dir,
235 struct dentry *dentry, struct inode *new_dir,
236 struct dentry *new_dentry, u32 flags)
239 struct super_block *sb = dir->i_sb;
240 struct ntfs_sb_info *sbi = sb->s_fs_info;
241 struct ntfs_inode *dir_ni = ntfs_i(dir);
242 struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
243 struct inode *inode = d_inode(dentry);
244 struct ntfs_inode *ni = ntfs_i(inode);
245 struct inode *new_inode = d_inode(new_dentry);
246 struct NTFS_DE *de, *new_de;
247 bool is_same, is_bad;
249 * de - memory of PATH_MAX bytes:
250 * [0-1024) - original name (dentry->d_name)
251 * [1024-2048) - paired to original name, usually DOS variant of dentry->d_name
252 * [2048-3072) - new name (new_dentry->d_name)
254 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
255 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
257 static_assert(PATH_MAX >= 4 * 1024);
259 if (unlikely(ntfs3_forced_shutdown(sb)))
262 if (flags & ~RENAME_NOREPLACE)
265 is_same = dentry->d_name.len == new_dentry->d_name.len &&
266 !memcmp(dentry->d_name.name, new_dentry->d_name.name,
269 if (is_same && dir == new_dir) {
274 if (ntfs_is_meta_file(sbi, inode->i_ino)) {
275 /* Should we print an error? */
280 /* Target name exists. Unlink it. */
282 ni_lock_dir(new_dir_ni);
283 err = ntfs_unlink_inode(new_dir, new_dentry);
284 ni_unlock(new_dir_ni);
290 /* Allocate PATH_MAX bytes. */
295 /* Translate dentry->d_name into unicode form. */
296 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
304 /* Translate new_dentry->d_name into unicode form. */
305 new_de = Add2Ptr(de, 2048);
306 err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
313 if (dir_ni != new_dir_ni)
314 ni_lock_dir2(new_dir_ni);
317 err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
319 /* Restore after failed rename failed too. */
320 _ntfs_bad_inode(inode);
322 simple_rename_timestamp(dir, dentry, new_dir, new_dentry);
323 mark_inode_dirty(inode);
324 mark_inode_dirty(dir);
326 mark_inode_dirty(new_dir);
329 ntfs_sync_inode(dir);
331 if (IS_DIRSYNC(new_dir))
332 ntfs_sync_inode(inode);
335 if (dir_ni != new_dir_ni)
336 ni_unlock(new_dir_ni);
344 struct dentry *ntfs3_get_parent(struct dentry *child)
346 struct inode *inode = d_inode(child);
347 struct ntfs_inode *ni = ntfs_i(inode);
349 struct ATTR_LIST_ENTRY *le = NULL;
350 struct ATTRIB *attr = NULL;
351 struct ATTR_FILE_NAME *fname;
353 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
355 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
359 return d_obtain_alias(
360 ntfs_iget5(inode->i_sb, &fname->home, NULL));
363 return ERR_PTR(-ENOENT);
367 * dentry_operations::d_hash
369 static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
371 struct ntfs_sb_info *sbi;
372 const char *n = name->name;
373 unsigned int len = name->len;
379 /* First try fast implementation. */
380 hash = init_name_hash(dentry);
384 name->hash = end_name_hash(hash);
392 hash = partial_name_hash(toupper(c), hash);
396 * Try slow way with current upcase table
398 uni = kmem_cache_alloc(names_cachep, GFP_NOWAIT);
402 sbi = dentry->d_sb->s_fs_info;
404 err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
414 hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
415 init_name_hash(dentry));
416 name->hash = end_name_hash(hash);
420 kmem_cache_free(names_cachep, uni);
425 * dentry_operations::d_compare
427 static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
428 const char *str, const struct qstr *name)
430 struct ntfs_sb_info *sbi;
432 const char *n1 = str;
433 const char *n2 = name->name;
434 unsigned int len2 = name->len;
435 unsigned int lm = min(len1, len2);
436 unsigned char c1, c2;
437 struct cpu_str *uni1;
440 /* First try fast implementation. */
445 if ((c1 = *n1++) == (c2 = *n2++))
448 if (c1 >= 0x80 || c2 >= 0x80)
451 if (toupper(c1) != toupper(c2))
456 * Try slow way with current upcase table
458 sbi = dentry->d_sb->s_fs_info;
463 ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
473 uni2 = Add2Ptr(uni1, 2048);
475 ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
476 (struct cpu_str *)uni2, NTFS_NAME_LEN,
477 UTF16_LITTLE_ENDIAN);
486 ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
494 const struct inode_operations ntfs_dir_inode_operations = {
495 .lookup = ntfs_lookup,
496 .create = ntfs_create,
498 .unlink = ntfs_unlink,
499 .symlink = ntfs_symlink,
503 .rename = ntfs_rename,
504 .get_acl = ntfs_get_acl,
505 .set_acl = ntfs_set_acl,
506 .setattr = ntfs_setattr,
507 .getattr = ntfs_getattr,
508 .listxattr = ntfs_listxattr,
509 .fiemap = ntfs_fiemap,
510 .fileattr_get = ntfs_fileattr_get,
511 .fileattr_set = ntfs_fileattr_set,
514 const struct inode_operations ntfs_special_inode_operations = {
515 .setattr = ntfs_setattr,
516 .getattr = ntfs_getattr,
517 .listxattr = ntfs_listxattr,
518 .get_acl = ntfs_get_acl,
519 .set_acl = ntfs_set_acl,
522 const struct dentry_operations ntfs_dentry_ops = {
523 .d_hash = ntfs_d_hash,
524 .d_compare = ntfs_d_compare,