xref: /openbmc/linux/fs/ntfs3/namei.c (revision 0ad53fe3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5  *
6  */
7 
8 #include <linux/blkdev.h>
9 #include <linux/buffer_head.h>
10 #include <linux/fs.h>
11 #include <linux/iversion.h>
12 #include <linux/namei.h>
13 #include <linux/nls.h>
14 
15 #include "debug.h"
16 #include "ntfs.h"
17 #include "ntfs_fs.h"
18 
19 /*
20  * fill_name_de - Format NTFS_DE in @buf.
21  */
22 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
23 		 const struct cpu_str *uni)
24 {
25 	int err;
26 	struct NTFS_DE *e = buf;
27 	u16 data_size;
28 	struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
29 
30 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
31 	e->ref.high = fname->home.high = 0;
32 #endif
33 	if (uni) {
34 #ifdef __BIG_ENDIAN
35 		int ulen = uni->len;
36 		__le16 *uname = fname->name;
37 		const u16 *name_cpu = uni->name;
38 
39 		while (ulen--)
40 			*uname++ = cpu_to_le16(*name_cpu++);
41 #else
42 		memcpy(fname->name, uni->name, uni->len * sizeof(u16));
43 #endif
44 		fname->name_len = uni->len;
45 
46 	} else {
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);
51 		if (err < 0)
52 			return err;
53 	}
54 
55 	fname->type = FILE_NAME_POSIX;
56 	data_size = fname_full_size(fname);
57 
58 	e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
59 	e->key_size = cpu_to_le16(data_size);
60 	e->flags = 0;
61 	e->res = 0;
62 
63 	return 0;
64 }
65 
66 /*
67  * ntfs_lookup - inode_operations::lookup
68  */
69 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
70 				  u32 flags)
71 {
72 	struct ntfs_inode *ni = ntfs_i(dir);
73 	struct cpu_str *uni = __getname();
74 	struct inode *inode;
75 	int err;
76 
77 	if (!uni)
78 		inode = ERR_PTR(-ENOMEM);
79 	else {
80 		err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
81 					dentry->d_name.len, uni, NTFS_NAME_LEN,
82 					UTF16_HOST_ENDIAN);
83 		if (err < 0)
84 			inode = ERR_PTR(err);
85 		else {
86 			ni_lock(ni);
87 			inode = dir_search_u(dir, uni, NULL);
88 			ni_unlock(ni);
89 		}
90 		__putname(uni);
91 	}
92 
93 	return d_splice_alias(inode, dentry);
94 }
95 
96 /*
97  * ntfs_create - inode_operations::create
98  */
99 static int ntfs_create(struct user_namespace *mnt_userns, struct inode *dir,
100 		       struct dentry *dentry, umode_t mode, bool excl)
101 {
102 	struct ntfs_inode *ni = ntfs_i(dir);
103 	struct inode *inode;
104 
105 	ni_lock_dir(ni);
106 
107 	inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFREG | mode,
108 				  0, NULL, 0, NULL);
109 
110 	ni_unlock(ni);
111 
112 	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
113 }
114 
115 /*
116  * ntfs_mknod
117  *
118  * inode_operations::mknod
119  */
120 static int ntfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
121 		      struct dentry *dentry, umode_t mode, dev_t rdev)
122 {
123 	struct ntfs_inode *ni = ntfs_i(dir);
124 	struct inode *inode;
125 
126 	ni_lock_dir(ni);
127 
128 	inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, mode, rdev,
129 				  NULL, 0, NULL);
130 
131 	ni_unlock(ni);
132 
133 	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
134 }
135 
136 /*
137  * ntfs_link - inode_operations::link
138  */
139 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
140 {
141 	int err;
142 	struct inode *inode = d_inode(ode);
143 	struct ntfs_inode *ni = ntfs_i(inode);
144 
145 	if (S_ISDIR(inode->i_mode))
146 		return -EPERM;
147 
148 	if (inode->i_nlink >= NTFS_LINK_MAX)
149 		return -EMLINK;
150 
151 	ni_lock_dir(ntfs_i(dir));
152 	if (inode != dir)
153 		ni_lock(ni);
154 
155 	inc_nlink(inode);
156 	ihold(inode);
157 
158 	err = ntfs_link_inode(inode, de);
159 
160 	if (!err) {
161 		dir->i_ctime = dir->i_mtime = inode->i_ctime =
162 			current_time(dir);
163 		mark_inode_dirty(inode);
164 		mark_inode_dirty(dir);
165 		d_instantiate(de, inode);
166 	} else {
167 		drop_nlink(inode);
168 		iput(inode);
169 	}
170 
171 	if (inode != dir)
172 		ni_unlock(ni);
173 	ni_unlock(ntfs_i(dir));
174 
175 	return err;
176 }
177 
178 /*
179  * ntfs_unlink - inode_operations::unlink
180  */
181 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
182 {
183 	struct ntfs_inode *ni = ntfs_i(dir);
184 	int err;
185 
186 	ni_lock_dir(ni);
187 
188 	err = ntfs_unlink_inode(dir, dentry);
189 
190 	ni_unlock(ni);
191 
192 	return err;
193 }
194 
195 /*
196  * ntfs_symlink - inode_operations::symlink
197  */
198 static int ntfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
199 			struct dentry *dentry, const char *symname)
200 {
201 	u32 size = strlen(symname);
202 	struct inode *inode;
203 	struct ntfs_inode *ni = ntfs_i(dir);
204 
205 	ni_lock_dir(ni);
206 
207 	inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFLNK | 0777,
208 				  0, symname, size, NULL);
209 
210 	ni_unlock(ni);
211 
212 	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
213 }
214 
215 /*
216  * ntfs_mkdir- inode_operations::mkdir
217  */
218 static int ntfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
219 		      struct dentry *dentry, umode_t mode)
220 {
221 	struct inode *inode;
222 	struct ntfs_inode *ni = ntfs_i(dir);
223 
224 	ni_lock_dir(ni);
225 
226 	inode = ntfs_create_inode(mnt_userns, dir, dentry, NULL, S_IFDIR | mode,
227 				  0, NULL, 0, NULL);
228 
229 	ni_unlock(ni);
230 
231 	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
232 }
233 
234 /*
235  * ntfs_rmdir - inode_operations::rm_dir
236  */
237 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
238 {
239 	struct ntfs_inode *ni = ntfs_i(dir);
240 	int err;
241 
242 	ni_lock_dir(ni);
243 
244 	err = ntfs_unlink_inode(dir, dentry);
245 
246 	ni_unlock(ni);
247 
248 	return err;
249 }
250 
251 /*
252  * ntfs_rename - inode_operations::rename
253  */
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)
257 {
258 	int err;
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;
268 	/*
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)
273 	 */
274 	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
275 	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
276 		      1024);
277 	static_assert(PATH_MAX >= 4 * 1024);
278 
279 	if (flags & ~RENAME_NOREPLACE)
280 		return -EINVAL;
281 
282 	is_same = dentry->d_name.len == new_dentry->d_name.len &&
283 		  !memcmp(dentry->d_name.name, new_dentry->d_name.name,
284 			  dentry->d_name.len);
285 
286 	if (is_same && dir == new_dir) {
287 		/* Nothing to do. */
288 		return 0;
289 	}
290 
291 	if (ntfs_is_meta_file(sbi, inode->i_ino)) {
292 		/* Should we print an error? */
293 		return -EINVAL;
294 	}
295 
296 	if (new_inode) {
297 		/* Target name exists. Unlink it. */
298 		dget(new_dentry);
299 		ni_lock_dir(new_dir_ni);
300 		err = ntfs_unlink_inode(new_dir, new_dentry);
301 		ni_unlock(new_dir_ni);
302 		dput(new_dentry);
303 		if (err)
304 			return err;
305 	}
306 
307 	/* Allocate PATH_MAX bytes. */
308 	de = __getname();
309 	if (!de)
310 		return -ENOMEM;
311 
312 	/* Translate dentry->d_name into unicode form. */
313 	err = fill_name_de(sbi, de, &dentry->d_name, NULL);
314 	if (err < 0)
315 		goto out;
316 
317 	if (is_same) {
318 		/* Reuse 'de'. */
319 		new_de = de;
320 	} else {
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);
324 		if (err < 0)
325 			goto out;
326 	}
327 
328 	ni_lock_dir(dir_ni);
329 	ni_lock(ni);
330 
331 	is_bad = false;
332 	err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
333 	if (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);
338 	} else if (!err) {
339 		inode->i_ctime = dir->i_ctime = dir->i_mtime =
340 			current_time(dir);
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);
346 		}
347 
348 		if (IS_DIRSYNC(dir))
349 			ntfs_sync_inode(dir);
350 
351 		if (IS_DIRSYNC(new_dir))
352 			ntfs_sync_inode(inode);
353 	}
354 
355 	ni_unlock(ni);
356 	ni_unlock(dir_ni);
357 out:
358 	__putname(de);
359 	return err;
360 }
361 
362 struct dentry *ntfs3_get_parent(struct dentry *child)
363 {
364 	struct inode *inode = d_inode(child);
365 	struct ntfs_inode *ni = ntfs_i(inode);
366 
367 	struct ATTR_LIST_ENTRY *le = NULL;
368 	struct ATTRIB *attr = NULL;
369 	struct ATTR_FILE_NAME *fname;
370 
371 	while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
372 				    NULL))) {
373 		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
374 		if (!fname)
375 			continue;
376 
377 		return d_obtain_alias(
378 			ntfs_iget5(inode->i_sb, &fname->home, NULL));
379 	}
380 
381 	return ERR_PTR(-ENOENT);
382 }
383 
384 // clang-format off
385 const struct inode_operations ntfs_dir_inode_operations = {
386 	.lookup		= ntfs_lookup,
387 	.create		= ntfs_create,
388 	.link		= ntfs_link,
389 	.unlink		= ntfs_unlink,
390 	.symlink	= ntfs_symlink,
391 	.mkdir		= ntfs_mkdir,
392 	.rmdir		= ntfs_rmdir,
393 	.mknod		= ntfs_mknod,
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,
402 };
403 
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,
410 };
411 // clang-format on
412