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