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