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