1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
8 #include <linux/blkdev.h>
9 #include <linux/buffer_head.h>
11 #include <linux/iversion.h>
12 #include <linux/namei.h>
13 #include <linux/nls.h>
20 * fill_name_de - Format NTFS_DE in @buf.
22 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
23 const struct cpu_str *uni)
26 struct NTFS_DE *e = buf;
28 struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
30 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
31 e->ref.high = fname->home.high = 0;
36 __le16 *uname = fname->name;
37 const u16 *name_cpu = uni->name;
40 *uname++ = cpu_to_le16(*name_cpu++);
42 memcpy(fname->name, uni->name, uni->len * sizeof(u16));
44 fname->name_len = uni->len;
47 /* Convert input string to unicode. */
48 err = ntfs_nls_to_utf16(sbi, name->name, name->len,
49 (struct cpu_str *)&fname->name_len,
50 NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN);
55 fname->type = FILE_NAME_POSIX;
56 data_size = fname_full_size(fname);
58 e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
59 e->key_size = cpu_to_le16(data_size);
67 * ntfs_lookup - inode_operations::lookup
69 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
72 struct ntfs_inode *ni = ntfs_i(dir);
73 struct cpu_str *uni = __getname();
78 inode = ERR_PTR(-ENOMEM);
80 err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
81 dentry->d_name.len, uni, NTFS_NAME_LEN,
87 inode = dir_search_u(dir, uni, NULL);
93 return d_splice_alias(inode, dentry);
97 * ntfs_create - inode_operations::create
99 static int ntfs_create(struct user_namespace *mnt_userns, struct inode *dir,
100 struct dentry *dentry, umode_t mode, bool excl)
102 struct ntfs_inode *ni = ntfs_i(dir);
107 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFREG | mode,
112 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
118 * inode_operations::mknod
120 static int ntfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
121 struct dentry *dentry, umode_t mode, dev_t rdev)
123 struct ntfs_inode *ni = ntfs_i(dir);
128 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, mode, rdev,
133 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
137 * ntfs_link - inode_operations::link
139 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
142 struct inode *inode = d_inode(ode);
143 struct ntfs_inode *ni = ntfs_i(inode);
145 if (S_ISDIR(inode->i_mode))
148 if (inode->i_nlink >= NTFS_LINK_MAX)
151 ni_lock_dir(ntfs_i(dir));
158 err = ntfs_link_inode(inode, de);
161 dir->i_ctime = dir->i_mtime = inode->i_ctime =
163 mark_inode_dirty(inode);
164 mark_inode_dirty(dir);
165 d_instantiate(de, inode);
173 ni_unlock(ntfs_i(dir));
179 * ntfs_unlink - inode_operations::unlink
181 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
183 struct ntfs_inode *ni = ntfs_i(dir);
188 err = ntfs_unlink_inode(dir, dentry);
196 * ntfs_symlink - inode_operations::symlink
198 static int ntfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
199 struct dentry *dentry, const char *symname)
201 u32 size = strlen(symname);
203 struct ntfs_inode *ni = ntfs_i(dir);
207 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFLNK | 0777,
208 0, symname, size, NULL);
212 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
216 * ntfs_mkdir- inode_operations::mkdir
218 static int ntfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
219 struct dentry *dentry, umode_t mode)
222 struct ntfs_inode *ni = ntfs_i(dir);
226 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFDIR | mode,
231 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
235 * ntfs_rmdir - inode_operations::rm_dir
237 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
239 struct ntfs_inode *ni = ntfs_i(dir);
244 err = ntfs_unlink_inode(dir, dentry);
252 * ntfs_rename - inode_operations::rename
254 static int ntfs_rename(struct user_namespace *mnt_userns, struct inode *dir,
255 struct dentry *dentry, struct inode *new_dir,
256 struct dentry *new_dentry, u32 flags)
259 struct super_block *sb = dir->i_sb;
260 struct ntfs_sb_info *sbi = sb->s_fs_info;
261 struct ntfs_inode *dir_ni = ntfs_i(dir);
262 struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
263 struct inode *inode = d_inode(dentry);
264 struct ntfs_inode *ni = ntfs_i(inode);
265 struct inode *new_inode = d_inode(new_dentry);
266 struct NTFS_DE *de, *new_de;
267 bool is_same, is_bad;
269 * de - memory of PATH_MAX bytes:
270 * [0-1024) - original name (dentry->d_name)
271 * [1024-2048) - paired to original name, usually DOS variant of dentry->d_name
272 * [2048-3072) - new name (new_dentry->d_name)
274 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
275 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
277 static_assert(PATH_MAX >= 4 * 1024);
279 if (flags & ~RENAME_NOREPLACE)
282 is_same = dentry->d_name.len == new_dentry->d_name.len &&
283 !memcmp(dentry->d_name.name, new_dentry->d_name.name,
286 if (is_same && dir == new_dir) {
291 if (ntfs_is_meta_file(sbi, inode->i_ino)) {
292 /* Should we print an error? */
297 /* Target name exists. Unlink it. */
299 ni_lock_dir(new_dir_ni);
300 err = ntfs_unlink_inode(new_dir, new_dentry);
301 ni_unlock(new_dir_ni);
307 /* Allocate PATH_MAX bytes. */
312 /* Translate dentry->d_name into unicode form. */
313 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
321 /* Translate new_dentry->d_name into unicode form. */
322 new_de = Add2Ptr(de, 2048);
323 err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
332 err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
334 /* Restore after failed rename failed too. */
335 make_bad_inode(inode);
336 ntfs_inode_err(inode, "failed to undo rename");
337 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
339 inode->i_ctime = dir->i_ctime = dir->i_mtime =
341 mark_inode_dirty(inode);
342 mark_inode_dirty(dir);
343 if (dir != new_dir) {
344 new_dir->i_mtime = new_dir->i_ctime = dir->i_ctime;
345 mark_inode_dirty(new_dir);
349 ntfs_sync_inode(dir);
351 if (IS_DIRSYNC(new_dir))
352 ntfs_sync_inode(inode);
362 struct dentry *ntfs3_get_parent(struct dentry *child)
364 struct inode *inode = d_inode(child);
365 struct ntfs_inode *ni = ntfs_i(inode);
367 struct ATTR_LIST_ENTRY *le = NULL;
368 struct ATTRIB *attr = NULL;
369 struct ATTR_FILE_NAME *fname;
371 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
373 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
377 return d_obtain_alias(
378 ntfs_iget5(inode->i_sb, &fname->home, NULL));
381 return ERR_PTR(-ENOENT);
385 const struct inode_operations ntfs_dir_inode_operations = {
386 .lookup = ntfs_lookup,
387 .create = ntfs_create,
389 .unlink = ntfs_unlink,
390 .symlink = ntfs_symlink,
394 .rename = ntfs_rename,
395 .permission = ntfs_permission,
396 .get_acl = ntfs_get_acl,
397 .set_acl = ntfs_set_acl,
398 .setattr = ntfs3_setattr,
399 .getattr = ntfs_getattr,
400 .listxattr = ntfs_listxattr,
401 .fiemap = ntfs_fiemap,
404 const struct inode_operations ntfs_special_inode_operations = {
405 .setattr = ntfs3_setattr,
406 .getattr = ntfs_getattr,
407 .listxattr = ntfs_listxattr,
408 .get_acl = ntfs_get_acl,
409 .set_acl = ntfs_set_acl,