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);
91 return d_splice_alias(inode, dentry);
95 * ntfs_create - inode_operations::create
97 static int ntfs_create(struct user_namespace *mnt_userns, struct inode *dir,
98 struct dentry *dentry, umode_t mode, bool excl)
102 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFREG | mode,
105 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
111 * inode_operations::mknod
113 static int ntfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
114 struct dentry *dentry, umode_t mode, dev_t rdev)
118 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, mode, rdev,
121 return IS_ERR(inode) ? PTR_ERR(inode) : 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 dir->i_ctime = dir->i_mtime = inode->i_ctime =
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);
176 err = ntfs_unlink_inode(dir, dentry);
184 * ntfs_symlink - inode_operations::symlink
186 static int ntfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
187 struct dentry *dentry, const char *symname)
189 u32 size = strlen(symname);
192 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFLNK | 0777,
193 0, symname, size, NULL);
195 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
199 * ntfs_mkdir- inode_operations::mkdir
201 static int ntfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
202 struct dentry *dentry, umode_t mode)
206 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFDIR | mode,
209 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
213 * ntfs_rmdir - inode_operations::rmdir
215 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
217 struct ntfs_inode *ni = ntfs_i(dir);
222 err = ntfs_unlink_inode(dir, dentry);
230 * ntfs_rename - inode_operations::rename
232 static int ntfs_rename(struct user_namespace *mnt_userns, struct inode *dir,
233 struct dentry *dentry, struct inode *new_dir,
234 struct dentry *new_dentry, u32 flags)
237 struct super_block *sb = dir->i_sb;
238 struct ntfs_sb_info *sbi = sb->s_fs_info;
239 struct ntfs_inode *dir_ni = ntfs_i(dir);
240 struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
241 struct inode *inode = d_inode(dentry);
242 struct ntfs_inode *ni = ntfs_i(inode);
243 struct inode *new_inode = d_inode(new_dentry);
244 struct NTFS_DE *de, *new_de;
245 bool is_same, is_bad;
247 * de - memory of PATH_MAX bytes:
248 * [0-1024) - original name (dentry->d_name)
249 * [1024-2048) - paired to original name, usually DOS variant of dentry->d_name
250 * [2048-3072) - new name (new_dentry->d_name)
252 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
253 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
255 static_assert(PATH_MAX >= 4 * 1024);
257 if (flags & ~RENAME_NOREPLACE)
260 is_same = dentry->d_name.len == new_dentry->d_name.len &&
261 !memcmp(dentry->d_name.name, new_dentry->d_name.name,
264 if (is_same && dir == new_dir) {
269 if (ntfs_is_meta_file(sbi, inode->i_ino)) {
270 /* Should we print an error? */
275 /* Target name exists. Unlink it. */
277 ni_lock_dir(new_dir_ni);
278 err = ntfs_unlink_inode(new_dir, new_dentry);
279 ni_unlock(new_dir_ni);
285 /* Allocate PATH_MAX bytes. */
290 /* Translate dentry->d_name into unicode form. */
291 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
299 /* Translate new_dentry->d_name into unicode form. */
300 new_de = Add2Ptr(de, 2048);
301 err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
308 if (dir_ni != new_dir_ni)
309 ni_lock_dir2(new_dir_ni);
312 err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
314 /* Restore after failed rename failed too. */
315 _ntfs_bad_inode(inode);
317 inode->i_ctime = dir->i_ctime = dir->i_mtime =
319 mark_inode_dirty(inode);
320 mark_inode_dirty(dir);
321 if (dir != new_dir) {
322 new_dir->i_mtime = new_dir->i_ctime = dir->i_ctime;
323 mark_inode_dirty(new_dir);
327 ntfs_sync_inode(dir);
329 if (IS_DIRSYNC(new_dir))
330 ntfs_sync_inode(inode);
333 if (dir_ni != new_dir_ni)
334 ni_unlock(new_dir_ni);
345 * inode_operations::atomic_open
347 static int ntfs_atomic_open(struct inode *dir, struct dentry *dentry,
348 struct file *file, u32 flags, umode_t mode)
352 struct ntfs_fnd *fnd = NULL;
353 struct ntfs_inode *ni = ntfs_i(dir);
354 struct dentry *d = NULL;
355 struct cpu_str *uni = __getname();
361 err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
362 dentry->d_name.len, uni, NTFS_NAME_LEN,
367 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
368 if (IS_POSIXACL(dir)) {
370 * Load in cache current acl to avoid ni_lock(dir):
371 * ntfs_create_inode -> ntfs_init_acl -> posix_acl_create ->
372 * ntfs_get_acl -> ntfs_get_acl_ex -> ni_lock
374 struct posix_acl *p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
380 posix_acl_release(p);
384 if (d_in_lookup(dentry)) {
393 d = d_splice_alias(dir_search_u(dir, uni, fnd), dentry);
404 if (!(flags & O_CREAT) || d_really_is_positive(dentry)) {
405 err = finish_no_open(file, d);
409 file->f_mode |= FMODE_CREATED;
412 * fnd contains tree's path to insert to.
413 * If fnd is not NULL then dir is locked.
417 * Unfortunately I don't know how to get here correct 'struct nameidata *nd'
418 * or 'struct user_namespace *mnt_userns'.
419 * See atomic_open in fs/namei.c.
420 * This is why xfstest/633 failed.
421 * Looks like ntfs_atomic_open must accept 'struct user_namespace *mnt_userns' as argument.
424 inode = ntfs_create_inode(&init_user_ns, dir, dentry, uni, mode, 0,
426 err = IS_ERR(inode) ? PTR_ERR(inode)
427 : finish_open(file, dentry, ntfs_file_open);
440 struct dentry *ntfs3_get_parent(struct dentry *child)
442 struct inode *inode = d_inode(child);
443 struct ntfs_inode *ni = ntfs_i(inode);
445 struct ATTR_LIST_ENTRY *le = NULL;
446 struct ATTRIB *attr = NULL;
447 struct ATTR_FILE_NAME *fname;
449 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
451 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
455 return d_obtain_alias(
456 ntfs_iget5(inode->i_sb, &fname->home, NULL));
459 return ERR_PTR(-ENOENT);
463 * dentry_operations::d_hash
465 static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
467 struct ntfs_sb_info *sbi;
468 const char *n = name->name;
469 unsigned int len = name->len;
475 /* First try fast implementation. */
476 hash = init_name_hash(dentry);
480 name->hash = end_name_hash(hash);
488 hash = partial_name_hash(toupper(c), hash);
492 * Try slow way with current upcase table
498 sbi = dentry->d_sb->s_fs_info;
500 err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
510 hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
511 init_name_hash(dentry));
512 name->hash = end_name_hash(hash);
521 * dentry_operations::d_compare
523 static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
524 const char *str, const struct qstr *name)
526 struct ntfs_sb_info *sbi;
528 const char *n1 = str;
529 const char *n2 = name->name;
530 unsigned int len2 = name->len;
531 unsigned int lm = min(len1, len2);
532 unsigned char c1, c2;
533 struct cpu_str *uni1;
536 /* First try fast implementation. */
541 if ((c1 = *n1++) == (c2 = *n2++))
544 if (c1 >= 0x80 || c2 >= 0x80)
547 if (toupper(c1) != toupper(c2))
552 * Try slow way with current upcase table
554 sbi = dentry->d_sb->s_fs_info;
559 ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
569 uni2 = Add2Ptr(uni1, 2048);
571 ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
572 (struct cpu_str *)uni2, NTFS_NAME_LEN,
573 UTF16_LITTLE_ENDIAN);
582 ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
590 const struct inode_operations ntfs_dir_inode_operations = {
591 .lookup = ntfs_lookup,
592 .create = ntfs_create,
594 .unlink = ntfs_unlink,
595 .symlink = ntfs_symlink,
599 .rename = ntfs_rename,
600 .permission = ntfs_permission,
601 .get_inode_acl = ntfs_get_acl,
602 .set_acl = ntfs_set_acl,
603 .setattr = ntfs3_setattr,
604 .getattr = ntfs_getattr,
605 .listxattr = ntfs_listxattr,
606 .atomic_open = ntfs_atomic_open,
607 .fiemap = ntfs_fiemap,
610 const struct inode_operations ntfs_special_inode_operations = {
611 .setattr = ntfs3_setattr,
612 .getattr = ntfs_getattr,
613 .listxattr = ntfs_listxattr,
614 .get_inode_acl = ntfs_get_acl,
615 .set_acl = ntfs_set_acl,
618 const struct dentry_operations ntfs_dentry_ops = {
619 .d_hash = ntfs_d_hash,
620 .d_compare = ntfs_d_compare,