1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
16 * fill_name_de - Format NTFS_DE in @buf.
18 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
19 const struct cpu_str *uni)
22 struct NTFS_DE *e = buf;
24 struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
26 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
27 e->ref.high = fname->home.high = 0;
32 __le16 *uname = fname->name;
33 const u16 *name_cpu = uni->name;
36 *uname++ = cpu_to_le16(*name_cpu++);
38 memcpy(fname->name, uni->name, uni->len * sizeof(u16));
40 fname->name_len = uni->len;
43 /* Convert input string to unicode. */
44 err = ntfs_nls_to_utf16(sbi, name->name, name->len,
45 (struct cpu_str *)&fname->name_len,
46 NTFS_NAME_LEN, UTF16_LITTLE_ENDIAN);
51 fname->type = FILE_NAME_POSIX;
52 data_size = fname_full_size(fname);
54 e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
55 e->key_size = cpu_to_le16(data_size);
63 * ntfs_lookup - inode_operations::lookup
65 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
68 struct ntfs_inode *ni = ntfs_i(dir);
69 struct cpu_str *uni = __getname();
74 inode = ERR_PTR(-ENOMEM);
76 err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
77 dentry->d_name.len, uni, NTFS_NAME_LEN,
83 inode = dir_search_u(dir, uni, NULL);
89 return d_splice_alias(inode, dentry);
93 * ntfs_create - inode_operations::create
95 static int ntfs_create(struct user_namespace *mnt_userns, struct inode *dir,
96 struct dentry *dentry, umode_t mode, bool excl)
98 struct ntfs_inode *ni = ntfs_i(dir);
103 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFREG | mode,
108 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
114 * inode_operations::mknod
116 static int ntfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
117 struct dentry *dentry, umode_t mode, dev_t rdev)
119 struct ntfs_inode *ni = ntfs_i(dir);
124 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, mode, rdev,
129 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
133 * ntfs_link - inode_operations::link
135 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
138 struct inode *inode = d_inode(ode);
139 struct ntfs_inode *ni = ntfs_i(inode);
141 if (S_ISDIR(inode->i_mode))
144 if (inode->i_nlink >= NTFS_LINK_MAX)
147 ni_lock_dir(ntfs_i(dir));
154 err = ntfs_link_inode(inode, de);
157 dir->i_ctime = dir->i_mtime = inode->i_ctime =
159 mark_inode_dirty(inode);
160 mark_inode_dirty(dir);
161 d_instantiate(de, inode);
169 ni_unlock(ntfs_i(dir));
175 * ntfs_unlink - inode_operations::unlink
177 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
179 struct ntfs_inode *ni = ntfs_i(dir);
184 err = ntfs_unlink_inode(dir, dentry);
192 * ntfs_symlink - inode_operations::symlink
194 static int ntfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
195 struct dentry *dentry, const char *symname)
197 u32 size = strlen(symname);
199 struct ntfs_inode *ni = ntfs_i(dir);
203 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFLNK | 0777,
204 0, symname, size, NULL);
208 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
212 * ntfs_mkdir- inode_operations::mkdir
214 static int ntfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
215 struct dentry *dentry, umode_t mode)
218 struct ntfs_inode *ni = ntfs_i(dir);
222 inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFDIR | mode,
227 return IS_ERR(inode) ? PTR_ERR(inode) : 0;
231 * ntfs_rmdir - inode_operations::rm_dir
233 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
235 struct ntfs_inode *ni = ntfs_i(dir);
240 err = ntfs_unlink_inode(dir, dentry);
248 * ntfs_rename - inode_operations::rename
250 static int ntfs_rename(struct user_namespace *mnt_userns, struct inode *dir,
251 struct dentry *dentry, struct inode *new_dir,
252 struct dentry *new_dentry, u32 flags)
255 struct super_block *sb = dir->i_sb;
256 struct ntfs_sb_info *sbi = sb->s_fs_info;
257 struct ntfs_inode *dir_ni = ntfs_i(dir);
258 struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
259 struct inode *inode = d_inode(dentry);
260 struct ntfs_inode *ni = ntfs_i(inode);
261 struct inode *new_inode = d_inode(new_dentry);
262 struct NTFS_DE *de, *new_de;
263 bool is_same, is_bad;
265 * de - memory of PATH_MAX bytes:
266 * [0-1024) - original name (dentry->d_name)
267 * [1024-2048) - paired to original name, usually DOS variant of dentry->d_name
268 * [2048-3072) - new name (new_dentry->d_name)
270 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
271 static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
273 static_assert(PATH_MAX >= 4 * 1024);
275 if (flags & ~RENAME_NOREPLACE)
278 is_same = dentry->d_name.len == new_dentry->d_name.len &&
279 !memcmp(dentry->d_name.name, new_dentry->d_name.name,
282 if (is_same && dir == new_dir) {
287 if (ntfs_is_meta_file(sbi, inode->i_ino)) {
288 /* Should we print an error? */
293 /* Target name exists. Unlink it. */
295 ni_lock_dir(new_dir_ni);
296 err = ntfs_unlink_inode(new_dir, new_dentry);
297 ni_unlock(new_dir_ni);
303 /* Allocate PATH_MAX bytes. */
308 /* Translate dentry->d_name into unicode form. */
309 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
317 /* Translate new_dentry->d_name into unicode form. */
318 new_de = Add2Ptr(de, 2048);
319 err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
328 err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
330 /* Restore after failed rename failed too. */
331 make_bad_inode(inode);
332 ntfs_inode_err(inode, "failed to undo rename");
333 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
335 inode->i_ctime = dir->i_ctime = dir->i_mtime =
337 mark_inode_dirty(inode);
338 mark_inode_dirty(dir);
339 if (dir != new_dir) {
340 new_dir->i_mtime = new_dir->i_ctime = dir->i_ctime;
341 mark_inode_dirty(new_dir);
345 ntfs_sync_inode(dir);
347 if (IS_DIRSYNC(new_dir))
348 ntfs_sync_inode(inode);
358 struct dentry *ntfs3_get_parent(struct dentry *child)
360 struct inode *inode = d_inode(child);
361 struct ntfs_inode *ni = ntfs_i(inode);
363 struct ATTR_LIST_ENTRY *le = NULL;
364 struct ATTRIB *attr = NULL;
365 struct ATTR_FILE_NAME *fname;
367 while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
369 fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
373 return d_obtain_alias(
374 ntfs_iget5(inode->i_sb, &fname->home, NULL));
377 return ERR_PTR(-ENOENT);
381 const struct inode_operations ntfs_dir_inode_operations = {
382 .lookup = ntfs_lookup,
383 .create = ntfs_create,
385 .unlink = ntfs_unlink,
386 .symlink = ntfs_symlink,
390 .rename = ntfs_rename,
391 .permission = ntfs_permission,
392 .get_acl = ntfs_get_acl,
393 .set_acl = ntfs_set_acl,
394 .setattr = ntfs3_setattr,
395 .getattr = ntfs_getattr,
396 .listxattr = ntfs_listxattr,
397 .fiemap = ntfs_fiemap,
400 const struct inode_operations ntfs_special_inode_operations = {
401 .setattr = ntfs3_setattr,
402 .getattr = ntfs_getattr,
403 .listxattr = ntfs_listxattr,
404 .get_acl = ntfs_get_acl,
405 .set_acl = ntfs_set_acl,