1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
10 #include <linux/ctype.h>
17 * fill_name_de - Format NTFS_DE in @buf.
19 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
20 const struct cpu_str *uni)
23 struct NTFS_DE *e = buf;
25 struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
27 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
28 e->ref.high = fname->home.high = 0;
33 __le16 *uname = fname->name;
34 const u16 *name_cpu = uni->name;
37 *uname++ = cpu_to_le16(*name_cpu++);
39 memcpy(fname->name, uni->name, uni->len * sizeof(u16));
41 fname->name_len = uni->len;
44 /* Convert input string to unicode. */
45 err = ntfs_nls_to_utf16(sbi, name->name, name->len,
46 (struct cpu_str *)&fname->name_len,
47 NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN);
52 fname->type = FILE_NAME_POSIX;
53 data_size = fname_full_size(fname);
55 e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
56 e->key_size = cpu_to_le16(data_size);
64 * ntfs_lookup - inode_operations::lookup
66 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
69 struct ntfs_inode *ni = ntfs_i(dir);
70 struct cpu_str *uni = __getname();
75 inode = ERR_PTR(-ENOMEM);
77 err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
78 dentry->d_name.len, uni, NTFS_NAME_LEN,
84 inode = dir_search_u(dir, uni, NULL);
90 return d_splice_alias(inode, dentry);
94 * ntfs_create - inode_operations::create
96 static int ntfs_create(struct user_namespace *mnt_userns, struct inode *dir,
97 struct dentry *dentry, umode_t mode, bool excl)
101 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFREG | mode,
104 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
110 * inode_operations::mknod
112 static int ntfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
113 struct dentry *dentry, umode_t mode, dev_t rdev)
117 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, mode, rdev,
120 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
124 * ntfs_link - inode_operations::link
126 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
129 struct inode *inode = d_inode(ode);
130 struct ntfs_inode *ni = ntfs_i(inode);
132 if (S_ISDIR(inode->i_mode))
135 if (inode->i_nlink >= NTFS_LINK_MAX)
138 ni_lock_dir(ntfs_i(dir));
145 err = ntfs_link_inode(inode, de);
148 dir->i_ctime = dir->i_mtime = inode->i_ctime =
150 mark_inode_dirty(inode);
151 mark_inode_dirty(dir);
152 d_instantiate(de, inode);
160 ni_unlock(ntfs_i(dir));
166 * ntfs_unlink - inode_operations::unlink
168 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
170 struct ntfs_inode *ni = ntfs_i(dir);
175 err = ntfs_unlink_inode(dir, dentry);
183 * ntfs_symlink - inode_operations::symlink
185 static int ntfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
186 struct dentry *dentry, const char *symname)
188 u32 size = strlen(symname);
191 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFLNK | 0777,
192 0, symname, size, NULL);
194 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
198 * ntfs_mkdir- inode_operations::mkdir
200 static int ntfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
201 struct dentry *dentry, umode_t mode)
205 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFDIR | mode,
208 return IS_ERR(inode) ? PTR_ERR(inode) : 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);
221 err = ntfs_unlink_inode(dir, dentry);
229 * ntfs_rename - inode_operations::rename
231 static int ntfs_rename(struct user_namespace *mnt_userns, struct inode *dir,
232 struct dentry *dentry, struct inode *new_dir,
233 struct dentry *new_dentry, u32 flags)
236 struct super_block *sb = dir->i_sb;
237 struct ntfs_sb_info *sbi = sb->s_fs_info;
238 struct ntfs_inode *dir_ni = ntfs_i(dir);
239 struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
240 struct inode *inode = d_inode(dentry);
241 struct ntfs_inode *ni = ntfs_i(inode);
242 struct inode *new_inode = d_inode(new_dentry);
243 struct NTFS_DE *de, *new_de;
244 bool is_same, is_bad;
246 * de - memory of PATH_MAX bytes:
247 * [0-1024) - original name (dentry->d_name)
248 * [1024-2048) - paired to original name, usually DOS variant of dentry->d_name
249 * [2048-3072) - new name (new_dentry->d_name)
251 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
252 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
254 static_assert(PATH_MAX >= 4 * 1024);
256 if (flags & ~RENAME_NOREPLACE)
259 is_same = dentry->d_name.len == new_dentry->d_name.len &&
260 !memcmp(dentry->d_name.name, new_dentry->d_name.name,
263 if (is_same && dir == new_dir) {
268 if (ntfs_is_meta_file(sbi, inode->i_ino)) {
269 /* Should we print an error? */
274 /* Target name exists. Unlink it. */
276 ni_lock_dir(new_dir_ni);
277 err = ntfs_unlink_inode(new_dir, new_dentry);
278 ni_unlock(new_dir_ni);
284 /* Allocate PATH_MAX bytes. */
289 /* Translate dentry->d_name into unicode form. */
290 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
298 /* Translate new_dentry->d_name into unicode form. */
299 new_de = Add2Ptr(de, 2048);
300 err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
309 err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
311 /* Restore after failed rename failed too. */
312 _ntfs_bad_inode(inode);
314 inode->i_ctime = dir->i_ctime = dir->i_mtime =
316 mark_inode_dirty(inode);
317 mark_inode_dirty(dir);
318 if (dir != new_dir) {
319 new_dir->i_mtime = new_dir->i_ctime = dir->i_ctime;
320 mark_inode_dirty(new_dir);
324 ntfs_sync_inode(dir);
326 if (IS_DIRSYNC(new_dir))
327 ntfs_sync_inode(inode);
337 struct dentry *ntfs3_get_parent(struct dentry *child)
339 struct inode *inode = d_inode(child);
340 struct ntfs_inode *ni = ntfs_i(inode);
342 struct ATTR_LIST_ENTRY *le = NULL;
343 struct ATTRIB *attr = NULL;
344 struct ATTR_FILE_NAME *fname;
346 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
348 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
352 return d_obtain_alias(
353 ntfs_iget5(inode->i_sb, &fname->home, NULL));
356 return ERR_PTR(-ENOENT);
360 * dentry_operations::d_hash
362 static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
364 struct ntfs_sb_info *sbi;
365 const char *n = name->name;
366 unsigned int len = name->len;
372 /* First try fast implementation. */
373 hash = init_name_hash(dentry);
377 name->hash = end_name_hash(hash);
385 hash = partial_name_hash(toupper(c), hash);
389 * Try slow way with current upcase table
395 sbi = dentry->d_sb->s_fs_info;
397 err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
407 hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
408 init_name_hash(dentry));
409 name->hash = end_name_hash(hash);
418 * dentry_operations::d_compare
420 static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
421 const char *str, const struct qstr *name)
423 struct ntfs_sb_info *sbi;
425 const char *n1 = str;
426 const char *n2 = name->name;
427 unsigned int len2 = name->len;
428 unsigned int lm = min(len1, len2);
429 unsigned char c1, c2;
430 struct cpu_str *uni1;
433 /* First try fast implementation. */
438 if ((c1 = *n1++) == (c2 = *n2++))
441 if (c1 >= 0x80 || c2 >= 0x80)
444 if (toupper(c1) != toupper(c2))
449 * Try slow way with current upcase table
451 sbi = dentry->d_sb->s_fs_info;
456 ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
466 uni2 = Add2Ptr(uni1, 2048);
468 ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
469 (struct cpu_str *)uni2, NTFS_NAME_LEN,
470 UTF16_LITTLE_ENDIAN);
479 ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
487 const struct inode_operations ntfs_dir_inode_operations = {
488 .lookup = ntfs_lookup,
489 .create = ntfs_create,
491 .unlink = ntfs_unlink,
492 .symlink = ntfs_symlink,
496 .rename = ntfs_rename,
497 .permission = ntfs_permission,
498 .get_acl = ntfs_get_acl,
499 .set_acl = ntfs_set_acl,
500 .setattr = ntfs3_setattr,
501 .getattr = ntfs_getattr,
502 .listxattr = ntfs_listxattr,
503 .fiemap = ntfs_fiemap,
506 const struct inode_operations ntfs_special_inode_operations = {
507 .setattr = ntfs3_setattr,
508 .getattr = ntfs_getattr,
509 .listxattr = ntfs_listxattr,
510 .get_acl = ntfs_get_acl,
511 .set_acl = ntfs_set_acl,
514 const struct dentry_operations ntfs_dentry_ops = {
515 .d_hash = ntfs_d_hash,
516 .d_compare = ntfs_d_compare,