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 */
20int 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 */
67static 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 */
107static int ntfs_create(struct mnt_idmap *idmap, struct inode *dir,
108		       struct dentry *dentry, umode_t mode, bool excl)
109{
110	struct inode *inode;
111
112	inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFREG | mode, 0,
113				  NULL, 0, NULL);
114
115	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
116}
117
118/*
119 * ntfs_mknod
120 *
121 * inode_operations::mknod
122 */
123static int ntfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
124		      struct dentry *dentry, umode_t mode, dev_t rdev)
125{
126	struct inode *inode;
127
128	inode = ntfs_create_inode(idmap, dir, dentry, NULL, mode, rdev, NULL, 0,
129				  NULL);
130
131	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
132}
133
134/*
135 * ntfs_link - inode_operations::link
136 */
137static int ntfs_link(struct dentry *ode, struct inode *dir, struct dentry *de)
138{
139	int err;
140	struct inode *inode = d_inode(ode);
141	struct ntfs_inode *ni = ntfs_i(inode);
142
143	if (S_ISDIR(inode->i_mode))
144		return -EPERM;
145
146	if (inode->i_nlink >= NTFS_LINK_MAX)
147		return -EMLINK;
148
149	ni_lock_dir(ntfs_i(dir));
150	if (inode != dir)
151		ni_lock(ni);
152
153	inc_nlink(inode);
154	ihold(inode);
155
156	err = ntfs_link_inode(inode, de);
157
158	if (!err) {
159		inode_set_ctime_current(inode);
160		inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
161		mark_inode_dirty(inode);
162		mark_inode_dirty(dir);
163		d_instantiate(de, inode);
164	} else {
165		drop_nlink(inode);
166		iput(inode);
167	}
168
169	if (inode != dir)
170		ni_unlock(ni);
171	ni_unlock(ntfs_i(dir));
172
173	return err;
174}
175
176/*
177 * ntfs_unlink - inode_operations::unlink
178 */
179static int ntfs_unlink(struct inode *dir, struct dentry *dentry)
180{
181	struct ntfs_inode *ni = ntfs_i(dir);
182	int err;
183
184	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
185		return -EIO;
186
187	ni_lock_dir(ni);
188
189	err = ntfs_unlink_inode(dir, dentry);
190
191	ni_unlock(ni);
192
193	return err;
194}
195
196/*
197 * ntfs_symlink - inode_operations::symlink
198 */
199static int ntfs_symlink(struct mnt_idmap *idmap, struct inode *dir,
200			struct dentry *dentry, const char *symname)
201{
202	u32 size = strlen(symname);
203	struct inode *inode;
204
205	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
206		return -EIO;
207
208	inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFLNK | 0777, 0,
209				  symname, size, NULL);
210
211	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
212}
213
214/*
215 * ntfs_mkdir- inode_operations::mkdir
216 */
217static int ntfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
218		      struct dentry *dentry, umode_t mode)
219{
220	struct inode *inode;
221
222	inode = ntfs_create_inode(idmap, dir, dentry, NULL, S_IFDIR | mode, 0,
223				  NULL, 0, NULL);
224
225	return IS_ERR(inode) ? PTR_ERR(inode) : 0;
226}
227
228/*
229 * ntfs_rmdir - inode_operations::rmdir
230 */
231static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)
232{
233	struct ntfs_inode *ni = ntfs_i(dir);
234	int err;
235
236	if (unlikely(ntfs3_forced_shutdown(dir->i_sb)))
237		return -EIO;
238
239	ni_lock_dir(ni);
240
241	err = ntfs_unlink_inode(dir, dentry);
242
243	ni_unlock(ni);
244
245	return err;
246}
247
248/*
249 * ntfs_rename - inode_operations::rename
250 */
251static int ntfs_rename(struct mnt_idmap *idmap, struct inode *dir,
252		       struct dentry *dentry, struct inode *new_dir,
253		       struct dentry *new_dentry, u32 flags)
254{
255	int err;
256	struct super_block *sb = dir->i_sb;
257	struct ntfs_sb_info *sbi = sb->s_fs_info;
258	struct ntfs_inode *dir_ni = ntfs_i(dir);
259	struct ntfs_inode *new_dir_ni = ntfs_i(new_dir);
260	struct inode *inode = d_inode(dentry);
261	struct ntfs_inode *ni = ntfs_i(inode);
262	struct inode *new_inode = d_inode(new_dentry);
263	struct NTFS_DE *de, *new_de;
264	bool is_same, is_bad;
265	/*
266	 * de		- memory of PATH_MAX bytes:
267	 * [0-1024)	- original name (dentry->d_name)
268	 * [1024-2048)	- paired to original name, usually DOS variant of dentry->d_name
269	 * [2048-3072)	- new name (new_dentry->d_name)
270	 */
271	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + SIZEOF_RESIDENT < 1024);
272	static_assert(SIZEOF_ATTRIBUTE_FILENAME_MAX + sizeof(struct NTFS_DE) <
273		      1024);
274	static_assert(PATH_MAX >= 4 * 1024);
275
276	if (unlikely(ntfs3_forced_shutdown(sb)))
277		return -EIO;
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	if (dir_ni != new_dir_ni)
331		ni_lock_dir2(new_dir_ni);
332
333	is_bad = false;
334	err = ni_rename(dir_ni, new_dir_ni, ni, de, new_de, &is_bad);
335	if (is_bad) {
336		/* Restore after failed rename failed too. */
337		_ntfs_bad_inode(inode);
338	} else if (!err) {
339		simple_rename_timestamp(dir, dentry, new_dir, new_dentry);
340		mark_inode_dirty(inode);
341		mark_inode_dirty(dir);
342		if (dir != new_dir)
343			mark_inode_dirty(new_dir);
344
345		if (IS_DIRSYNC(dir))
346			ntfs_sync_inode(dir);
347
348		if (IS_DIRSYNC(new_dir))
349			ntfs_sync_inode(inode);
350	}
351
352	if (dir_ni != new_dir_ni)
353		ni_unlock(new_dir_ni);
354	ni_unlock(ni);
355	ni_unlock(dir_ni);
356out:
357	__putname(de);
358	return err;
359}
360
361/*
362 * ntfs_atomic_open
363 *
364 * inode_operations::atomic_open
365 */
366static int ntfs_atomic_open(struct inode *dir, struct dentry *dentry,
367			    struct file *file, u32 flags, umode_t mode)
368{
369	int err;
370	struct inode *inode;
371	struct ntfs_fnd *fnd = NULL;
372	struct ntfs_inode *ni = ntfs_i(dir);
373	struct dentry *d = NULL;
374	struct cpu_str *uni = __getname();
375	bool locked = false;
376
377	if (!uni)
378		return -ENOMEM;
379
380	err = ntfs_nls_to_utf16(ni->mi.sbi, dentry->d_name.name,
381				dentry->d_name.len, uni, NTFS_NAME_LEN,
382				UTF16_HOST_ENDIAN);
383	if (err < 0)
384		goto out;
385
386#ifdef CONFIG_NTFS3_FS_POSIX_ACL
387	if (IS_POSIXACL(dir)) {
388		/*
389		 * Load in cache current acl to avoid ni_lock(dir):
390		 * ntfs_create_inode -> ntfs_init_acl -> posix_acl_create ->
391		 * ntfs_get_acl -> ntfs_get_acl_ex -> ni_lock
392		 */
393		struct posix_acl *p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
394
395		if (IS_ERR(p)) {
396			err = PTR_ERR(p);
397			goto out;
398		}
399		posix_acl_release(p);
400	}
401#endif
402
403	if (d_in_lookup(dentry)) {
404		ni_lock_dir(ni);
405		locked = true;
406		fnd = fnd_get();
407		if (!fnd) {
408			err = -ENOMEM;
409			goto out1;
410		}
411
412		d = d_splice_alias(dir_search_u(dir, uni, fnd), dentry);
413		if (IS_ERR(d)) {
414			err = PTR_ERR(d);
415			d = NULL;
416			goto out2;
417		}
418
419		if (d)
420			dentry = d;
421	}
422
423	if (!(flags & O_CREAT) || d_really_is_positive(dentry)) {
424		err = finish_no_open(file, d);
425		goto out2;
426	}
427
428	file->f_mode |= FMODE_CREATED;
429
430	/*
431	 * fnd contains tree's path to insert to.
432	 * If fnd is not NULL then dir is locked.
433	 */
434	inode = ntfs_create_inode(file_mnt_idmap(file), dir, dentry, uni,
435				  mode, 0, NULL, 0, fnd);
436	err = IS_ERR(inode) ? PTR_ERR(inode) :
437			      finish_open(file, dentry, ntfs_file_open);
438	dput(d);
439
440out2:
441	fnd_put(fnd);
442out1:
443	if (locked)
444		ni_unlock(ni);
445out:
446	__putname(uni);
447	return err;
448}
449
450struct dentry *ntfs3_get_parent(struct dentry *child)
451{
452	struct inode *inode = d_inode(child);
453	struct ntfs_inode *ni = ntfs_i(inode);
454
455	struct ATTR_LIST_ENTRY *le = NULL;
456	struct ATTRIB *attr = NULL;
457	struct ATTR_FILE_NAME *fname;
458
459	while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL,
460				    NULL))) {
461		fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME);
462		if (!fname)
463			continue;
464
465		return d_obtain_alias(
466			ntfs_iget5(inode->i_sb, &fname->home, NULL));
467	}
468
469	return ERR_PTR(-ENOENT);
470}
471
472/*
473 * dentry_operations::d_hash
474 */
475static int ntfs_d_hash(const struct dentry *dentry, struct qstr *name)
476{
477	struct ntfs_sb_info *sbi;
478	const char *n = name->name;
479	unsigned int len = name->len;
480	unsigned long hash;
481	struct cpu_str *uni;
482	unsigned int c;
483	int err;
484
485	/* First try fast implementation. */
486	hash = init_name_hash(dentry);
487
488	for (;;) {
489		if (!len--) {
490			name->hash = end_name_hash(hash);
491			return 0;
492		}
493
494		c = *n++;
495		if (c >= 0x80)
496			break;
497
498		hash = partial_name_hash(toupper(c), hash);
499	}
500
501	/*
502	 * Try slow way with current upcase table
503	 */
504	uni = __getname();
505	if (!uni)
506		return -ENOMEM;
507
508	sbi = dentry->d_sb->s_fs_info;
509
510	err = ntfs_nls_to_utf16(sbi, name->name, name->len, uni, NTFS_NAME_LEN,
511				UTF16_HOST_ENDIAN);
512	if (err < 0)
513		goto out;
514
515	if (!err) {
516		err = -EINVAL;
517		goto out;
518	}
519
520	hash = ntfs_names_hash(uni->name, uni->len, sbi->upcase,
521			       init_name_hash(dentry));
522	name->hash = end_name_hash(hash);
523	err = 0;
524
525out:
526	__putname(uni);
527	return err;
528}
529
530/*
531 * dentry_operations::d_compare
532 */
533static int ntfs_d_compare(const struct dentry *dentry, unsigned int len1,
534			  const char *str, const struct qstr *name)
535{
536	struct ntfs_sb_info *sbi;
537	int ret;
538	const char *n1 = str;
539	const char *n2 = name->name;
540	unsigned int len2 = name->len;
541	unsigned int lm = min(len1, len2);
542	unsigned char c1, c2;
543	struct cpu_str *uni1;
544	struct le_str *uni2;
545
546	/* First try fast implementation. */
547	for (;;) {
548		if (!lm--)
549			return len1 != len2;
550
551		if ((c1 = *n1++) == (c2 = *n2++))
552			continue;
553
554		if (c1 >= 0x80 || c2 >= 0x80)
555			break;
556
557		if (toupper(c1) != toupper(c2))
558			return 1;
559	}
560
561	/*
562	 * Try slow way with current upcase table
563	 */
564	sbi = dentry->d_sb->s_fs_info;
565	uni1 = __getname();
566	if (!uni1)
567		return -ENOMEM;
568
569	ret = ntfs_nls_to_utf16(sbi, str, len1, uni1, NTFS_NAME_LEN,
570				UTF16_HOST_ENDIAN);
571	if (ret < 0)
572		goto out;
573
574	if (!ret) {
575		ret = -EINVAL;
576		goto out;
577	}
578
579	uni2 = Add2Ptr(uni1, 2048);
580
581	ret = ntfs_nls_to_utf16(sbi, name->name, name->len,
582				(struct cpu_str *)uni2, NTFS_NAME_LEN,
583				UTF16_LITTLE_ENDIAN);
584	if (ret < 0)
585		goto out;
586
587	if (!ret) {
588		ret = -EINVAL;
589		goto out;
590	}
591
592	ret = !ntfs_cmp_names_cpu(uni1, uni2, sbi->upcase, false) ? 0 : 1;
593
594out:
595	__putname(uni1);
596	return ret;
597}
598
599// clang-format off
600const struct inode_operations ntfs_dir_inode_operations = {
601	.lookup		= ntfs_lookup,
602	.create		= ntfs_create,
603	.link		= ntfs_link,
604	.unlink		= ntfs_unlink,
605	.symlink	= ntfs_symlink,
606	.mkdir		= ntfs_mkdir,
607	.rmdir		= ntfs_rmdir,
608	.mknod		= ntfs_mknod,
609	.rename		= ntfs_rename,
610	.get_acl	= ntfs_get_acl,
611	.set_acl	= ntfs_set_acl,
612	.setattr	= ntfs3_setattr,
613	.getattr	= ntfs_getattr,
614	.listxattr	= ntfs_listxattr,
615	.atomic_open	= ntfs_atomic_open,
616	.fiemap		= ntfs_fiemap,
617};
618
619const struct inode_operations ntfs_special_inode_operations = {
620	.setattr	= ntfs3_setattr,
621	.getattr	= ntfs_getattr,
622	.listxattr	= ntfs_listxattr,
623	.get_acl	= ntfs_get_acl,
624	.set_acl	= ntfs_set_acl,
625};
626
627const struct dentry_operations ntfs_dentry_ops = {
628	.d_hash		= ntfs_d_hash,
629	.d_compare	= ntfs_d_compare,
630};
631
632// clang-format on
633