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