xref: /linux/fs/ntfs3/namei.c (revision ab52c59103002b49f2455371e4b9c56ba3ef1781)
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/fs.h>
9 #include <linux/nls.h>
10 #include <linux/ctype.h>
11 #include <linux/posix_acl.h>
12 
13 #include "debug.h"
14 #include "ntfs.h"
15 #include "ntfs_fs.h"
16 
17 /*
18  * fill_name_de - Format NTFS_DE in @buf.
19  */
20 int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
21 		 const struct cpu_str *uni)
22 {
23 	int err;
24 	struct NTFS_DE *e = buf;
25 	u16 data_size;
26 	struct ATTR_FILE_NAME *fname = (struct ATTR_FILE_NAME *)(e + 1);
27 
28 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
29 	e->ref.high = fname->home.high = 0;
30 #endif
31 	if (uni) {
32 #ifdef __BIG_ENDIAN
33 		int ulen = uni->len;
34 		__le16 *uname = fname->name;
35 		const u16 *name_cpu = uni->name;
36 
37 		while (ulen--)
38 			*uname++ = cpu_to_le16(*name_cpu++);
39 #else
40 		memcpy(fname->name, uni->name, uni->len * sizeof(u16));
41 #endif
42 		fname->name_len = uni->len;
43 
44 	} else {
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);
49 		if (err < 0)
50 			return err;
51 	}
52 
53 	fname->type = FILE_NAME_POSIX;
54 	data_size = fname_full_size(fname);
55 
56 	e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
57 	e->key_size = cpu_to_le16(data_size);
58 	e->flags = 0;
59 	e->res = 0;
60 
61 	return 0;
62 }
63 
64 /*
65  * ntfs_lookup - inode_operations::lookup
66  */
67 static struct dentry *ntfs_lookup(struct inode *dir, struct dentry *dentry,
68 				  u32 flags)
69 {
70 	struct ntfs_inode *ni = ntfs_i(dir);
71 	struct cpu_str *uni = __getname();
72 	struct inode *inode;
73 	int err;
74 
75 	if (!uni)
76 		inode = ERR_PTR(-ENOMEM);
77 	else {
78 		err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
79 					dentry->d_name.len, uni, NTFS_NAME_LEN,
80 					UTF16_HOST_ENDIAN);
81 		if (err < 0)
82 			inode = ERR_PTR(err);
83 		else {
84 			ni_lock(ni);
85 			inode = dir_search_u(dir, uni, NULL);
86 			ni_unlock(ni);
87 		}
88 		__putname(uni);
89 	}
90 
91 	/*
92 	 * Check for a null pointer
93 	 * If the MFT record of ntfs inode is not a base record, inode->i_op can be NULL.
94 	 * This causes null pointer dereference in d_splice_alias().
95 	 */
96 	if (!IS_ERR_OR_NULL(inode) && !inode->i_op) {
97 		iput(inode);
98 		inode = ERR_PTR(-EINVAL);
99 	}
100 
101 	return d_splice_alias(inode, dentry);
102 }
103 
104 /*
105  * ntfs_create - inode_operations::create
106  */
107 static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
108 		       struct dentry *dentry, umode_t mode, bool excl)
109 {
110 	return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0,
111 				 NULL, 0, NULL);
112 }
113 
114 /*
115  * ntfs_mknod
116  *
117  * inode_operations::mknod
118  */
119 static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
120 		      struct dentry *dentry, umode_t mode, dev_t rdev)
121 {
122 	return ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0,
123 				 NULL);
124 }
125 
126 /*
127  * ntfs_link - inode_operations::link
128  */
129 static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
130 {
131 	int err;
132 	struct inode *inode = d_inode(ode);
133 	struct ntfs_inode *ni = ntfs_i(inode);
134 
135 	if (S_ISDIR(inode->i_mode))
136 		return -EPERM;
137 
138 	if (inode->i_nlink >= NTFS_LINK_MAX)
139 		return -EMLINK;
140 
141 	ni_lock_dir(ntfs_i(dir));
142 	if (inode != dir)
143 		ni_lock(ni);
144 
145 	inc_nlink(inode);
146 	ihold(inode);
147 
148 	err = ntfs_link_inode(inode, de);
149 
150 	if (!err) {
151 		inode_set_ctime_current(inode);
152 		inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
153 		mark_inode_dirty(inode);
154 		mark_inode_dirty(dir);
155 		d_instantiate(de, inode);
156 	} else {
157 		drop_nlink(inode);
158 		iput(inode);
159 	}
160 
161 	if (inode != dir)
162 		ni_unlock(ni);
163 	ni_unlock(ntfs_i(dir));
164 
165 	return err;
166 }
167 
168 /*
169  * ntfs_unlink - inode_operations::unlink
170  */
171 static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
172 {
173 	struct ntfs_inode *ni = ntfs_i(dir);
174 	int err;
175 
176 	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
177 		return -EIO;
178 
179 	ni_lock_dir(ni);
180 
181 	err = ntfs_unlink_inode(dir, dentry);
182 
183 	ni_unlock(ni);
184 
185 	return err;
186 }
187 
188 /*
189  * ntfs_symlink - inode_operations::symlink
190  */
191 static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
192 			struct dentry *dentry, const char *symname)
193 {
194 	u32 size = strlen(symname);
195 
196 	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
197 		return -EIO;
198 
199 	return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0,
200 				 symname, size, NULL);
201 }
202 
203 /*
204  * ntfs_mkdir- inode_operations::mkdir
205  */
206 static int ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
207 		      struct dentry *dentry, umode_t mode)
208 {
209 	return ntfs_create_inode(idmap, dir, dentry, NULL, S_IFDIR | mode, 0,
210 				 NULL, 0, NULL);
211 }
212 
213 /*
214  * ntfs_rmdir - inode_operations::rmdir
215  */
216 static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
217 {
218 	struct ntfs_inode *ni = ntfs_i(dir);
219 	int err;
220 
221 	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
222 		return -EIO;
223 
224 	ni_lock_dir(ni);
225 
226 	err = ntfs_unlink_inode(dir, dentry);
227 
228 	ni_unlock(ni);
229 
230 	return err;
231 }
232 
233 /*
234  * ntfs_rename - inode_operations::rename
235  */
236 static int ntfs_rename(struct mnt_idmap *idmap, struct inode *dir,
237 		       struct dentry *dentry, struct inode *new_dir,
238 		       struct dentry *new_dentry, u32 flags)
239 {
240 	int err;
241 	struct super_block *sb = dir->i_sb;
242 	struct ntfs_sb_info *sbi = sb->s_fs_info;
243 	struct ntfs_inode *dir_ni = ntfs_i(dir);
244 	struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
245 	struct inode *inode = d_inode(dentry);
246 	struct ntfs_inode *ni = ntfs_i(inode);
247 	struct inode *new_inode = d_inode(new_dentry);
248 	struct NTFS_DE *de, *new_de;
249 	bool is_same, is_bad;
250 	/*
251 	 * de		- memory of PATH_MAX bytes:
252 	 * [0-1024)	- original name (dentry->d_name)
253 	 * [1024-2048)	- paired to original name, usually DOS variant of dentry->d_name
254 	 * [2048-3072)	- new name (new_dentry->d_name)
255 	 */
256 	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
257 	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
258 		      1024);
259 	static_assert(PATH_MAX >= 4 * 1024);
260 
261 	if (unlikely(ntfs3_forced_shutdown(sb)))
262 		return -EIO;
263 
264 	if (flags & ~RENAME_NOREPLACE)
265 		return -EINVAL;
266 
267 	is_same = dentry->d_name.len == new_dentry->d_name.len &&
268 		  !memcmp(dentry->d_name.name, new_dentry->d_name.name,
269 			  dentry->d_name.len);
270 
271 	if (is_same && dir == new_dir) {
272 		/* Nothing to do. */
273 		return 0;
274 	}
275 
276 	if (ntfs_is_meta_file(sbi, inode->i_ino)) {
277 		/* Should we print an error? */
278 		return -EINVAL;
279 	}
280 
281 	if (new_inode) {
282 		/* Target name exists. Unlink it. */
283 		dget(new_dentry);
284 		ni_lock_dir(new_dir_ni);
285 		err = ntfs_unlink_inode(new_dir, new_dentry);
286 		ni_unlock(new_dir_ni);
287 		dput(new_dentry);
288 		if (err)
289 			return err;
290 	}
291 
292 	/* Allocate PATH_MAX bytes. */
293 	de = __getname();
294 	if (!de)
295 		return -ENOMEM;
296 
297 	/* Translate dentry->d_name into unicode form. */
298 	err = fill_name_de(sbi, de, &dentry->d_name, NULL);
299 	if (err < 0)
300 		goto out;
301 
302 	if (is_same) {
303 		/* Reuse 'de'. */
304 		new_de = de;
305 	} else {
306 		/* Translate new_dentry->d_name into unicode form. */
307 		new_de = Add2Ptr(de, 2048);
308 		err = fill_name_de(sbi, new_de, &new_dentry->d_name, NULL);
309 		if (err < 0)
310 			goto out;
311 	}
312 
313 	ni_lock_dir(dir_ni);
314 	ni_lock(ni);
315 	if (dir_ni != new_dir_ni)
316 		ni_lock_dir2(new_dir_ni);
317 
318 	is_bad = false;
319 	err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
320 	if (is_bad) {
321 		/* Restore after failed rename failed too. */
322 		_ntfs_bad_inode(inode);
323 	} else if (!err) {
324 		simple_rename_timestamp(dir, dentry, new_dir, new_dentry);
325 		mark_inode_dirty(inode);
326 		mark_inode_dirty(dir);
327 		if (dir != new_dir)
328 			mark_inode_dirty(new_dir);
329 
330 		if (IS_DIRSYNC(dir))
331 			ntfs_sync_inode(dir);
332 
333 		if (IS_DIRSYNC(new_dir))
334 			ntfs_sync_inode(inode);
335 	}
336 
337 	if (dir_ni != new_dir_ni)
338 		ni_unlock(new_dir_ni);
339 	ni_unlock(ni);
340 	ni_unlock(dir_ni);
341 out:
342 	__putname(de);
343 	return err;
344 }
345 
346 struct dentry *ntfs3_get_parent(struct dentry *child)
347 {
348 	struct inode *inode = d_inode(child);
349 	struct ntfs_inode *ni = ntfs_i(inode);
350 
351 	struct ATTR_LIST_ENTRY *le = NULL;
352 	struct ATTRIB *attr = NULL;
353 	struct ATTR_FILE_NAME *fname;
354 
355 	while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
356 				    NULL))) {
357 		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
358 		if (!fname)
359 			continue;
360 
361 		return d_obtain_alias(
362 			ntfs_iget5(inode->i_sb, &fname->home, NULL));
363 	}
364 
365 	return ERR_PTR(-ENOENT);
366 }
367 
368 /*
369  * dentry_operations::d_hash
370  */
371 static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
372 {
373 	struct ntfs_sb_info *sbi;
374 	const char *n = name->name;
375 	unsigned int len = name->len;
376 	unsigned long hash;
377 	struct cpu_str *uni;
378 	unsigned int c;
379 	int err;
380 
381 	/* First try fast implementation. */
382 	hash = init_name_hash(dentry);
383 
384 	for (;;) {
385 		if (!len--) {
386 			name->hash = end_name_hash(hash);
387 			return 0;
388 		}
389 
390 		c = *n++;
391 		if (c >= 0x80)
392 			break;
393 
394 		hash = partial_name_hash(toupper(c), hash);
395 	}
396 
397 	/*
398 	 * Try slow way with current upcase table
399 	 */
400 	uni = __getname();
401 	if (!uni)
402 		return -ENOMEM;
403 
404 	sbi = dentry->d_sb->s_fs_info;
405 
406 	err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
407 				UTF16_HOST_ENDIAN);
408 	if (err < 0)
409 		goto out;
410 
411 	if (!err) {
412 		err = -EINVAL;
413 		goto out;
414 	}
415 
416 	hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
417 			       init_name_hash(dentry));
418 	name->hash = end_name_hash(hash);
419 	err = 0;
420 
421 out:
422 	__putname(uni);
423 	return err;
424 }
425 
426 /*
427  * dentry_operations::d_compare
428  */
429 static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
430 			  const char *str, const struct qstr *name)
431 {
432 	struct ntfs_sb_info *sbi;
433 	int ret;
434 	const char *n1 = str;
435 	const char *n2 = name->name;
436 	unsigned int len2 = name->len;
437 	unsigned int lm = min(len1, len2);
438 	unsigned char c1, c2;
439 	struct cpu_str *uni1;
440 	struct le_str *uni2;
441 
442 	/* First try fast implementation. */
443 	for (;;) {
444 		if (!lm--)
445 			return len1 != len2;
446 
447 		if ((c1 = *n1++) == (c2 = *n2++))
448 			continue;
449 
450 		if (c1 >= 0x80 || c2 >= 0x80)
451 			break;
452 
453 		if (toupper(c1) != toupper(c2))
454 			return 1;
455 	}
456 
457 	/*
458 	 * Try slow way with current upcase table
459 	 */
460 	sbi = dentry->d_sb->s_fs_info;
461 	uni1 = __getname();
462 	if (!uni1)
463 		return -ENOMEM;
464 
465 	ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
466 				UTF16_HOST_ENDIAN);
467 	if (ret < 0)
468 		goto out;
469 
470 	if (!ret) {
471 		ret = -EINVAL;
472 		goto out;
473 	}
474 
475 	uni2 = Add2Ptr(uni1, 2048);
476 
477 	ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
478 				(struct cpu_str *)uni2, NTFS_NAME_LEN,
479 				UTF16_LITTLE_ENDIAN);
480 	if (ret < 0)
481 		goto out;
482 
483 	if (!ret) {
484 		ret = -EINVAL;
485 		goto out;
486 	}
487 
488 	ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
489 
490 out:
491 	__putname(uni1);
492 	return ret;
493 }
494 
495 // clang-format off
496 const struct inode_operations ntfs_dir_inode_operations = {
497 	.lookup		= ntfs_lookup,
498 	.create		= ntfs_create,
499 	.link		= ntfs_link,
500 	.unlink		= ntfs_unlink,
501 	.symlink	= ntfs_symlink,
502 	.mkdir		= ntfs_mkdir,
503 	.rmdir		= ntfs_rmdir,
504 	.mknod		= ntfs_mknod,
505 	.rename		= ntfs_rename,
506 	.get_acl	= ntfs_get_acl,
507 	.set_acl	= ntfs_set_acl,
508 	.setattr	= ntfs3_setattr,
509 	.getattr	= ntfs_getattr,
510 	.listxattr	= ntfs_listxattr,
511 	.fiemap		= ntfs_fiemap,
512 };
513 
514 const struct inode_operations ntfs_special_inode_operations = {
515 	.setattr	= ntfs3_setattr,
516 	.getattr	= ntfs_getattr,
517 	.listxattr	= ntfs_listxattr,
518 	.get_acl	= ntfs_get_acl,
519 	.set_acl	= ntfs_set_acl,
520 };
521 
522 const struct dentry_operations ntfs_dentry_ops = {
523 	.d_hash		= ntfs_d_hash,
524 	.d_compare	= ntfs_d_compare,
525 };
526 
527 // clang-format on
528