xref: /openbmc/linux/fs/ntfs3/inode.c (revision c494a447)
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/buffer_head.h>
9 #include <linux/fs.h>
10 #include <linux/mpage.h>
11 #include <linux/namei.h>
12 #include <linux/nls.h>
13 #include <linux/uio.h>
14 #include <linux/writeback.h>
15 
16 #include "debug.h"
17 #include "ntfs.h"
18 #include "ntfs_fs.h"
19 
20 /*
21  * ntfs_read_mft - Read record and parses MFT.
22  */
23 static struct inode *ntfs_read_mft(struct inode *inode,
24 				   const struct cpu_str *name,
25 				   const struct MFT_REF *ref)
26 {
27 	int err = 0;
28 	struct ntfs_inode *ni = ntfs_i(inode);
29 	struct super_block *sb = inode->i_sb;
30 	struct ntfs_sb_info *sbi = sb->s_fs_info;
31 	mode_t mode = 0;
32 	struct ATTR_STD_INFO5 *std5 = NULL;
33 	struct ATTR_LIST_ENTRY *le;
34 	struct ATTRIB *attr;
35 	bool is_match = false;
36 	bool is_root = false;
37 	bool is_dir;
38 	unsigned long ino = inode->i_ino;
39 	u32 rp_fa = 0, asize, t32;
40 	u16 roff, rsize, names = 0;
41 	const struct ATTR_FILE_NAME *fname = NULL;
42 	const struct INDEX_ROOT *root;
43 	struct REPARSE_DATA_BUFFER rp; // 0x18 bytes
44 	u64 t64;
45 	struct MFT_REC *rec;
46 	struct runs_tree *run;
47 
48 	inode->i_op = NULL;
49 	/* Setup 'uid' and 'gid' */
50 	inode->i_uid = sbi->options->fs_uid;
51 	inode->i_gid = sbi->options->fs_gid;
52 
53 	err = mi_init(&ni->mi, sbi, ino);
54 	if (err)
55 		goto out;
56 
57 	if (!sbi->mft.ni && ino == MFT_REC_MFT && !sb->s_root) {
58 		t64 = sbi->mft.lbo >> sbi->cluster_bits;
59 		t32 = bytes_to_cluster(sbi, MFT_REC_VOL * sbi->record_size);
60 		sbi->mft.ni = ni;
61 		init_rwsem(&ni->file.run_lock);
62 
63 		if (!run_add_entry(&ni->file.run, 0, t64, t32, true)) {
64 			err = -ENOMEM;
65 			goto out;
66 		}
67 	}
68 
69 	err = mi_read(&ni->mi, ino == MFT_REC_MFT);
70 
71 	if (err)
72 		goto out;
73 
74 	rec = ni->mi.mrec;
75 
76 	if (sbi->flags & NTFS_FLAGS_LOG_REPLAYING) {
77 		;
78 	} else if (ref->seq != rec->seq) {
79 		err = -EINVAL;
80 		ntfs_err(sb, "MFT: r=%lx, expect seq=%x instead of %x!", ino,
81 			 le16_to_cpu(ref->seq), le16_to_cpu(rec->seq));
82 		goto out;
83 	} else if (!is_rec_inuse(rec)) {
84 		err = -EINVAL;
85 		ntfs_err(sb, "Inode r=%x is not in use!", (u32)ino);
86 		goto out;
87 	}
88 
89 	if (le32_to_cpu(rec->total) != sbi->record_size) {
90 		/* Bad inode? */
91 		err = -EINVAL;
92 		goto out;
93 	}
94 
95 	if (!is_rec_base(rec))
96 		goto Ok;
97 
98 	/* Record should contain $I30 root. */
99 	is_dir = rec->flags & RECORD_FLAG_DIR;
100 
101 	inode->i_generation = le16_to_cpu(rec->seq);
102 
103 	/* Enumerate all struct Attributes MFT. */
104 	le = NULL;
105 	attr = NULL;
106 
107 	/*
108 	 * To reduce tab pressure use goto instead of
109 	 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
110 	 */
111 next_attr:
112 	run = NULL;
113 	err = -EINVAL;
114 	attr = ni_enum_attr_ex(ni, attr, &le, NULL);
115 	if (!attr)
116 		goto end_enum;
117 
118 	if (le && le->vcn) {
119 		/* This is non primary attribute segment. Ignore if not MFT. */
120 		if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
121 			goto next_attr;
122 
123 		run = &ni->file.run;
124 		asize = le32_to_cpu(attr->size);
125 		goto attr_unpack_run;
126 	}
127 
128 	roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
129 	rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
130 	asize = le32_to_cpu(attr->size);
131 
132 	switch (attr->type) {
133 	case ATTR_STD:
134 		if (attr->non_res ||
135 		    asize < sizeof(struct ATTR_STD_INFO) + roff ||
136 		    rsize < sizeof(struct ATTR_STD_INFO))
137 			goto out;
138 
139 		if (std5)
140 			goto next_attr;
141 
142 		std5 = Add2Ptr(attr, roff);
143 
144 #ifdef STATX_BTIME
145 		nt2kernel(std5->cr_time, &ni->i_crtime);
146 #endif
147 		nt2kernel(std5->a_time, &inode->i_atime);
148 		nt2kernel(std5->c_time, &inode->i_ctime);
149 		nt2kernel(std5->m_time, &inode->i_mtime);
150 
151 		ni->std_fa = std5->fa;
152 
153 		if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
154 		    rsize >= sizeof(struct ATTR_STD_INFO5))
155 			ni->std_security_id = std5->security_id;
156 		goto next_attr;
157 
158 	case ATTR_LIST:
159 		if (attr->name_len || le || ino == MFT_REC_LOG)
160 			goto out;
161 
162 		err = ntfs_load_attr_list(ni, attr);
163 		if (err)
164 			goto out;
165 
166 		le = NULL;
167 		attr = NULL;
168 		goto next_attr;
169 
170 	case ATTR_NAME:
171 		if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
172 		    rsize < SIZEOF_ATTRIBUTE_FILENAME)
173 			goto out;
174 
175 		fname = Add2Ptr(attr, roff);
176 		if (fname->type == FILE_NAME_DOS)
177 			goto next_attr;
178 
179 		names += 1;
180 		if (name && name->len == fname->name_len &&
181 		    !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
182 					NULL, false))
183 			is_match = true;
184 
185 		goto next_attr;
186 
187 	case ATTR_DATA:
188 		if (is_dir) {
189 			/* Ignore data attribute in dir record. */
190 			goto next_attr;
191 		}
192 
193 		if (ino == MFT_REC_BADCLUST && !attr->non_res)
194 			goto next_attr;
195 
196 		if (attr->name_len &&
197 		    ((ino != MFT_REC_BADCLUST || !attr->non_res ||
198 		      attr->name_len != ARRAY_SIZE(BAD_NAME) ||
199 		      memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
200 		     (ino != MFT_REC_SECURE || !attr->non_res ||
201 		      attr->name_len != ARRAY_SIZE(SDS_NAME) ||
202 		      memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
203 			/* File contains stream attribute. Ignore it. */
204 			goto next_attr;
205 		}
206 
207 		if (is_attr_sparsed(attr))
208 			ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
209 		else
210 			ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
211 
212 		if (is_attr_compressed(attr))
213 			ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
214 		else
215 			ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
216 
217 		if (is_attr_encrypted(attr))
218 			ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
219 		else
220 			ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
221 
222 		if (!attr->non_res) {
223 			ni->i_valid = inode->i_size = rsize;
224 			inode_set_bytes(inode, rsize);
225 		}
226 
227 		mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
228 
229 		if (!attr->non_res) {
230 			ni->ni_flags |= NI_FLAG_RESIDENT;
231 			goto next_attr;
232 		}
233 
234 		inode_set_bytes(inode, attr_ondisk_size(attr));
235 
236 		ni->i_valid = le64_to_cpu(attr->nres.valid_size);
237 		inode->i_size = le64_to_cpu(attr->nres.data_size);
238 		if (!attr->nres.alloc_size)
239 			goto next_attr;
240 
241 		run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run
242 					    : &ni->file.run;
243 		break;
244 
245 	case ATTR_ROOT:
246 		if (attr->non_res)
247 			goto out;
248 
249 		root = Add2Ptr(attr, roff);
250 		is_root = true;
251 
252 		if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
253 		    memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
254 			goto next_attr;
255 
256 		if (root->type != ATTR_NAME ||
257 		    root->rule != NTFS_COLLATION_TYPE_FILENAME)
258 			goto out;
259 
260 		if (!is_dir)
261 			goto next_attr;
262 
263 		ni->ni_flags |= NI_FLAG_DIR;
264 
265 		err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
266 		if (err)
267 			goto out;
268 
269 		mode = sb->s_root
270 			       ? (S_IFDIR | (0777 & sbi->options->fs_dmask_inv))
271 			       : (S_IFDIR | 0777);
272 		goto next_attr;
273 
274 	case ATTR_ALLOC:
275 		if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
276 		    memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
277 			goto next_attr;
278 
279 		inode->i_size = le64_to_cpu(attr->nres.data_size);
280 		ni->i_valid = le64_to_cpu(attr->nres.valid_size);
281 		inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
282 
283 		run = &ni->dir.alloc_run;
284 		break;
285 
286 	case ATTR_BITMAP:
287 		if (ino == MFT_REC_MFT) {
288 			if (!attr->non_res)
289 				goto out;
290 #ifndef CONFIG_NTFS3_64BIT_CLUSTER
291 			/* 0x20000000 = 2^32 / 8 */
292 			if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
293 				goto out;
294 #endif
295 			run = &sbi->mft.bitmap.run;
296 			break;
297 		} else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
298 			   !memcmp(attr_name(attr), I30_NAME,
299 				   sizeof(I30_NAME)) &&
300 			   attr->non_res) {
301 			run = &ni->dir.bitmap_run;
302 			break;
303 		}
304 		goto next_attr;
305 
306 	case ATTR_REPARSE:
307 		if (attr->name_len)
308 			goto next_attr;
309 
310 		rp_fa = ni_parse_reparse(ni, attr, &rp);
311 		switch (rp_fa) {
312 		case REPARSE_LINK:
313 			/*
314 			 * Normal symlink.
315 			 * Assume one unicode symbol == one utf8.
316 			 */
317 			inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer
318 							    .PrintNameLength) /
319 					sizeof(u16);
320 
321 			ni->i_valid = inode->i_size;
322 
323 			/* Clear directory bit. */
324 			if (ni->ni_flags & NI_FLAG_DIR) {
325 				indx_clear(&ni->dir);
326 				memset(&ni->dir, 0, sizeof(ni->dir));
327 				ni->ni_flags &= ~NI_FLAG_DIR;
328 			} else {
329 				run_close(&ni->file.run);
330 			}
331 			mode = S_IFLNK | 0777;
332 			is_dir = false;
333 			if (attr->non_res) {
334 				run = &ni->file.run;
335 				goto attr_unpack_run; // Double break.
336 			}
337 			break;
338 
339 		case REPARSE_COMPRESSED:
340 			break;
341 
342 		case REPARSE_DEDUPLICATED:
343 			break;
344 		}
345 		goto next_attr;
346 
347 	case ATTR_EA_INFO:
348 		if (!attr->name_len &&
349 		    resident_data_ex(attr, sizeof(struct EA_INFO))) {
350 			ni->ni_flags |= NI_FLAG_EA;
351 			/*
352 			 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
353 			 */
354 			inode->i_mode = mode;
355 			ntfs_get_wsl_perm(inode);
356 			mode = inode->i_mode;
357 		}
358 		goto next_attr;
359 
360 	default:
361 		goto next_attr;
362 	}
363 
364 attr_unpack_run:
365 	roff = le16_to_cpu(attr->nres.run_off);
366 
367 	t64 = le64_to_cpu(attr->nres.svcn);
368 	err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
369 			    t64, Add2Ptr(attr, roff), asize - roff);
370 	if (err < 0)
371 		goto out;
372 	err = 0;
373 	goto next_attr;
374 
375 end_enum:
376 
377 	if (!std5)
378 		goto out;
379 
380 	if (!is_match && name) {
381 		/* Reuse rec as buffer for ascii name. */
382 		err = -ENOENT;
383 		goto out;
384 	}
385 
386 	if (std5->fa & FILE_ATTRIBUTE_READONLY)
387 		mode &= ~0222;
388 
389 	if (!names) {
390 		err = -EINVAL;
391 		goto out;
392 	}
393 
394 	if (names != le16_to_cpu(rec->hard_links)) {
395 		/* Correct minor error on the fly. Do not mark inode as dirty. */
396 		rec->hard_links = cpu_to_le16(names);
397 		ni->mi.dirty = true;
398 	}
399 
400 	set_nlink(inode, names);
401 
402 	if (S_ISDIR(mode)) {
403 		ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
404 
405 		/*
406 		 * Dot and dot-dot should be included in count but was not
407 		 * included in enumeration.
408 		 * Usually a hard links to directories are disabled.
409 		 */
410 		inode->i_op = &ntfs_dir_inode_operations;
411 		inode->i_fop = &ntfs_dir_operations;
412 		ni->i_valid = 0;
413 	} else if (S_ISLNK(mode)) {
414 		ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
415 		inode->i_op = &ntfs_link_inode_operations;
416 		inode->i_fop = NULL;
417 		inode_nohighmem(inode);
418 	} else if (S_ISREG(mode)) {
419 		ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
420 		inode->i_op = &ntfs_file_inode_operations;
421 		inode->i_fop = &ntfs_file_operations;
422 		inode->i_mapping->a_ops =
423 			is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
424 		if (ino != MFT_REC_MFT)
425 			init_rwsem(&ni->file.run_lock);
426 	} else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
427 		   S_ISSOCK(mode)) {
428 		inode->i_op = &ntfs_special_inode_operations;
429 		init_special_inode(inode, mode, inode->i_rdev);
430 	} else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
431 		   fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
432 		/* Records in $Extend are not a files or general directories. */
433 	} else {
434 		err = -EINVAL;
435 		goto out;
436 	}
437 
438 	if ((sbi->options->sys_immutable &&
439 	     (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
440 	    !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
441 		inode->i_flags |= S_IMMUTABLE;
442 	} else {
443 		inode->i_flags &= ~S_IMMUTABLE;
444 	}
445 
446 	inode->i_mode = mode;
447 	if (!(ni->ni_flags & NI_FLAG_EA)) {
448 		/* If no xattr then no security (stored in xattr). */
449 		inode->i_flags |= S_NOSEC;
450 	}
451 
452 Ok:
453 	if (ino == MFT_REC_MFT && !sb->s_root)
454 		sbi->mft.ni = NULL;
455 
456 	unlock_new_inode(inode);
457 
458 	return inode;
459 
460 out:
461 	if (ino == MFT_REC_MFT && !sb->s_root)
462 		sbi->mft.ni = NULL;
463 
464 	iget_failed(inode);
465 	return ERR_PTR(err);
466 }
467 
468 /*
469  * ntfs_test_inode
470  *
471  * Return: 1 if match.
472  */
473 static int ntfs_test_inode(struct inode *inode, void *data)
474 {
475 	struct MFT_REF *ref = data;
476 
477 	return ino_get(ref) == inode->i_ino;
478 }
479 
480 static int ntfs_set_inode(struct inode *inode, void *data)
481 {
482 	const struct MFT_REF *ref = data;
483 
484 	inode->i_ino = ino_get(ref);
485 	return 0;
486 }
487 
488 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
489 			 const struct cpu_str *name)
490 {
491 	struct inode *inode;
492 
493 	inode = iget5_locked(sb, ino_get(ref), ntfs_test_inode, ntfs_set_inode,
494 			     (void *)ref);
495 	if (unlikely(!inode))
496 		return ERR_PTR(-ENOMEM);
497 
498 	/* If this is a freshly allocated inode, need to read it now. */
499 	if (inode->i_state & I_NEW)
500 		inode = ntfs_read_mft(inode, name, ref);
501 	else if (ref->seq != ntfs_i(inode)->mi.mrec->seq) {
502 		/* Inode overlaps? */
503 		make_bad_inode(inode);
504 	}
505 
506 	return inode;
507 }
508 
509 enum get_block_ctx {
510 	GET_BLOCK_GENERAL = 0,
511 	GET_BLOCK_WRITE_BEGIN = 1,
512 	GET_BLOCK_DIRECT_IO_R = 2,
513 	GET_BLOCK_DIRECT_IO_W = 3,
514 	GET_BLOCK_BMAP = 4,
515 };
516 
517 static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo,
518 				       struct buffer_head *bh, int create,
519 				       enum get_block_ctx ctx)
520 {
521 	struct super_block *sb = inode->i_sb;
522 	struct ntfs_sb_info *sbi = sb->s_fs_info;
523 	struct ntfs_inode *ni = ntfs_i(inode);
524 	struct page *page = bh->b_page;
525 	u8 cluster_bits = sbi->cluster_bits;
526 	u32 block_size = sb->s_blocksize;
527 	u64 bytes, lbo, valid;
528 	u32 off;
529 	int err;
530 	CLST vcn, lcn, len;
531 	bool new;
532 
533 	/* Clear previous state. */
534 	clear_buffer_new(bh);
535 	clear_buffer_uptodate(bh);
536 
537 	/* Direct write uses 'create=0'. */
538 	if (!create && vbo >= ni->i_valid) {
539 		/* Out of valid. */
540 		return 0;
541 	}
542 
543 	if (vbo >= inode->i_size) {
544 		/* Out of size. */
545 		return 0;
546 	}
547 
548 	if (is_resident(ni)) {
549 		ni_lock(ni);
550 		err = attr_data_read_resident(ni, page);
551 		ni_unlock(ni);
552 
553 		if (!err)
554 			set_buffer_uptodate(bh);
555 		bh->b_size = block_size;
556 		return err;
557 	}
558 
559 	vcn = vbo >> cluster_bits;
560 	off = vbo & sbi->cluster_mask;
561 	new = false;
562 
563 	err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL);
564 	if (err)
565 		goto out;
566 
567 	if (!len)
568 		return 0;
569 
570 	bytes = ((u64)len << cluster_bits) - off;
571 
572 	if (lcn == SPARSE_LCN) {
573 		if (!create) {
574 			if (bh->b_size > bytes)
575 				bh->b_size = bytes;
576 			return 0;
577 		}
578 		WARN_ON(1);
579 	}
580 
581 	if (new) {
582 		set_buffer_new(bh);
583 		if ((len << cluster_bits) > block_size)
584 			ntfs_sparse_cluster(inode, page, vcn, len);
585 	}
586 
587 	lbo = ((u64)lcn << cluster_bits) + off;
588 
589 	set_buffer_mapped(bh);
590 	bh->b_bdev = sb->s_bdev;
591 	bh->b_blocknr = lbo >> sb->s_blocksize_bits;
592 
593 	valid = ni->i_valid;
594 
595 	if (ctx == GET_BLOCK_DIRECT_IO_W) {
596 		/* ntfs_direct_IO will update ni->i_valid. */
597 		if (vbo >= valid)
598 			set_buffer_new(bh);
599 	} else if (create) {
600 		/* Normal write. */
601 		if (bytes > bh->b_size)
602 			bytes = bh->b_size;
603 
604 		if (vbo >= valid)
605 			set_buffer_new(bh);
606 
607 		if (vbo + bytes > valid) {
608 			ni->i_valid = vbo + bytes;
609 			mark_inode_dirty(inode);
610 		}
611 	} else if (vbo >= valid) {
612 		/* Read out of valid data. */
613 		/* Should never be here 'cause already checked. */
614 		clear_buffer_mapped(bh);
615 	} else if (vbo + bytes <= valid) {
616 		/* Normal read. */
617 	} else if (vbo + block_size <= valid) {
618 		/* Normal short read. */
619 		bytes = block_size;
620 	} else {
621 		/*
622 		 * Read across valid size: vbo < valid && valid < vbo + block_size
623 		 */
624 		bytes = block_size;
625 
626 		if (page) {
627 			u32 voff = valid - vbo;
628 
629 			bh->b_size = block_size;
630 			off = vbo & (PAGE_SIZE - 1);
631 			set_bh_page(bh, page, off);
632 			ll_rw_block(REQ_OP_READ, 0, 1, &bh);
633 			wait_on_buffer(bh);
634 			if (!buffer_uptodate(bh)) {
635 				err = -EIO;
636 				goto out;
637 			}
638 			zero_user_segment(page, off + voff, off + block_size);
639 		}
640 	}
641 
642 	if (bh->b_size > bytes)
643 		bh->b_size = bytes;
644 
645 #ifndef __LP64__
646 	if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) {
647 		static_assert(sizeof(size_t) < sizeof(loff_t));
648 		if (bytes > 0x40000000u)
649 			bh->b_size = 0x40000000u;
650 	}
651 #endif
652 
653 	return 0;
654 
655 out:
656 	return err;
657 }
658 
659 int ntfs_get_block(struct inode *inode, sector_t vbn,
660 		   struct buffer_head *bh_result, int create)
661 {
662 	return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
663 				  bh_result, create, GET_BLOCK_GENERAL);
664 }
665 
666 static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn,
667 			       struct buffer_head *bh_result, int create)
668 {
669 	return ntfs_get_block_vbo(inode,
670 				  (u64)vsn << inode->i_sb->s_blocksize_bits,
671 				  bh_result, create, GET_BLOCK_BMAP);
672 }
673 
674 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block)
675 {
676 	return generic_block_bmap(mapping, block, ntfs_get_block_bmap);
677 }
678 
679 static int ntfs_read_folio(struct file *file, struct folio *folio)
680 {
681 	struct page *page = &folio->page;
682 	int err;
683 	struct address_space *mapping = page->mapping;
684 	struct inode *inode = mapping->host;
685 	struct ntfs_inode *ni = ntfs_i(inode);
686 
687 	if (is_resident(ni)) {
688 		ni_lock(ni);
689 		err = attr_data_read_resident(ni, page);
690 		ni_unlock(ni);
691 		if (err != E_NTFS_NONRESIDENT) {
692 			unlock_page(page);
693 			return err;
694 		}
695 	}
696 
697 	if (is_compressed(ni)) {
698 		ni_lock(ni);
699 		err = ni_readpage_cmpr(ni, page);
700 		ni_unlock(ni);
701 		return err;
702 	}
703 
704 	/* Normal + sparse files. */
705 	return mpage_read_folio(folio, ntfs_get_block);
706 }
707 
708 static void ntfs_readahead(struct readahead_control *rac)
709 {
710 	struct address_space *mapping = rac->mapping;
711 	struct inode *inode = mapping->host;
712 	struct ntfs_inode *ni = ntfs_i(inode);
713 	u64 valid;
714 	loff_t pos;
715 
716 	if (is_resident(ni)) {
717 		/* No readahead for resident. */
718 		return;
719 	}
720 
721 	if (is_compressed(ni)) {
722 		/* No readahead for compressed. */
723 		return;
724 	}
725 
726 	valid = ni->i_valid;
727 	pos = readahead_pos(rac);
728 
729 	if (valid < i_size_read(inode) && pos <= valid &&
730 	    valid < pos + readahead_length(rac)) {
731 		/* Range cross 'valid'. Read it page by page. */
732 		return;
733 	}
734 
735 	mpage_readahead(rac, ntfs_get_block);
736 }
737 
738 static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock,
739 				      struct buffer_head *bh_result, int create)
740 {
741 	return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
742 				  bh_result, create, GET_BLOCK_DIRECT_IO_R);
743 }
744 
745 static int ntfs_get_block_direct_IO_W(struct inode *inode, sector_t iblock,
746 				      struct buffer_head *bh_result, int create)
747 {
748 	return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits,
749 				  bh_result, create, GET_BLOCK_DIRECT_IO_W);
750 }
751 
752 static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
753 {
754 	struct file *file = iocb->ki_filp;
755 	struct address_space *mapping = file->f_mapping;
756 	struct inode *inode = mapping->host;
757 	struct ntfs_inode *ni = ntfs_i(inode);
758 	loff_t vbo = iocb->ki_pos;
759 	loff_t end;
760 	int wr = iov_iter_rw(iter) & WRITE;
761 	size_t iter_count = iov_iter_count(iter);
762 	loff_t valid;
763 	ssize_t ret;
764 
765 	if (is_resident(ni)) {
766 		/* Switch to buffered write. */
767 		ret = 0;
768 		goto out;
769 	}
770 
771 	ret = blockdev_direct_IO(iocb, inode, iter,
772 				 wr ? ntfs_get_block_direct_IO_W
773 				    : ntfs_get_block_direct_IO_R);
774 
775 	if (ret > 0)
776 		end = vbo + ret;
777 	else if (wr && ret == -EIOCBQUEUED)
778 		end = vbo + iter_count;
779 	else
780 		goto out;
781 
782 	valid = ni->i_valid;
783 	if (wr) {
784 		if (end > valid && !S_ISBLK(inode->i_mode)) {
785 			ni->i_valid = end;
786 			mark_inode_dirty(inode);
787 		}
788 	} else if (vbo < valid && valid < end) {
789 		/* Fix page. */
790 		iov_iter_revert(iter, end - valid);
791 		iov_iter_zero(end - valid, iter);
792 	}
793 
794 out:
795 	return ret;
796 }
797 
798 int ntfs_set_size(struct inode *inode, u64 new_size)
799 {
800 	struct super_block *sb = inode->i_sb;
801 	struct ntfs_sb_info *sbi = sb->s_fs_info;
802 	struct ntfs_inode *ni = ntfs_i(inode);
803 	int err;
804 
805 	/* Check for maximum file size. */
806 	if (is_sparsed(ni) || is_compressed(ni)) {
807 		if (new_size > sbi->maxbytes_sparse) {
808 			err = -EFBIG;
809 			goto out;
810 		}
811 	} else if (new_size > sbi->maxbytes) {
812 		err = -EFBIG;
813 		goto out;
814 	}
815 
816 	ni_lock(ni);
817 	down_write(&ni->file.run_lock);
818 
819 	err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
820 			    &ni->i_valid, true, NULL);
821 
822 	up_write(&ni->file.run_lock);
823 	ni_unlock(ni);
824 
825 	mark_inode_dirty(inode);
826 
827 out:
828 	return err;
829 }
830 
831 static int ntfs_writepage(struct page *page, struct writeback_control *wbc)
832 {
833 	struct address_space *mapping = page->mapping;
834 	struct inode *inode = mapping->host;
835 	struct ntfs_inode *ni = ntfs_i(inode);
836 	int err;
837 
838 	if (is_resident(ni)) {
839 		ni_lock(ni);
840 		err = attr_data_write_resident(ni, page);
841 		ni_unlock(ni);
842 		if (err != E_NTFS_NONRESIDENT) {
843 			unlock_page(page);
844 			return err;
845 		}
846 	}
847 
848 	return block_write_full_page(page, ntfs_get_block, wbc);
849 }
850 
851 static int ntfs_writepages(struct address_space *mapping,
852 			   struct writeback_control *wbc)
853 {
854 	struct inode *inode = mapping->host;
855 	struct ntfs_inode *ni = ntfs_i(inode);
856 	/* Redirect call to 'ntfs_writepage' for resident files. */
857 	get_block_t *get_block = is_resident(ni) ? NULL : &ntfs_get_block;
858 
859 	return mpage_writepages(mapping, wbc, get_block);
860 }
861 
862 static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn,
863 				      struct buffer_head *bh_result, int create)
864 {
865 	return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits,
866 				  bh_result, create, GET_BLOCK_WRITE_BEGIN);
867 }
868 
869 int ntfs_write_begin(struct file *file, struct address_space *mapping,
870 		     loff_t pos, u32 len, struct page **pagep, void **fsdata)
871 {
872 	int err;
873 	struct inode *inode = mapping->host;
874 	struct ntfs_inode *ni = ntfs_i(inode);
875 
876 	*pagep = NULL;
877 	if (is_resident(ni)) {
878 		struct page *page = grab_cache_page_write_begin(
879 			mapping, pos >> PAGE_SHIFT);
880 
881 		if (!page) {
882 			err = -ENOMEM;
883 			goto out;
884 		}
885 
886 		ni_lock(ni);
887 		err = attr_data_read_resident(ni, page);
888 		ni_unlock(ni);
889 
890 		if (!err) {
891 			*pagep = page;
892 			goto out;
893 		}
894 		unlock_page(page);
895 		put_page(page);
896 
897 		if (err != E_NTFS_NONRESIDENT)
898 			goto out;
899 	}
900 
901 	err = block_write_begin(mapping, pos, len, pagep,
902 				ntfs_get_block_write_begin);
903 
904 out:
905 	return err;
906 }
907 
908 /*
909  * ntfs_write_end - Address_space_operations::write_end.
910  */
911 int ntfs_write_end(struct file *file, struct address_space *mapping,
912 		   loff_t pos, u32 len, u32 copied, struct page *page,
913 		   void *fsdata)
914 {
915 	struct inode *inode = mapping->host;
916 	struct ntfs_inode *ni = ntfs_i(inode);
917 	u64 valid = ni->i_valid;
918 	bool dirty = false;
919 	int err;
920 
921 	if (is_resident(ni)) {
922 		ni_lock(ni);
923 		err = attr_data_write_resident(ni, page);
924 		ni_unlock(ni);
925 		if (!err) {
926 			dirty = true;
927 			/* Clear any buffers in page. */
928 			if (page_has_buffers(page)) {
929 				struct buffer_head *head, *bh;
930 
931 				bh = head = page_buffers(page);
932 				do {
933 					clear_buffer_dirty(bh);
934 					clear_buffer_mapped(bh);
935 					set_buffer_uptodate(bh);
936 				} while (head != (bh = bh->b_this_page));
937 			}
938 			SetPageUptodate(page);
939 			err = copied;
940 		}
941 		unlock_page(page);
942 		put_page(page);
943 	} else {
944 		err = generic_write_end(file, mapping, pos, len, copied, page,
945 					fsdata);
946 	}
947 
948 	if (err >= 0) {
949 		if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
950 			inode->i_ctime = inode->i_mtime = current_time(inode);
951 			ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
952 			dirty = true;
953 		}
954 
955 		if (valid != ni->i_valid) {
956 			/* ni->i_valid is changed in ntfs_get_block_vbo. */
957 			dirty = true;
958 		}
959 
960 		if (dirty)
961 			mark_inode_dirty(inode);
962 	}
963 
964 	return err;
965 }
966 
967 int reset_log_file(struct inode *inode)
968 {
969 	int err;
970 	loff_t pos = 0;
971 	u32 log_size = inode->i_size;
972 	struct address_space *mapping = inode->i_mapping;
973 
974 	for (;;) {
975 		u32 len;
976 		void *kaddr;
977 		struct page *page;
978 
979 		len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
980 
981 		err = block_write_begin(mapping, pos, len, &page,
982 					ntfs_get_block_write_begin);
983 		if (err)
984 			goto out;
985 
986 		kaddr = kmap_atomic(page);
987 		memset(kaddr, -1, len);
988 		kunmap_atomic(kaddr);
989 		flush_dcache_page(page);
990 
991 		err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
992 		if (err < 0)
993 			goto out;
994 		pos += len;
995 
996 		if (pos >= log_size)
997 			break;
998 		balance_dirty_pages_ratelimited(mapping);
999 	}
1000 out:
1001 	mark_inode_dirty_sync(inode);
1002 
1003 	return err;
1004 }
1005 
1006 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1007 {
1008 	return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1009 }
1010 
1011 int ntfs_sync_inode(struct inode *inode)
1012 {
1013 	return _ni_write_inode(inode, 1);
1014 }
1015 
1016 /*
1017  * writeback_inode - Helper function for ntfs_flush_inodes().
1018  *
1019  * This writes both the inode and the file data blocks, waiting
1020  * for in flight data blocks before the start of the call.  It
1021  * does not wait for any io started during the call.
1022  */
1023 static int writeback_inode(struct inode *inode)
1024 {
1025 	int ret = sync_inode_metadata(inode, 0);
1026 
1027 	if (!ret)
1028 		ret = filemap_fdatawrite(inode->i_mapping);
1029 	return ret;
1030 }
1031 
1032 /*
1033  * ntfs_flush_inodes
1034  *
1035  * Write data and metadata corresponding to i1 and i2.  The io is
1036  * started but we do not wait for any of it to finish.
1037  *
1038  * filemap_flush() is used for the block device, so if there is a dirty
1039  * page for a block already in flight, we will not wait and start the
1040  * io over again.
1041  */
1042 int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1043 		      struct inode *i2)
1044 {
1045 	int ret = 0;
1046 
1047 	if (i1)
1048 		ret = writeback_inode(i1);
1049 	if (!ret && i2)
1050 		ret = writeback_inode(i2);
1051 	if (!ret)
1052 		ret = sync_blockdev_nowait(sb->s_bdev);
1053 	return ret;
1054 }
1055 
1056 int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1057 {
1058 	pgoff_t idx;
1059 
1060 	/* Write non resident data. */
1061 	for (idx = 0; bytes; idx++) {
1062 		size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1063 		struct page *page = ntfs_map_page(inode->i_mapping, idx);
1064 
1065 		if (IS_ERR(page))
1066 			return PTR_ERR(page);
1067 
1068 		lock_page(page);
1069 		WARN_ON(!PageUptodate(page));
1070 		ClearPageUptodate(page);
1071 
1072 		memcpy(page_address(page), data, op);
1073 
1074 		flush_dcache_page(page);
1075 		SetPageUptodate(page);
1076 		unlock_page(page);
1077 
1078 		ntfs_unmap_page(page);
1079 
1080 		bytes -= op;
1081 		data = Add2Ptr(data, PAGE_SIZE);
1082 	}
1083 	return 0;
1084 }
1085 
1086 /*
1087  * ntfs_reparse_bytes
1088  *
1089  * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
1090  * for unicode string of @uni_len length.
1091  */
1092 static inline u32 ntfs_reparse_bytes(u32 uni_len)
1093 {
1094 	/* Header + unicode string + decorated unicode string. */
1095 	return sizeof(short) * (2 * uni_len + 4) +
1096 	       offsetof(struct REPARSE_DATA_BUFFER,
1097 			SymbolicLinkReparseBuffer.PathBuffer);
1098 }
1099 
1100 static struct REPARSE_DATA_BUFFER *
1101 ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1102 			   u32 size, u16 *nsize)
1103 {
1104 	int i, err;
1105 	struct REPARSE_DATA_BUFFER *rp;
1106 	__le16 *rp_name;
1107 	typeof(rp->SymbolicLinkReparseBuffer) *rs;
1108 
1109 	rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
1110 	if (!rp)
1111 		return ERR_PTR(-ENOMEM);
1112 
1113 	rs = &rp->SymbolicLinkReparseBuffer;
1114 	rp_name = rs->PathBuffer;
1115 
1116 	/* Convert link name to UTF-16. */
1117 	err = ntfs_nls_to_utf16(sbi, symname, size,
1118 				(struct cpu_str *)(rp_name - 1), 2 * size,
1119 				UTF16_LITTLE_ENDIAN);
1120 	if (err < 0)
1121 		goto out;
1122 
1123 	/* err = the length of unicode name of symlink. */
1124 	*nsize = ntfs_reparse_bytes(err);
1125 
1126 	if (*nsize > sbi->reparse.max_size) {
1127 		err = -EFBIG;
1128 		goto out;
1129 	}
1130 
1131 	/* Translate Linux '/' into Windows '\'. */
1132 	for (i = 0; i < err; i++) {
1133 		if (rp_name[i] == cpu_to_le16('/'))
1134 			rp_name[i] = cpu_to_le16('\\');
1135 	}
1136 
1137 	rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1138 	rp->ReparseDataLength =
1139 		cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1140 					      SymbolicLinkReparseBuffer));
1141 
1142 	/* PrintName + SubstituteName. */
1143 	rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1144 	rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1145 	rs->PrintNameLength = rs->SubstituteNameOffset;
1146 
1147 	/*
1148 	 * TODO: Use relative path if possible to allow Windows to
1149 	 * parse this path.
1150 	 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
1151 	 */
1152 	rs->Flags = 0;
1153 
1154 	memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1155 
1156 	/* Decorate SubstituteName. */
1157 	rp_name += err;
1158 	rp_name[0] = cpu_to_le16('\\');
1159 	rp_name[1] = cpu_to_le16('?');
1160 	rp_name[2] = cpu_to_le16('?');
1161 	rp_name[3] = cpu_to_le16('\\');
1162 
1163 	return rp;
1164 out:
1165 	kfree(rp);
1166 	return ERR_PTR(err);
1167 }
1168 
1169 struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1170 				struct inode *dir, struct dentry *dentry,
1171 				const struct cpu_str *uni, umode_t mode,
1172 				dev_t dev, const char *symname, u32 size,
1173 				struct ntfs_fnd *fnd)
1174 {
1175 	int err;
1176 	struct super_block *sb = dir->i_sb;
1177 	struct ntfs_sb_info *sbi = sb->s_fs_info;
1178 	const struct qstr *name = &dentry->d_name;
1179 	CLST ino = 0;
1180 	struct ntfs_inode *dir_ni = ntfs_i(dir);
1181 	struct ntfs_inode *ni = NULL;
1182 	struct inode *inode = NULL;
1183 	struct ATTRIB *attr;
1184 	struct ATTR_STD_INFO5 *std5;
1185 	struct ATTR_FILE_NAME *fname;
1186 	struct MFT_REC *rec;
1187 	u32 asize, dsize, sd_size;
1188 	enum FILE_ATTRIBUTE fa;
1189 	__le32 security_id = SECURITY_ID_INVALID;
1190 	CLST vcn;
1191 	const void *sd;
1192 	u16 t16, nsize = 0, aid = 0;
1193 	struct INDEX_ROOT *root, *dir_root;
1194 	struct NTFS_DE *e, *new_de = NULL;
1195 	struct REPARSE_DATA_BUFFER *rp = NULL;
1196 	bool rp_inserted = false;
1197 
1198 	ni_lock_dir(dir_ni);
1199 
1200 	dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
1201 	if (!dir_root) {
1202 		err = -EINVAL;
1203 		goto out1;
1204 	}
1205 
1206 	if (S_ISDIR(mode)) {
1207 		/* Use parent's directory attributes. */
1208 		fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1209 		     FILE_ATTRIBUTE_ARCHIVE;
1210 		/*
1211 		 * By default child directory inherits parent attributes.
1212 		 * Root directory is hidden + system.
1213 		 * Make an exception for children in root.
1214 		 */
1215 		if (dir->i_ino == MFT_REC_ROOT)
1216 			fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1217 	} else if (S_ISLNK(mode)) {
1218 		/* It is good idea that link should be the same type (file/dir) as target */
1219 		fa = FILE_ATTRIBUTE_REPARSE_POINT;
1220 
1221 		/*
1222 		 * Linux: there are dir/file/symlink and so on.
1223 		 * NTFS: symlinks are "dir + reparse" or "file + reparse"
1224 		 * It is good idea to create:
1225 		 * dir + reparse if 'symname' points to directory
1226 		 * or
1227 		 * file + reparse if 'symname' points to file
1228 		 * Unfortunately kern_path hangs if symname contains 'dir'.
1229 		 */
1230 
1231 		/*
1232 		 *	struct path path;
1233 		 *
1234 		 *	if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1235 		 *		struct inode *target = d_inode(path.dentry);
1236 		 *
1237 		 *		if (S_ISDIR(target->i_mode))
1238 		 *			fa |= FILE_ATTRIBUTE_DIRECTORY;
1239 		 *		// if ( target->i_sb == sb ){
1240 		 *		//	use relative path?
1241 		 *		// }
1242 		 *		path_put(&path);
1243 		 *	}
1244 		 */
1245 	} else if (S_ISREG(mode)) {
1246 		if (sbi->options->sparse) {
1247 			/* Sparsed regular file, cause option 'sparse'. */
1248 			fa = FILE_ATTRIBUTE_SPARSE_FILE |
1249 			     FILE_ATTRIBUTE_ARCHIVE;
1250 		} else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
1251 			/* Compressed regular file, if parent is compressed. */
1252 			fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1253 		} else {
1254 			/* Regular file, default attributes. */
1255 			fa = FILE_ATTRIBUTE_ARCHIVE;
1256 		}
1257 	} else {
1258 		fa = FILE_ATTRIBUTE_ARCHIVE;
1259 	}
1260 
1261 	if (!(mode & 0222))
1262 		fa |= FILE_ATTRIBUTE_READONLY;
1263 
1264 	/* Allocate PATH_MAX bytes. */
1265 	new_de = __getname();
1266 	if (!new_de) {
1267 		err = -ENOMEM;
1268 		goto out1;
1269 	}
1270 
1271 	/* Mark rw ntfs as dirty. it will be cleared at umount. */
1272 	ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1273 
1274 	/* Step 1: allocate and fill new mft record. */
1275 	err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1276 	if (err)
1277 		goto out2;
1278 
1279 	ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1280 	if (IS_ERR(ni)) {
1281 		err = PTR_ERR(ni);
1282 		ni = NULL;
1283 		goto out3;
1284 	}
1285 	inode = &ni->vfs_inode;
1286 	inode_init_owner(mnt_userns, inode, dir, mode);
1287 	mode = inode->i_mode;
1288 
1289 	inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1290 		current_time(inode);
1291 
1292 	rec = ni->mi.mrec;
1293 	rec->hard_links = cpu_to_le16(1);
1294 	attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1295 
1296 	/* Get default security id. */
1297 	sd = s_default_security;
1298 	sd_size = sizeof(s_default_security);
1299 
1300 	if (is_ntfs3(sbi)) {
1301 		security_id = dir_ni->std_security_id;
1302 		if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1303 			security_id = sbi->security.def_security_id;
1304 
1305 			if (security_id == SECURITY_ID_INVALID &&
1306 			    !ntfs_insert_security(sbi, sd, sd_size,
1307 						  &security_id, NULL))
1308 				sbi->security.def_security_id = security_id;
1309 		}
1310 	}
1311 
1312 	/* Insert standard info. */
1313 	std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1314 
1315 	if (security_id == SECURITY_ID_INVALID) {
1316 		dsize = sizeof(struct ATTR_STD_INFO);
1317 	} else {
1318 		dsize = sizeof(struct ATTR_STD_INFO5);
1319 		std5->security_id = security_id;
1320 		ni->std_security_id = security_id;
1321 	}
1322 	asize = SIZEOF_RESIDENT + dsize;
1323 
1324 	attr->type = ATTR_STD;
1325 	attr->size = cpu_to_le32(asize);
1326 	attr->id = cpu_to_le16(aid++);
1327 	attr->res.data_off = SIZEOF_RESIDENT_LE;
1328 	attr->res.data_size = cpu_to_le32(dsize);
1329 
1330 	std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1331 		kernel2nt(&inode->i_atime);
1332 
1333 	ni->std_fa = fa;
1334 	std5->fa = fa;
1335 
1336 	attr = Add2Ptr(attr, asize);
1337 
1338 	/* Insert file name. */
1339 	err = fill_name_de(sbi, new_de, name, uni);
1340 	if (err)
1341 		goto out4;
1342 
1343 	mi_get_ref(&ni->mi, &new_de->ref);
1344 
1345 	fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1346 	mi_get_ref(&dir_ni->mi, &fname->home);
1347 	fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1348 		fname->dup.a_time = std5->cr_time;
1349 	fname->dup.alloc_size = fname->dup.data_size = 0;
1350 	fname->dup.fa = std5->fa;
1351 	fname->dup.ea_size = fname->dup.reparse = 0;
1352 
1353 	dsize = le16_to_cpu(new_de->key_size);
1354 	asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
1355 
1356 	attr->type = ATTR_NAME;
1357 	attr->size = cpu_to_le32(asize);
1358 	attr->res.data_off = SIZEOF_RESIDENT_LE;
1359 	attr->res.flags = RESIDENT_FLAG_INDEXED;
1360 	attr->id = cpu_to_le16(aid++);
1361 	attr->res.data_size = cpu_to_le32(dsize);
1362 	memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1363 
1364 	attr = Add2Ptr(attr, asize);
1365 
1366 	if (security_id == SECURITY_ID_INVALID) {
1367 		/* Insert security attribute. */
1368 		asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
1369 
1370 		attr->type = ATTR_SECURE;
1371 		attr->size = cpu_to_le32(asize);
1372 		attr->id = cpu_to_le16(aid++);
1373 		attr->res.data_off = SIZEOF_RESIDENT_LE;
1374 		attr->res.data_size = cpu_to_le32(sd_size);
1375 		memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1376 
1377 		attr = Add2Ptr(attr, asize);
1378 	}
1379 
1380 	attr->id = cpu_to_le16(aid++);
1381 	if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1382 		/*
1383 		 * Regular directory or symlink to directory.
1384 		 * Create root attribute.
1385 		 */
1386 		dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1387 		asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1388 
1389 		attr->type = ATTR_ROOT;
1390 		attr->size = cpu_to_le32(asize);
1391 
1392 		attr->name_len = ARRAY_SIZE(I30_NAME);
1393 		attr->name_off = SIZEOF_RESIDENT_LE;
1394 		attr->res.data_off =
1395 			cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1396 		attr->res.data_size = cpu_to_le32(dsize);
1397 		memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1398 		       sizeof(I30_NAME));
1399 
1400 		root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1401 		memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1402 		root->ihdr.de_off =
1403 			cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1404 		root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1405 					      sizeof(struct NTFS_DE));
1406 		root->ihdr.total = root->ihdr.used;
1407 
1408 		e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1409 		e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1410 		e->flags = NTFS_IE_LAST;
1411 	} else if (S_ISLNK(mode)) {
1412 		/*
1413 		 * Symlink to file.
1414 		 * Create empty resident data attribute.
1415 		 */
1416 		asize = SIZEOF_RESIDENT;
1417 
1418 		/* Insert empty ATTR_DATA */
1419 		attr->type = ATTR_DATA;
1420 		attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1421 		attr->name_off = SIZEOF_RESIDENT_LE;
1422 		attr->res.data_off = SIZEOF_RESIDENT_LE;
1423 	} else if (S_ISREG(mode)) {
1424 		/*
1425 		 * Regular file. Create empty non resident data attribute.
1426 		 */
1427 		attr->type = ATTR_DATA;
1428 		attr->non_res = 1;
1429 		attr->nres.evcn = cpu_to_le64(-1ll);
1430 		if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1431 			attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1432 			attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1433 			attr->flags = ATTR_FLAG_SPARSED;
1434 			asize = SIZEOF_NONRESIDENT_EX + 8;
1435 		} else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1436 			attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1437 			attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1438 			attr->flags = ATTR_FLAG_COMPRESSED;
1439 			attr->nres.c_unit = COMPRESSION_UNIT;
1440 			asize = SIZEOF_NONRESIDENT_EX + 8;
1441 		} else {
1442 			attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1443 			attr->name_off = SIZEOF_NONRESIDENT_LE;
1444 			asize = SIZEOF_NONRESIDENT + 8;
1445 		}
1446 		attr->nres.run_off = attr->name_off;
1447 	} else {
1448 		/*
1449 		 * Node. Create empty resident data attribute.
1450 		 */
1451 		attr->type = ATTR_DATA;
1452 		attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1453 		attr->name_off = SIZEOF_RESIDENT_LE;
1454 		if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1455 			attr->flags = ATTR_FLAG_SPARSED;
1456 		else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1457 			attr->flags = ATTR_FLAG_COMPRESSED;
1458 		attr->res.data_off = SIZEOF_RESIDENT_LE;
1459 		asize = SIZEOF_RESIDENT;
1460 		ni->ni_flags |= NI_FLAG_RESIDENT;
1461 	}
1462 
1463 	if (S_ISDIR(mode)) {
1464 		ni->ni_flags |= NI_FLAG_DIR;
1465 		err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1466 		if (err)
1467 			goto out4;
1468 	} else if (S_ISLNK(mode)) {
1469 		rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1470 
1471 		if (IS_ERR(rp)) {
1472 			err = PTR_ERR(rp);
1473 			rp = NULL;
1474 			goto out4;
1475 		}
1476 
1477 		/*
1478 		 * Insert ATTR_REPARSE.
1479 		 */
1480 		attr = Add2Ptr(attr, asize);
1481 		attr->type = ATTR_REPARSE;
1482 		attr->id = cpu_to_le16(aid++);
1483 
1484 		/* Resident or non resident? */
1485 		asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
1486 		t16 = PtrOffset(rec, attr);
1487 
1488 		/*
1489 		 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1490 		 * It is good idea to keep extened attributes resident.
1491 		 */
1492 		if (asize + t16 + 0x78 + 8 > sbi->record_size) {
1493 			CLST alen;
1494 			CLST clst = bytes_to_cluster(sbi, nsize);
1495 
1496 			/* Bytes per runs. */
1497 			t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1498 
1499 			attr->non_res = 1;
1500 			attr->nres.evcn = cpu_to_le64(clst - 1);
1501 			attr->name_off = SIZEOF_NONRESIDENT_LE;
1502 			attr->nres.run_off = attr->name_off;
1503 			attr->nres.data_size = cpu_to_le64(nsize);
1504 			attr->nres.valid_size = attr->nres.data_size;
1505 			attr->nres.alloc_size =
1506 				cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1507 
1508 			err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1509 						     clst, NULL, 0, &alen, 0,
1510 						     NULL);
1511 			if (err)
1512 				goto out5;
1513 
1514 			err = run_pack(&ni->file.run, 0, clst,
1515 				       Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1516 				       &vcn);
1517 			if (err < 0)
1518 				goto out5;
1519 
1520 			if (vcn != clst) {
1521 				err = -EINVAL;
1522 				goto out5;
1523 			}
1524 
1525 			asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
1526 		} else {
1527 			attr->res.data_off = SIZEOF_RESIDENT_LE;
1528 			attr->res.data_size = cpu_to_le32(nsize);
1529 			memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
1530 			nsize = 0;
1531 		}
1532 		/* Size of symlink equals the length of input string. */
1533 		inode->i_size = size;
1534 
1535 		attr->size = cpu_to_le32(asize);
1536 
1537 		err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1538 					  &new_de->ref);
1539 		if (err)
1540 			goto out5;
1541 
1542 		rp_inserted = true;
1543 	}
1544 
1545 	attr = Add2Ptr(attr, asize);
1546 	attr->type = ATTR_END;
1547 
1548 	rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1549 	rec->next_attr_id = cpu_to_le16(aid);
1550 
1551 	/* Step 2: Add new name in index. */
1552 	err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
1553 	if (err)
1554 		goto out6;
1555 
1556 	/* Unlock parent directory before ntfs_init_acl. */
1557 	ni_unlock(dir_ni);
1558 
1559 	inode->i_generation = le16_to_cpu(rec->seq);
1560 
1561 	dir->i_mtime = dir->i_ctime = inode->i_atime;
1562 
1563 	if (S_ISDIR(mode)) {
1564 		inode->i_op = &ntfs_dir_inode_operations;
1565 		inode->i_fop = &ntfs_dir_operations;
1566 	} else if (S_ISLNK(mode)) {
1567 		inode->i_op = &ntfs_link_inode_operations;
1568 		inode->i_fop = NULL;
1569 		inode->i_mapping->a_ops = &ntfs_aops;
1570 		inode->i_size = size;
1571 		inode_nohighmem(inode);
1572 	} else if (S_ISREG(mode)) {
1573 		inode->i_op = &ntfs_file_inode_operations;
1574 		inode->i_fop = &ntfs_file_operations;
1575 		inode->i_mapping->a_ops =
1576 			is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1577 		init_rwsem(&ni->file.run_lock);
1578 	} else {
1579 		inode->i_op = &ntfs_special_inode_operations;
1580 		init_special_inode(inode, mode, dev);
1581 	}
1582 
1583 #ifdef CONFIG_NTFS3_FS_POSIX_ACL
1584 	if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1585 		err = ntfs_init_acl(mnt_userns, inode, dir);
1586 		if (err)
1587 			goto out7;
1588 	} else
1589 #endif
1590 	{
1591 		inode->i_flags |= S_NOSEC;
1592 	}
1593 
1594 	/* Write non resident data. */
1595 	if (nsize) {
1596 		err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
1597 		if (err)
1598 			goto out7;
1599 	}
1600 
1601 	/*
1602 	 * Call 'd_instantiate' after inode->i_op is set
1603 	 * but before finish_open.
1604 	 */
1605 	d_instantiate(dentry, inode);
1606 
1607 	ntfs_save_wsl_perm(inode);
1608 	mark_inode_dirty(dir);
1609 	mark_inode_dirty(inode);
1610 
1611 	/* Normal exit. */
1612 	goto out2;
1613 
1614 out7:
1615 
1616 	/* Undo 'indx_insert_entry'. */
1617 	ni_lock_dir(dir_ni);
1618 	indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1619 			  le16_to_cpu(new_de->key_size), sbi);
1620 	/* ni_unlock(dir_ni); will be called later. */
1621 out6:
1622 	if (rp_inserted)
1623 		ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1624 
1625 out5:
1626 	if (S_ISDIR(mode) || run_is_empty(&ni->file.run))
1627 		goto out4;
1628 
1629 	run_deallocate(sbi, &ni->file.run, false);
1630 
1631 out4:
1632 	clear_rec_inuse(rec);
1633 	clear_nlink(inode);
1634 	ni->mi.dirty = false;
1635 	discard_new_inode(inode);
1636 out3:
1637 	ntfs_mark_rec_free(sbi, ino);
1638 
1639 out2:
1640 	__putname(new_de);
1641 	kfree(rp);
1642 
1643 out1:
1644 	if (err) {
1645 		ni_unlock(dir_ni);
1646 		return ERR_PTR(err);
1647 	}
1648 
1649 	unlock_new_inode(inode);
1650 
1651 	return inode;
1652 }
1653 
1654 int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1655 {
1656 	int err;
1657 	struct ntfs_inode *ni = ntfs_i(inode);
1658 	struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1659 	struct NTFS_DE *de;
1660 	struct ATTR_FILE_NAME *de_name;
1661 
1662 	/* Allocate PATH_MAX bytes. */
1663 	de = __getname();
1664 	if (!de)
1665 		return -ENOMEM;
1666 
1667 	/* Mark rw ntfs as dirty. It will be cleared at umount. */
1668 	ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1669 
1670 	/* Construct 'de'. */
1671 	err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1672 	if (err)
1673 		goto out;
1674 
1675 	de_name = (struct ATTR_FILE_NAME *)(de + 1);
1676 	/* Fill duplicate info. */
1677 	de_name->dup.cr_time = de_name->dup.m_time = de_name->dup.c_time =
1678 		de_name->dup.a_time = kernel2nt(&inode->i_ctime);
1679 	de_name->dup.alloc_size = de_name->dup.data_size =
1680 		cpu_to_le64(inode->i_size);
1681 	de_name->dup.fa = ni->std_fa;
1682 	de_name->dup.ea_size = de_name->dup.reparse = 0;
1683 
1684 	err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
1685 out:
1686 	__putname(de);
1687 	return err;
1688 }
1689 
1690 /*
1691  * ntfs_unlink_inode
1692  *
1693  * inode_operations::unlink
1694  * inode_operations::rmdir
1695  */
1696 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1697 {
1698 	int err;
1699 	struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
1700 	struct inode *inode = d_inode(dentry);
1701 	struct ntfs_inode *ni = ntfs_i(inode);
1702 	struct ntfs_inode *dir_ni = ntfs_i(dir);
1703 	struct NTFS_DE *de, *de2 = NULL;
1704 	int undo_remove;
1705 
1706 	if (ntfs_is_meta_file(sbi, ni->mi.rno))
1707 		return -EINVAL;
1708 
1709 	/* Allocate PATH_MAX bytes. */
1710 	de = __getname();
1711 	if (!de)
1712 		return -ENOMEM;
1713 
1714 	ni_lock(ni);
1715 
1716 	if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
1717 		err = -ENOTEMPTY;
1718 		goto out;
1719 	}
1720 
1721 	err = fill_name_de(sbi, de, &dentry->d_name, NULL);
1722 	if (err < 0)
1723 		goto out;
1724 
1725 	undo_remove = 0;
1726 	err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
1727 
1728 	if (!err) {
1729 		drop_nlink(inode);
1730 		dir->i_mtime = dir->i_ctime = current_time(dir);
1731 		mark_inode_dirty(dir);
1732 		inode->i_ctime = dir->i_ctime;
1733 		if (inode->i_nlink)
1734 			mark_inode_dirty(inode);
1735 	} else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
1736 		make_bad_inode(inode);
1737 		ntfs_inode_err(inode, "failed to undo unlink");
1738 		ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1739 	} else {
1740 		if (ni_is_dirty(dir))
1741 			mark_inode_dirty(dir);
1742 		if (ni_is_dirty(inode))
1743 			mark_inode_dirty(inode);
1744 	}
1745 
1746 out:
1747 	ni_unlock(ni);
1748 	__putname(de);
1749 	return err;
1750 }
1751 
1752 void ntfs_evict_inode(struct inode *inode)
1753 {
1754 	truncate_inode_pages_final(&inode->i_data);
1755 
1756 	if (inode->i_nlink)
1757 		_ni_write_inode(inode, inode_needs_sync(inode));
1758 
1759 	invalidate_inode_buffers(inode);
1760 	clear_inode(inode);
1761 
1762 	ni_clear(ntfs_i(inode));
1763 }
1764 
1765 static noinline int ntfs_readlink_hlp(struct inode *inode, char *buffer,
1766 				      int buflen)
1767 {
1768 	int i, err = -EINVAL;
1769 	struct ntfs_inode *ni = ntfs_i(inode);
1770 	struct super_block *sb = inode->i_sb;
1771 	struct ntfs_sb_info *sbi = sb->s_fs_info;
1772 	u64 size;
1773 	u16 ulen = 0;
1774 	void *to_free = NULL;
1775 	struct REPARSE_DATA_BUFFER *rp;
1776 	const __le16 *uname;
1777 	struct ATTRIB *attr;
1778 
1779 	/* Reparse data present. Try to parse it. */
1780 	static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1781 	static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1782 
1783 	*buffer = 0;
1784 
1785 	attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
1786 	if (!attr)
1787 		goto out;
1788 
1789 	if (!attr->non_res) {
1790 		rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1791 		if (!rp)
1792 			goto out;
1793 		size = le32_to_cpu(attr->res.data_size);
1794 	} else {
1795 		size = le64_to_cpu(attr->nres.data_size);
1796 		rp = NULL;
1797 	}
1798 
1799 	if (size > sbi->reparse.max_size || size <= sizeof(u32))
1800 		goto out;
1801 
1802 	if (!rp) {
1803 		rp = kmalloc(size, GFP_NOFS);
1804 		if (!rp) {
1805 			err = -ENOMEM;
1806 			goto out;
1807 		}
1808 		to_free = rp;
1809 		/* Read into temporal buffer. */
1810 		err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
1811 		if (err)
1812 			goto out;
1813 	}
1814 
1815 	/* Microsoft Tag. */
1816 	switch (rp->ReparseTag) {
1817 	case IO_REPARSE_TAG_MOUNT_POINT:
1818 		/* Mount points and junctions. */
1819 		/* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
1820 		if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1821 				     MountPointReparseBuffer.PathBuffer))
1822 			goto out;
1823 		uname = Add2Ptr(rp,
1824 				offsetof(struct REPARSE_DATA_BUFFER,
1825 					 MountPointReparseBuffer.PathBuffer) +
1826 					le16_to_cpu(rp->MountPointReparseBuffer
1827 							    .PrintNameOffset));
1828 		ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
1829 		break;
1830 
1831 	case IO_REPARSE_TAG_SYMLINK:
1832 		/* FolderSymbolicLink */
1833 		/* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
1834 		if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1835 				     SymbolicLinkReparseBuffer.PathBuffer))
1836 			goto out;
1837 		uname = Add2Ptr(
1838 			rp, offsetof(struct REPARSE_DATA_BUFFER,
1839 				     SymbolicLinkReparseBuffer.PathBuffer) +
1840 				    le16_to_cpu(rp->SymbolicLinkReparseBuffer
1841 							.PrintNameOffset));
1842 		ulen = le16_to_cpu(
1843 			rp->SymbolicLinkReparseBuffer.PrintNameLength);
1844 		break;
1845 
1846 	case IO_REPARSE_TAG_CLOUD:
1847 	case IO_REPARSE_TAG_CLOUD_1:
1848 	case IO_REPARSE_TAG_CLOUD_2:
1849 	case IO_REPARSE_TAG_CLOUD_3:
1850 	case IO_REPARSE_TAG_CLOUD_4:
1851 	case IO_REPARSE_TAG_CLOUD_5:
1852 	case IO_REPARSE_TAG_CLOUD_6:
1853 	case IO_REPARSE_TAG_CLOUD_7:
1854 	case IO_REPARSE_TAG_CLOUD_8:
1855 	case IO_REPARSE_TAG_CLOUD_9:
1856 	case IO_REPARSE_TAG_CLOUD_A:
1857 	case IO_REPARSE_TAG_CLOUD_B:
1858 	case IO_REPARSE_TAG_CLOUD_C:
1859 	case IO_REPARSE_TAG_CLOUD_D:
1860 	case IO_REPARSE_TAG_CLOUD_E:
1861 	case IO_REPARSE_TAG_CLOUD_F:
1862 		err = sizeof("OneDrive") - 1;
1863 		if (err > buflen)
1864 			err = buflen;
1865 		memcpy(buffer, "OneDrive", err);
1866 		goto out;
1867 
1868 	default:
1869 		if (IsReparseTagMicrosoft(rp->ReparseTag)) {
1870 			/* Unknown Microsoft Tag. */
1871 			goto out;
1872 		}
1873 		if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
1874 		    size <= sizeof(struct REPARSE_POINT)) {
1875 			goto out;
1876 		}
1877 
1878 		/* Users tag. */
1879 		uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
1880 		ulen = le16_to_cpu(rp->ReparseDataLength) -
1881 		       sizeof(struct REPARSE_POINT);
1882 	}
1883 
1884 	/* Convert nlen from bytes to UNICODE chars. */
1885 	ulen >>= 1;
1886 
1887 	/* Check that name is available. */
1888 	if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
1889 		goto out;
1890 
1891 	/* If name is already zero terminated then truncate it now. */
1892 	if (!uname[ulen - 1])
1893 		ulen -= 1;
1894 
1895 	err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
1896 
1897 	if (err < 0)
1898 		goto out;
1899 
1900 	/* Translate Windows '\' into Linux '/'. */
1901 	for (i = 0; i < err; i++) {
1902 		if (buffer[i] == '\\')
1903 			buffer[i] = '/';
1904 	}
1905 
1906 	/* Always set last zero. */
1907 	buffer[err] = 0;
1908 out:
1909 	kfree(to_free);
1910 	return err;
1911 }
1912 
1913 static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
1914 				 struct delayed_call *done)
1915 {
1916 	int err;
1917 	char *ret;
1918 
1919 	if (!de)
1920 		return ERR_PTR(-ECHILD);
1921 
1922 	ret = kmalloc(PAGE_SIZE, GFP_NOFS);
1923 	if (!ret)
1924 		return ERR_PTR(-ENOMEM);
1925 
1926 	err = ntfs_readlink_hlp(inode, ret, PAGE_SIZE);
1927 	if (err < 0) {
1928 		kfree(ret);
1929 		return ERR_PTR(err);
1930 	}
1931 
1932 	set_delayed_call(done, kfree_link, ret);
1933 
1934 	return ret;
1935 }
1936 
1937 // clang-format off
1938 const struct inode_operations ntfs_link_inode_operations = {
1939 	.get_link	= ntfs_get_link,
1940 	.setattr	= ntfs3_setattr,
1941 	.listxattr	= ntfs_listxattr,
1942 	.permission	= ntfs_permission,
1943 	.get_acl	= ntfs_get_acl,
1944 	.set_acl	= ntfs_set_acl,
1945 };
1946 
1947 const struct address_space_operations ntfs_aops = {
1948 	.read_folio	= ntfs_read_folio,
1949 	.readahead	= ntfs_readahead,
1950 	.writepage	= ntfs_writepage,
1951 	.writepages	= ntfs_writepages,
1952 	.write_begin	= ntfs_write_begin,
1953 	.write_end	= ntfs_write_end,
1954 	.direct_IO	= ntfs_direct_IO,
1955 	.bmap		= ntfs_bmap,
1956 	.dirty_folio	= block_dirty_folio,
1957 	.invalidate_folio = block_invalidate_folio,
1958 };
1959 
1960 const struct address_space_operations ntfs_aops_cmpr = {
1961 	.read_folio	= ntfs_read_folio,
1962 	.readahead	= ntfs_readahead,
1963 };
1964 // clang-format on
1965