xref: /linux/fs/ntfs3/fsntfs.c (revision e483783c)
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/blkdev.h>
9 #include <linux/buffer_head.h>
10 #include <linux/fs.h>
11 #include <linux/kernel.h>
12 
13 #include "debug.h"
14 #include "ntfs.h"
15 #include "ntfs_fs.h"
16 
17 // clang-format off
18 const struct cpu_str NAME_MFT = {
19 	4, 0, { '$', 'M', 'F', 'T' },
20 };
21 const struct cpu_str NAME_MIRROR = {
22 	8, 0, { '$', 'M', 'F', 'T', 'M', 'i', 'r', 'r' },
23 };
24 const struct cpu_str NAME_LOGFILE = {
25 	8, 0, { '$', 'L', 'o', 'g', 'F', 'i', 'l', 'e' },
26 };
27 const struct cpu_str NAME_VOLUME = {
28 	7, 0, { '$', 'V', 'o', 'l', 'u', 'm', 'e' },
29 };
30 const struct cpu_str NAME_ATTRDEF = {
31 	8, 0, { '$', 'A', 't', 't', 'r', 'D', 'e', 'f' },
32 };
33 const struct cpu_str NAME_ROOT = {
34 	1, 0, { '.' },
35 };
36 const struct cpu_str NAME_BITMAP = {
37 	7, 0, { '$', 'B', 'i', 't', 'm', 'a', 'p' },
38 };
39 const struct cpu_str NAME_BOOT = {
40 	5, 0, { '$', 'B', 'o', 'o', 't' },
41 };
42 const struct cpu_str NAME_BADCLUS = {
43 	8, 0, { '$', 'B', 'a', 'd', 'C', 'l', 'u', 's' },
44 };
45 const struct cpu_str NAME_QUOTA = {
46 	6, 0, { '$', 'Q', 'u', 'o', 't', 'a' },
47 };
48 const struct cpu_str NAME_SECURE = {
49 	7, 0, { '$', 'S', 'e', 'c', 'u', 'r', 'e' },
50 };
51 const struct cpu_str NAME_UPCASE = {
52 	7, 0, { '$', 'U', 'p', 'C', 'a', 's', 'e' },
53 };
54 const struct cpu_str NAME_EXTEND = {
55 	7, 0, { '$', 'E', 'x', 't', 'e', 'n', 'd' },
56 };
57 const struct cpu_str NAME_OBJID = {
58 	6, 0, { '$', 'O', 'b', 'j', 'I', 'd' },
59 };
60 const struct cpu_str NAME_REPARSE = {
61 	8, 0, { '$', 'R', 'e', 'p', 'a', 'r', 's', 'e' },
62 };
63 const struct cpu_str NAME_USNJRNL = {
64 	8, 0, { '$', 'U', 's', 'n', 'J', 'r', 'n', 'l' },
65 };
66 const __le16 BAD_NAME[4] = {
67 	cpu_to_le16('$'), cpu_to_le16('B'), cpu_to_le16('a'), cpu_to_le16('d'),
68 };
69 const __le16 I30_NAME[4] = {
70 	cpu_to_le16('$'), cpu_to_le16('I'), cpu_to_le16('3'), cpu_to_le16('0'),
71 };
72 const __le16 SII_NAME[4] = {
73 	cpu_to_le16('$'), cpu_to_le16('S'), cpu_to_le16('I'), cpu_to_le16('I'),
74 };
75 const __le16 SDH_NAME[4] = {
76 	cpu_to_le16('$'), cpu_to_le16('S'), cpu_to_le16('D'), cpu_to_le16('H'),
77 };
78 const __le16 SDS_NAME[4] = {
79 	cpu_to_le16('$'), cpu_to_le16('S'), cpu_to_le16('D'), cpu_to_le16('S'),
80 };
81 const __le16 SO_NAME[2] = {
82 	cpu_to_le16('$'), cpu_to_le16('O'),
83 };
84 const __le16 SQ_NAME[2] = {
85 	cpu_to_le16('$'), cpu_to_le16('Q'),
86 };
87 const __le16 SR_NAME[2] = {
88 	cpu_to_le16('$'), cpu_to_le16('R'),
89 };
90 
91 #ifdef CONFIG_NTFS3_LZX_XPRESS
92 const __le16 WOF_NAME[17] = {
93 	cpu_to_le16('W'), cpu_to_le16('o'), cpu_to_le16('f'), cpu_to_le16('C'),
94 	cpu_to_le16('o'), cpu_to_le16('m'), cpu_to_le16('p'), cpu_to_le16('r'),
95 	cpu_to_le16('e'), cpu_to_le16('s'), cpu_to_le16('s'), cpu_to_le16('e'),
96 	cpu_to_le16('d'), cpu_to_le16('D'), cpu_to_le16('a'), cpu_to_le16('t'),
97 	cpu_to_le16('a'),
98 };
99 #endif
100 
101 static const __le16 CON_NAME[3] = {
102 	cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('N'),
103 };
104 
105 static const __le16 NUL_NAME[3] = {
106 	cpu_to_le16('N'), cpu_to_le16('U'), cpu_to_le16('L'),
107 };
108 
109 static const __le16 AUX_NAME[3] = {
110 	cpu_to_le16('A'), cpu_to_le16('U'), cpu_to_le16('X'),
111 };
112 
113 static const __le16 PRN_NAME[3] = {
114 	cpu_to_le16('P'), cpu_to_le16('R'), cpu_to_le16('N'),
115 };
116 
117 static const __le16 COM_NAME[3] = {
118 	cpu_to_le16('C'), cpu_to_le16('O'), cpu_to_le16('M'),
119 };
120 
121 static const __le16 LPT_NAME[3] = {
122 	cpu_to_le16('L'), cpu_to_le16('P'), cpu_to_le16('T'),
123 };
124 
125 // clang-format on
126 
127 /*
128  * ntfs_fix_pre_write - Insert fixups into @rhdr before writing to disk.
129  */
130 bool ntfs_fix_pre_write(struct NTFS_RECORD_HEADER *rhdr, size_t bytes)
131 {
132 	u16 *fixup, *ptr;
133 	u16 sample;
134 	u16 fo = le16_to_cpu(rhdr->fix_off);
135 	u16 fn = le16_to_cpu(rhdr->fix_num);
136 
137 	if ((fo & 1) || fo + fn * sizeof(short) > SECTOR_SIZE || !fn-- ||
138 	    fn * SECTOR_SIZE > bytes) {
139 		return false;
140 	}
141 
142 	/* Get fixup pointer. */
143 	fixup = Add2Ptr(rhdr, fo);
144 
145 	if (*fixup >= 0x7FFF)
146 		*fixup = 1;
147 	else
148 		*fixup += 1;
149 
150 	sample = *fixup;
151 
152 	ptr = Add2Ptr(rhdr, SECTOR_SIZE - sizeof(short));
153 
154 	while (fn--) {
155 		*++fixup = *ptr;
156 		*ptr = sample;
157 		ptr += SECTOR_SIZE / sizeof(short);
158 	}
159 	return true;
160 }
161 
162 /*
163  * ntfs_fix_post_read - Remove fixups after reading from disk.
164  *
165  * Return: < 0 if error, 0 if ok, 1 if need to update fixups.
166  */
167 int ntfs_fix_post_read(struct NTFS_RECORD_HEADER *rhdr, size_t bytes,
168 		       bool simple)
169 {
170 	int ret;
171 	u16 *fixup, *ptr;
172 	u16 sample, fo, fn;
173 
174 	fo = le16_to_cpu(rhdr->fix_off);
175 	fn = simple ? ((bytes >> SECTOR_SHIFT) + 1)
176 		    : le16_to_cpu(rhdr->fix_num);
177 
178 	/* Check errors. */
179 	if ((fo & 1) || fo + fn * sizeof(short) > SECTOR_SIZE || !fn-- ||
180 	    fn * SECTOR_SIZE > bytes) {
181 		return -EINVAL; /* Native chkntfs returns ok! */
182 	}
183 
184 	/* Get fixup pointer. */
185 	fixup = Add2Ptr(rhdr, fo);
186 	sample = *fixup;
187 	ptr = Add2Ptr(rhdr, SECTOR_SIZE - sizeof(short));
188 	ret = 0;
189 
190 	while (fn--) {
191 		/* Test current word. */
192 		if (*ptr != sample) {
193 			/* Fixup does not match! Is it serious error? */
194 			ret = -E_NTFS_FIXUP;
195 		}
196 
197 		/* Replace fixup. */
198 		*ptr = *++fixup;
199 		ptr += SECTOR_SIZE / sizeof(short);
200 	}
201 
202 	return ret;
203 }
204 
205 /*
206  * ntfs_extend_init - Load $Extend file.
207  */
208 int ntfs_extend_init(struct ntfs_sb_info *sbi)
209 {
210 	int err;
211 	struct super_block *sb = sbi->sb;
212 	struct inode *inode, *inode2;
213 	struct MFT_REF ref;
214 
215 	if (sbi->volume.major_ver < 3) {
216 		ntfs_notice(sb, "Skip $Extend 'cause NTFS version");
217 		return 0;
218 	}
219 
220 	ref.low = cpu_to_le32(MFT_REC_EXTEND);
221 	ref.high = 0;
222 	ref.seq = cpu_to_le16(MFT_REC_EXTEND);
223 	inode = ntfs_iget5(sb, &ref, &NAME_EXTEND);
224 	if (IS_ERR(inode)) {
225 		err = PTR_ERR(inode);
226 		ntfs_err(sb, "Failed to load $Extend.");
227 		inode = NULL;
228 		goto out;
229 	}
230 
231 	/* If ntfs_iget5() reads from disk it never returns bad inode. */
232 	if (!S_ISDIR(inode->i_mode)) {
233 		err = -EINVAL;
234 		goto out;
235 	}
236 
237 	/* Try to find $ObjId */
238 	inode2 = dir_search_u(inode, &NAME_OBJID, NULL);
239 	if (inode2 && !IS_ERR(inode2)) {
240 		if (is_bad_inode(inode2)) {
241 			iput(inode2);
242 		} else {
243 			sbi->objid.ni = ntfs_i(inode2);
244 			sbi->objid_no = inode2->i_ino;
245 		}
246 	}
247 
248 	/* Try to find $Quota */
249 	inode2 = dir_search_u(inode, &NAME_QUOTA, NULL);
250 	if (inode2 && !IS_ERR(inode2)) {
251 		sbi->quota_no = inode2->i_ino;
252 		iput(inode2);
253 	}
254 
255 	/* Try to find $Reparse */
256 	inode2 = dir_search_u(inode, &NAME_REPARSE, NULL);
257 	if (inode2 && !IS_ERR(inode2)) {
258 		sbi->reparse.ni = ntfs_i(inode2);
259 		sbi->reparse_no = inode2->i_ino;
260 	}
261 
262 	/* Try to find $UsnJrnl */
263 	inode2 = dir_search_u(inode, &NAME_USNJRNL, NULL);
264 	if (inode2 && !IS_ERR(inode2)) {
265 		sbi->usn_jrnl_no = inode2->i_ino;
266 		iput(inode2);
267 	}
268 
269 	err = 0;
270 out:
271 	iput(inode);
272 	return err;
273 }
274 
275 int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi)
276 {
277 	int err = 0;
278 	struct super_block *sb = sbi->sb;
279 	bool initialized = false;
280 	struct MFT_REF ref;
281 	struct inode *inode;
282 
283 	/* Check for 4GB. */
284 	if (ni->vfs_inode.i_size >= 0x100000000ull) {
285 		ntfs_err(sb, "\x24LogFile is too big");
286 		err = -EINVAL;
287 		goto out;
288 	}
289 
290 	sbi->flags |= NTFS_FLAGS_LOG_REPLAYING;
291 
292 	ref.low = cpu_to_le32(MFT_REC_MFT);
293 	ref.high = 0;
294 	ref.seq = cpu_to_le16(1);
295 
296 	inode = ntfs_iget5(sb, &ref, NULL);
297 
298 	if (IS_ERR(inode))
299 		inode = NULL;
300 
301 	if (!inode) {
302 		/* Try to use MFT copy. */
303 		u64 t64 = sbi->mft.lbo;
304 
305 		sbi->mft.lbo = sbi->mft.lbo2;
306 		inode = ntfs_iget5(sb, &ref, NULL);
307 		sbi->mft.lbo = t64;
308 		if (IS_ERR(inode))
309 			inode = NULL;
310 	}
311 
312 	if (!inode) {
313 		err = -EINVAL;
314 		ntfs_err(sb, "Failed to load $MFT.");
315 		goto out;
316 	}
317 
318 	sbi->mft.ni = ntfs_i(inode);
319 
320 	/* LogFile should not contains attribute list. */
321 	err = ni_load_all_mi(sbi->mft.ni);
322 	if (!err)
323 		err = log_replay(ni, &initialized);
324 
325 	iput(inode);
326 	sbi->mft.ni = NULL;
327 
328 	sync_blockdev(sb->s_bdev);
329 	invalidate_bdev(sb->s_bdev);
330 
331 	if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
332 		err = 0;
333 		goto out;
334 	}
335 
336 	if (sb_rdonly(sb) || !initialized)
337 		goto out;
338 
339 	/* Fill LogFile by '-1' if it is initialized. */
340 	err = ntfs_bio_fill_1(sbi, &ni->file.run);
341 
342 out:
343 	sbi->flags &= ~NTFS_FLAGS_LOG_REPLAYING;
344 
345 	return err;
346 }
347 
348 /*
349  * ntfs_look_for_free_space - Look for a free space in bitmap.
350  */
351 int ntfs_look_for_free_space(struct ntfs_sb_info *sbi, CLST lcn, CLST len,
352 			     CLST *new_lcn, CLST *new_len,
353 			     enum ALLOCATE_OPT opt)
354 {
355 	int err;
356 	CLST alen;
357 	struct super_block *sb = sbi->sb;
358 	size_t alcn, zlen, zeroes, zlcn, zlen2, ztrim, new_zlen;
359 	struct wnd_bitmap *wnd = &sbi->used.bitmap;
360 
361 	down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
362 	if (opt & ALLOCATE_MFT) {
363 		zlen = wnd_zone_len(wnd);
364 
365 		if (!zlen) {
366 			err = ntfs_refresh_zone(sbi);
367 			if (err)
368 				goto up_write;
369 
370 			zlen = wnd_zone_len(wnd);
371 		}
372 
373 		if (!zlen) {
374 			ntfs_err(sbi->sb, "no free space to extend mft");
375 			err = -ENOSPC;
376 			goto up_write;
377 		}
378 
379 		lcn = wnd_zone_bit(wnd);
380 		alen = min_t(CLST, len, zlen);
381 
382 		wnd_zone_set(wnd, lcn + alen, zlen - alen);
383 
384 		err = wnd_set_used(wnd, lcn, alen);
385 		if (err)
386 			goto up_write;
387 
388 		alcn = lcn;
389 		goto space_found;
390 	}
391 	/*
392 	 * 'Cause cluster 0 is always used this value means that we should use
393 	 * cached value of 'next_free_lcn' to improve performance.
394 	 */
395 	if (!lcn)
396 		lcn = sbi->used.next_free_lcn;
397 
398 	if (lcn >= wnd->nbits)
399 		lcn = 0;
400 
401 	alen = wnd_find(wnd, len, lcn, BITMAP_FIND_MARK_AS_USED, &alcn);
402 	if (alen)
403 		goto space_found;
404 
405 	/* Try to use clusters from MftZone. */
406 	zlen = wnd_zone_len(wnd);
407 	zeroes = wnd_zeroes(wnd);
408 
409 	/* Check too big request */
410 	if (len > zeroes + zlen || zlen <= NTFS_MIN_MFT_ZONE) {
411 		err = -ENOSPC;
412 		goto up_write;
413 	}
414 
415 	/* How many clusters to cat from zone. */
416 	zlcn = wnd_zone_bit(wnd);
417 	zlen2 = zlen >> 1;
418 	ztrim = clamp_val(len, zlen2, zlen);
419 	new_zlen = max_t(size_t, zlen - ztrim, NTFS_MIN_MFT_ZONE);
420 
421 	wnd_zone_set(wnd, zlcn, new_zlen);
422 
423 	/* Allocate continues clusters. */
424 	alen = wnd_find(wnd, len, 0,
425 			BITMAP_FIND_MARK_AS_USED | BITMAP_FIND_FULL, &alcn);
426 	if (!alen) {
427 		err = -ENOSPC;
428 		goto up_write;
429 	}
430 
431 space_found:
432 	err = 0;
433 	*new_len = alen;
434 	*new_lcn = alcn;
435 
436 	ntfs_unmap_meta(sb, alcn, alen);
437 
438 	/* Set hint for next requests. */
439 	if (!(opt & ALLOCATE_MFT))
440 		sbi->used.next_free_lcn = alcn + alen;
441 up_write:
442 	up_write(&wnd->rw_lock);
443 	return err;
444 }
445 
446 /*
447  * ntfs_check_for_free_space
448  *
449  * Check if it is possible to allocate 'clen' clusters and 'mlen' Mft records
450  */
451 bool ntfs_check_for_free_space(struct ntfs_sb_info *sbi, CLST clen, CLST mlen)
452 {
453 	size_t free, zlen, avail;
454 	struct wnd_bitmap *wnd;
455 
456 	wnd = &sbi->used.bitmap;
457 	down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
458 	free = wnd_zeroes(wnd);
459 	zlen = min_t(size_t, NTFS_MIN_MFT_ZONE, wnd_zone_len(wnd));
460 	up_read(&wnd->rw_lock);
461 
462 	if (free < zlen + clen)
463 		return false;
464 
465 	avail = free - (zlen + clen);
466 
467 	wnd = &sbi->mft.bitmap;
468 	down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT);
469 	free = wnd_zeroes(wnd);
470 	zlen = wnd_zone_len(wnd);
471 	up_read(&wnd->rw_lock);
472 
473 	if (free >= zlen + mlen)
474 		return true;
475 
476 	return avail >= bytes_to_cluster(sbi, mlen << sbi->record_bits);
477 }
478 
479 /*
480  * ntfs_extend_mft - Allocate additional MFT records.
481  *
482  * sbi->mft.bitmap is locked for write.
483  *
484  * NOTE: recursive:
485  *	ntfs_look_free_mft ->
486  *	ntfs_extend_mft ->
487  *	attr_set_size ->
488  *	ni_insert_nonresident ->
489  *	ni_insert_attr ->
490  *	ni_ins_attr_ext ->
491  *	ntfs_look_free_mft ->
492  *	ntfs_extend_mft
493  *
494  * To avoid recursive always allocate space for two new MFT records
495  * see attrib.c: "at least two MFT to avoid recursive loop".
496  */
497 static int ntfs_extend_mft(struct ntfs_sb_info *sbi)
498 {
499 	int err;
500 	struct ntfs_inode *ni = sbi->mft.ni;
501 	size_t new_mft_total;
502 	u64 new_mft_bytes, new_bitmap_bytes;
503 	struct ATTRIB *attr;
504 	struct wnd_bitmap *wnd = &sbi->mft.bitmap;
505 
506 	new_mft_total = ALIGN(wnd->nbits + NTFS_MFT_INCREASE_STEP, 128);
507 	new_mft_bytes = (u64)new_mft_total << sbi->record_bits;
508 
509 	/* Step 1: Resize $MFT::DATA. */
510 	down_write(&ni->file.run_lock);
511 	err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
512 			    new_mft_bytes, NULL, false, &attr);
513 
514 	if (err) {
515 		up_write(&ni->file.run_lock);
516 		goto out;
517 	}
518 
519 	attr->nres.valid_size = attr->nres.data_size;
520 	new_mft_total = le64_to_cpu(attr->nres.alloc_size) >> sbi->record_bits;
521 	ni->mi.dirty = true;
522 
523 	/* Step 2: Resize $MFT::BITMAP. */
524 	new_bitmap_bytes = bitmap_size(new_mft_total);
525 
526 	err = attr_set_size(ni, ATTR_BITMAP, NULL, 0, &sbi->mft.bitmap.run,
527 			    new_bitmap_bytes, &new_bitmap_bytes, true, NULL);
528 
529 	/* Refresh MFT Zone if necessary. */
530 	down_write_nested(&sbi->used.bitmap.rw_lock, BITMAP_MUTEX_CLUSTERS);
531 
532 	ntfs_refresh_zone(sbi);
533 
534 	up_write(&sbi->used.bitmap.rw_lock);
535 	up_write(&ni->file.run_lock);
536 
537 	if (err)
538 		goto out;
539 
540 	err = wnd_extend(wnd, new_mft_total);
541 
542 	if (err)
543 		goto out;
544 
545 	ntfs_clear_mft_tail(sbi, sbi->mft.used, new_mft_total);
546 
547 	err = _ni_write_inode(&ni->vfs_inode, 0);
548 out:
549 	return err;
550 }
551 
552 /*
553  * ntfs_look_free_mft - Look for a free MFT record.
554  */
555 int ntfs_look_free_mft(struct ntfs_sb_info *sbi, CLST *rno, bool mft,
556 		       struct ntfs_inode *ni, struct mft_inode **mi)
557 {
558 	int err = 0;
559 	size_t zbit, zlen, from, to, fr;
560 	size_t mft_total;
561 	struct MFT_REF ref;
562 	struct super_block *sb = sbi->sb;
563 	struct wnd_bitmap *wnd = &sbi->mft.bitmap;
564 	u32 ir;
565 
566 	static_assert(sizeof(sbi->mft.reserved_bitmap) * 8 >=
567 		      MFT_REC_FREE - MFT_REC_RESERVED);
568 
569 	if (!mft)
570 		down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT);
571 
572 	zlen = wnd_zone_len(wnd);
573 
574 	/* Always reserve space for MFT. */
575 	if (zlen) {
576 		if (mft) {
577 			zbit = wnd_zone_bit(wnd);
578 			*rno = zbit;
579 			wnd_zone_set(wnd, zbit + 1, zlen - 1);
580 		}
581 		goto found;
582 	}
583 
584 	/* No MFT zone. Find the nearest to '0' free MFT. */
585 	if (!wnd_find(wnd, 1, MFT_REC_FREE, 0, &zbit)) {
586 		/* Resize MFT */
587 		mft_total = wnd->nbits;
588 
589 		err = ntfs_extend_mft(sbi);
590 		if (!err) {
591 			zbit = mft_total;
592 			goto reserve_mft;
593 		}
594 
595 		if (!mft || MFT_REC_FREE == sbi->mft.next_reserved)
596 			goto out;
597 
598 		err = 0;
599 
600 		/*
601 		 * Look for free record reserved area [11-16) ==
602 		 * [MFT_REC_RESERVED, MFT_REC_FREE ) MFT bitmap always
603 		 * marks it as used.
604 		 */
605 		if (!sbi->mft.reserved_bitmap) {
606 			/* Once per session create internal bitmap for 5 bits. */
607 			sbi->mft.reserved_bitmap = 0xFF;
608 
609 			ref.high = 0;
610 			for (ir = MFT_REC_RESERVED; ir < MFT_REC_FREE; ir++) {
611 				struct inode *i;
612 				struct ntfs_inode *ni;
613 				struct MFT_REC *mrec;
614 
615 				ref.low = cpu_to_le32(ir);
616 				ref.seq = cpu_to_le16(ir);
617 
618 				i = ntfs_iget5(sb, &ref, NULL);
619 				if (IS_ERR(i)) {
620 next:
621 					ntfs_notice(
622 						sb,
623 						"Invalid reserved record %x",
624 						ref.low);
625 					continue;
626 				}
627 				if (is_bad_inode(i)) {
628 					iput(i);
629 					goto next;
630 				}
631 
632 				ni = ntfs_i(i);
633 
634 				mrec = ni->mi.mrec;
635 
636 				if (!is_rec_base(mrec))
637 					goto next;
638 
639 				if (mrec->hard_links)
640 					goto next;
641 
642 				if (!ni_std(ni))
643 					goto next;
644 
645 				if (ni_find_attr(ni, NULL, NULL, ATTR_NAME,
646 						 NULL, 0, NULL, NULL))
647 					goto next;
648 
649 				__clear_bit(ir - MFT_REC_RESERVED,
650 					    &sbi->mft.reserved_bitmap);
651 			}
652 		}
653 
654 		/* Scan 5 bits for zero. Bit 0 == MFT_REC_RESERVED */
655 		zbit = find_next_zero_bit(&sbi->mft.reserved_bitmap,
656 					  MFT_REC_FREE, MFT_REC_RESERVED);
657 		if (zbit >= MFT_REC_FREE) {
658 			sbi->mft.next_reserved = MFT_REC_FREE;
659 			goto out;
660 		}
661 
662 		zlen = 1;
663 		sbi->mft.next_reserved = zbit;
664 	} else {
665 reserve_mft:
666 		zlen = zbit == MFT_REC_FREE ? (MFT_REC_USER - MFT_REC_FREE) : 4;
667 		if (zbit + zlen > wnd->nbits)
668 			zlen = wnd->nbits - zbit;
669 
670 		while (zlen > 1 && !wnd_is_free(wnd, zbit, zlen))
671 			zlen -= 1;
672 
673 		/* [zbit, zbit + zlen) will be used for MFT itself. */
674 		from = sbi->mft.used;
675 		if (from < zbit)
676 			from = zbit;
677 		to = zbit + zlen;
678 		if (from < to) {
679 			ntfs_clear_mft_tail(sbi, from, to);
680 			sbi->mft.used = to;
681 		}
682 	}
683 
684 	if (mft) {
685 		*rno = zbit;
686 		zbit += 1;
687 		zlen -= 1;
688 	}
689 
690 	wnd_zone_set(wnd, zbit, zlen);
691 
692 found:
693 	if (!mft) {
694 		/* The request to get record for general purpose. */
695 		if (sbi->mft.next_free < MFT_REC_USER)
696 			sbi->mft.next_free = MFT_REC_USER;
697 
698 		for (;;) {
699 			if (sbi->mft.next_free >= sbi->mft.bitmap.nbits) {
700 			} else if (!wnd_find(wnd, 1, MFT_REC_USER, 0, &fr)) {
701 				sbi->mft.next_free = sbi->mft.bitmap.nbits;
702 			} else {
703 				*rno = fr;
704 				sbi->mft.next_free = *rno + 1;
705 				break;
706 			}
707 
708 			err = ntfs_extend_mft(sbi);
709 			if (err)
710 				goto out;
711 		}
712 	}
713 
714 	if (ni && !ni_add_subrecord(ni, *rno, mi)) {
715 		err = -ENOMEM;
716 		goto out;
717 	}
718 
719 	/* We have found a record that are not reserved for next MFT. */
720 	if (*rno >= MFT_REC_FREE)
721 		wnd_set_used(wnd, *rno, 1);
722 	else if (*rno >= MFT_REC_RESERVED && sbi->mft.reserved_bitmap_inited)
723 		__set_bit(*rno - MFT_REC_RESERVED, &sbi->mft.reserved_bitmap);
724 
725 out:
726 	if (!mft)
727 		up_write(&wnd->rw_lock);
728 
729 	return err;
730 }
731 
732 /*
733  * ntfs_mark_rec_free - Mark record as free.
734  * is_mft - true if we are changing MFT
735  */
736 void ntfs_mark_rec_free(struct ntfs_sb_info *sbi, CLST rno, bool is_mft)
737 {
738 	struct wnd_bitmap *wnd = &sbi->mft.bitmap;
739 
740 	if (!is_mft)
741 		down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT);
742 	if (rno >= wnd->nbits)
743 		goto out;
744 
745 	if (rno >= MFT_REC_FREE) {
746 		if (!wnd_is_used(wnd, rno, 1))
747 			ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
748 		else
749 			wnd_set_free(wnd, rno, 1);
750 	} else if (rno >= MFT_REC_RESERVED && sbi->mft.reserved_bitmap_inited) {
751 		__clear_bit(rno - MFT_REC_RESERVED, &sbi->mft.reserved_bitmap);
752 	}
753 
754 	if (rno < wnd_zone_bit(wnd))
755 		wnd_zone_set(wnd, rno, 1);
756 	else if (rno < sbi->mft.next_free && rno >= MFT_REC_USER)
757 		sbi->mft.next_free = rno;
758 
759 out:
760 	if (!is_mft)
761 		up_write(&wnd->rw_lock);
762 }
763 
764 /*
765  * ntfs_clear_mft_tail - Format empty records [from, to).
766  *
767  * sbi->mft.bitmap is locked for write.
768  */
769 int ntfs_clear_mft_tail(struct ntfs_sb_info *sbi, size_t from, size_t to)
770 {
771 	int err;
772 	u32 rs;
773 	u64 vbo;
774 	struct runs_tree *run;
775 	struct ntfs_inode *ni;
776 
777 	if (from >= to)
778 		return 0;
779 
780 	rs = sbi->record_size;
781 	ni = sbi->mft.ni;
782 	run = &ni->file.run;
783 
784 	down_read(&ni->file.run_lock);
785 	vbo = (u64)from * rs;
786 	for (; from < to; from++, vbo += rs) {
787 		struct ntfs_buffers nb;
788 
789 		err = ntfs_get_bh(sbi, run, vbo, rs, &nb);
790 		if (err)
791 			goto out;
792 
793 		err = ntfs_write_bh(sbi, &sbi->new_rec->rhdr, &nb, 0);
794 		nb_put(&nb);
795 		if (err)
796 			goto out;
797 	}
798 
799 out:
800 	sbi->mft.used = from;
801 	up_read(&ni->file.run_lock);
802 	return err;
803 }
804 
805 /*
806  * ntfs_refresh_zone - Refresh MFT zone.
807  *
808  * sbi->used.bitmap is locked for rw.
809  * sbi->mft.bitmap is locked for write.
810  * sbi->mft.ni->file.run_lock for write.
811  */
812 int ntfs_refresh_zone(struct ntfs_sb_info *sbi)
813 {
814 	CLST lcn, vcn, len;
815 	size_t lcn_s, zlen;
816 	struct wnd_bitmap *wnd = &sbi->used.bitmap;
817 	struct ntfs_inode *ni = sbi->mft.ni;
818 
819 	/* Do not change anything unless we have non empty MFT zone. */
820 	if (wnd_zone_len(wnd))
821 		return 0;
822 
823 	vcn = bytes_to_cluster(sbi,
824 			       (u64)sbi->mft.bitmap.nbits << sbi->record_bits);
825 
826 	if (!run_lookup_entry(&ni->file.run, vcn - 1, &lcn, &len, NULL))
827 		lcn = SPARSE_LCN;
828 
829 	/* We should always find Last Lcn for MFT. */
830 	if (lcn == SPARSE_LCN)
831 		return -EINVAL;
832 
833 	lcn_s = lcn + 1;
834 
835 	/* Try to allocate clusters after last MFT run. */
836 	zlen = wnd_find(wnd, sbi->zone_max, lcn_s, 0, &lcn_s);
837 	wnd_zone_set(wnd, lcn_s, zlen);
838 
839 	return 0;
840 }
841 
842 /*
843  * ntfs_update_mftmirr - Update $MFTMirr data.
844  */
845 void ntfs_update_mftmirr(struct ntfs_sb_info *sbi, int wait)
846 {
847 	int err;
848 	struct super_block *sb = sbi->sb;
849 	u32 blocksize, bytes;
850 	sector_t block1, block2;
851 
852 	if (!sb)
853 		return;
854 
855 	blocksize = sb->s_blocksize;
856 
857 	if (!(sbi->flags & NTFS_FLAGS_MFTMIRR))
858 		return;
859 
860 	bytes = sbi->mft.recs_mirr << sbi->record_bits;
861 	block1 = sbi->mft.lbo >> sb->s_blocksize_bits;
862 	block2 = sbi->mft.lbo2 >> sb->s_blocksize_bits;
863 
864 	for (; bytes >= blocksize; bytes -= blocksize) {
865 		struct buffer_head *bh1, *bh2;
866 
867 		bh1 = sb_bread(sb, block1++);
868 		if (!bh1)
869 			return;
870 
871 		bh2 = sb_getblk(sb, block2++);
872 		if (!bh2) {
873 			put_bh(bh1);
874 			return;
875 		}
876 
877 		if (buffer_locked(bh2))
878 			__wait_on_buffer(bh2);
879 
880 		lock_buffer(bh2);
881 		memcpy(bh2->b_data, bh1->b_data, blocksize);
882 		set_buffer_uptodate(bh2);
883 		mark_buffer_dirty(bh2);
884 		unlock_buffer(bh2);
885 
886 		put_bh(bh1);
887 		bh1 = NULL;
888 
889 		err = wait ? sync_dirty_buffer(bh2) : 0;
890 
891 		put_bh(bh2);
892 		if (err)
893 			return;
894 	}
895 
896 	sbi->flags &= ~NTFS_FLAGS_MFTMIRR;
897 }
898 
899 /*
900  * ntfs_bad_inode
901  *
902  * Marks inode as bad and marks fs as 'dirty'
903  */
904 void ntfs_bad_inode(struct inode *inode, const char *hint)
905 {
906 	struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
907 
908 	ntfs_inode_err(inode, "%s", hint);
909 	make_bad_inode(inode);
910 	ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
911 }
912 
913 /*
914  * ntfs_set_state
915  *
916  * Mount: ntfs_set_state(NTFS_DIRTY_DIRTY)
917  * Umount: ntfs_set_state(NTFS_DIRTY_CLEAR)
918  * NTFS error: ntfs_set_state(NTFS_DIRTY_ERROR)
919  */
920 int ntfs_set_state(struct ntfs_sb_info *sbi, enum NTFS_DIRTY_FLAGS dirty)
921 {
922 	int err;
923 	struct ATTRIB *attr;
924 	struct VOLUME_INFO *info;
925 	struct mft_inode *mi;
926 	struct ntfs_inode *ni;
927 	__le16 info_flags;
928 
929 	/*
930 	 * Do not change state if fs was real_dirty.
931 	 * Do not change state if fs already dirty(clear).
932 	 * Do not change any thing if mounted read only.
933 	 */
934 	if (sbi->volume.real_dirty || sb_rdonly(sbi->sb))
935 		return 0;
936 
937 	/* Check cached value. */
938 	if ((dirty == NTFS_DIRTY_CLEAR ? 0 : VOLUME_FLAG_DIRTY) ==
939 	    (sbi->volume.flags & VOLUME_FLAG_DIRTY))
940 		return 0;
941 
942 	ni = sbi->volume.ni;
943 	if (!ni)
944 		return -EINVAL;
945 
946 	mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_DIRTY);
947 
948 	attr = ni_find_attr(ni, NULL, NULL, ATTR_VOL_INFO, NULL, 0, NULL, &mi);
949 	if (!attr) {
950 		err = -EINVAL;
951 		goto out;
952 	}
953 
954 	info = resident_data_ex(attr, SIZEOF_ATTRIBUTE_VOLUME_INFO);
955 	if (!info) {
956 		err = -EINVAL;
957 		goto out;
958 	}
959 
960 	info_flags = info->flags;
961 
962 	switch (dirty) {
963 	case NTFS_DIRTY_ERROR:
964 		ntfs_notice(sbi->sb, "Mark volume as dirty due to NTFS errors");
965 		sbi->volume.real_dirty = true;
966 		fallthrough;
967 	case NTFS_DIRTY_DIRTY:
968 		info->flags |= VOLUME_FLAG_DIRTY;
969 		break;
970 	case NTFS_DIRTY_CLEAR:
971 		info->flags &= ~VOLUME_FLAG_DIRTY;
972 		break;
973 	}
974 	/* Cache current volume flags. */
975 	if (info_flags != info->flags) {
976 		sbi->volume.flags = info->flags;
977 		mi->dirty = true;
978 	}
979 	err = 0;
980 
981 out:
982 	ni_unlock(ni);
983 	if (err)
984 		return err;
985 
986 	mark_inode_dirty(&ni->vfs_inode);
987 	/* verify(!ntfs_update_mftmirr()); */
988 
989 	/*
990 	 * If we used wait=1, sync_inode_metadata waits for the io for the
991 	 * inode to finish. It hangs when media is removed.
992 	 * So wait=0 is sent down to sync_inode_metadata
993 	 * and filemap_fdatawrite is used for the data blocks.
994 	 */
995 	err = sync_inode_metadata(&ni->vfs_inode, 0);
996 	if (!err)
997 		err = filemap_fdatawrite(ni->vfs_inode.i_mapping);
998 
999 	return err;
1000 }
1001 
1002 /*
1003  * security_hash - Calculates a hash of security descriptor.
1004  */
1005 static inline __le32 security_hash(const void *sd, size_t bytes)
1006 {
1007 	u32 hash = 0;
1008 	const __le32 *ptr = sd;
1009 
1010 	bytes >>= 2;
1011 	while (bytes--)
1012 		hash = ((hash >> 0x1D) | (hash << 3)) + le32_to_cpu(*ptr++);
1013 	return cpu_to_le32(hash);
1014 }
1015 
1016 int ntfs_sb_read(struct super_block *sb, u64 lbo, size_t bytes, void *buffer)
1017 {
1018 	struct block_device *bdev = sb->s_bdev;
1019 	u32 blocksize = sb->s_blocksize;
1020 	u64 block = lbo >> sb->s_blocksize_bits;
1021 	u32 off = lbo & (blocksize - 1);
1022 	u32 op = blocksize - off;
1023 
1024 	for (; bytes; block += 1, off = 0, op = blocksize) {
1025 		struct buffer_head *bh = __bread(bdev, block, blocksize);
1026 
1027 		if (!bh)
1028 			return -EIO;
1029 
1030 		if (op > bytes)
1031 			op = bytes;
1032 
1033 		memcpy(buffer, bh->b_data + off, op);
1034 
1035 		put_bh(bh);
1036 
1037 		bytes -= op;
1038 		buffer = Add2Ptr(buffer, op);
1039 	}
1040 
1041 	return 0;
1042 }
1043 
1044 int ntfs_sb_write(struct super_block *sb, u64 lbo, size_t bytes,
1045 		  const void *buf, int wait)
1046 {
1047 	u32 blocksize = sb->s_blocksize;
1048 	struct block_device *bdev = sb->s_bdev;
1049 	sector_t block = lbo >> sb->s_blocksize_bits;
1050 	u32 off = lbo & (blocksize - 1);
1051 	u32 op = blocksize - off;
1052 	struct buffer_head *bh;
1053 
1054 	if (!wait && (sb->s_flags & SB_SYNCHRONOUS))
1055 		wait = 1;
1056 
1057 	for (; bytes; block += 1, off = 0, op = blocksize) {
1058 		if (op > bytes)
1059 			op = bytes;
1060 
1061 		if (op < blocksize) {
1062 			bh = __bread(bdev, block, blocksize);
1063 			if (!bh) {
1064 				ntfs_err(sb, "failed to read block %llx",
1065 					 (u64)block);
1066 				return -EIO;
1067 			}
1068 		} else {
1069 			bh = __getblk(bdev, block, blocksize);
1070 			if (!bh)
1071 				return -ENOMEM;
1072 		}
1073 
1074 		if (buffer_locked(bh))
1075 			__wait_on_buffer(bh);
1076 
1077 		lock_buffer(bh);
1078 		if (buf) {
1079 			memcpy(bh->b_data + off, buf, op);
1080 			buf = Add2Ptr(buf, op);
1081 		} else {
1082 			memset(bh->b_data + off, -1, op);
1083 		}
1084 
1085 		set_buffer_uptodate(bh);
1086 		mark_buffer_dirty(bh);
1087 		unlock_buffer(bh);
1088 
1089 		if (wait) {
1090 			int err = sync_dirty_buffer(bh);
1091 
1092 			if (err) {
1093 				ntfs_err(
1094 					sb,
1095 					"failed to sync buffer at block %llx, error %d",
1096 					(u64)block, err);
1097 				put_bh(bh);
1098 				return err;
1099 			}
1100 		}
1101 
1102 		put_bh(bh);
1103 
1104 		bytes -= op;
1105 	}
1106 	return 0;
1107 }
1108 
1109 int ntfs_sb_write_run(struct ntfs_sb_info *sbi, const struct runs_tree *run,
1110 		      u64 vbo, const void *buf, size_t bytes, int sync)
1111 {
1112 	struct super_block *sb = sbi->sb;
1113 	u8 cluster_bits = sbi->cluster_bits;
1114 	u32 off = vbo & sbi->cluster_mask;
1115 	CLST lcn, clen, vcn = vbo >> cluster_bits, vcn_next;
1116 	u64 lbo, len;
1117 	size_t idx;
1118 
1119 	if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx))
1120 		return -ENOENT;
1121 
1122 	if (lcn == SPARSE_LCN)
1123 		return -EINVAL;
1124 
1125 	lbo = ((u64)lcn << cluster_bits) + off;
1126 	len = ((u64)clen << cluster_bits) - off;
1127 
1128 	for (;;) {
1129 		u32 op = min_t(u64, len, bytes);
1130 		int err = ntfs_sb_write(sb, lbo, op, buf, sync);
1131 
1132 		if (err)
1133 			return err;
1134 
1135 		bytes -= op;
1136 		if (!bytes)
1137 			break;
1138 
1139 		vcn_next = vcn + clen;
1140 		if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
1141 		    vcn != vcn_next)
1142 			return -ENOENT;
1143 
1144 		if (lcn == SPARSE_LCN)
1145 			return -EINVAL;
1146 
1147 		if (buf)
1148 			buf = Add2Ptr(buf, op);
1149 
1150 		lbo = ((u64)lcn << cluster_bits);
1151 		len = ((u64)clen << cluster_bits);
1152 	}
1153 
1154 	return 0;
1155 }
1156 
1157 struct buffer_head *ntfs_bread_run(struct ntfs_sb_info *sbi,
1158 				   const struct runs_tree *run, u64 vbo)
1159 {
1160 	struct super_block *sb = sbi->sb;
1161 	u8 cluster_bits = sbi->cluster_bits;
1162 	CLST lcn;
1163 	u64 lbo;
1164 
1165 	if (!run_lookup_entry(run, vbo >> cluster_bits, &lcn, NULL, NULL))
1166 		return ERR_PTR(-ENOENT);
1167 
1168 	lbo = ((u64)lcn << cluster_bits) + (vbo & sbi->cluster_mask);
1169 
1170 	return ntfs_bread(sb, lbo >> sb->s_blocksize_bits);
1171 }
1172 
1173 int ntfs_read_run_nb(struct ntfs_sb_info *sbi, const struct runs_tree *run,
1174 		     u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb)
1175 {
1176 	int err;
1177 	struct super_block *sb = sbi->sb;
1178 	u32 blocksize = sb->s_blocksize;
1179 	u8 cluster_bits = sbi->cluster_bits;
1180 	u32 off = vbo & sbi->cluster_mask;
1181 	u32 nbh = 0;
1182 	CLST vcn_next, vcn = vbo >> cluster_bits;
1183 	CLST lcn, clen;
1184 	u64 lbo, len;
1185 	size_t idx;
1186 	struct buffer_head *bh;
1187 
1188 	if (!run) {
1189 		/* First reading of $Volume + $MFTMirr + $LogFile goes here. */
1190 		if (vbo > MFT_REC_VOL * sbi->record_size) {
1191 			err = -ENOENT;
1192 			goto out;
1193 		}
1194 
1195 		/* Use absolute boot's 'MFTCluster' to read record. */
1196 		lbo = vbo + sbi->mft.lbo;
1197 		len = sbi->record_size;
1198 	} else if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
1199 		err = -ENOENT;
1200 		goto out;
1201 	} else {
1202 		if (lcn == SPARSE_LCN) {
1203 			err = -EINVAL;
1204 			goto out;
1205 		}
1206 
1207 		lbo = ((u64)lcn << cluster_bits) + off;
1208 		len = ((u64)clen << cluster_bits) - off;
1209 	}
1210 
1211 	off = lbo & (blocksize - 1);
1212 	if (nb) {
1213 		nb->off = off;
1214 		nb->bytes = bytes;
1215 	}
1216 
1217 	for (;;) {
1218 		u32 len32 = len >= bytes ? bytes : len;
1219 		sector_t block = lbo >> sb->s_blocksize_bits;
1220 
1221 		do {
1222 			u32 op = blocksize - off;
1223 
1224 			if (op > len32)
1225 				op = len32;
1226 
1227 			bh = ntfs_bread(sb, block);
1228 			if (!bh) {
1229 				err = -EIO;
1230 				goto out;
1231 			}
1232 
1233 			if (buf) {
1234 				memcpy(buf, bh->b_data + off, op);
1235 				buf = Add2Ptr(buf, op);
1236 			}
1237 
1238 			if (!nb) {
1239 				put_bh(bh);
1240 			} else if (nbh >= ARRAY_SIZE(nb->bh)) {
1241 				err = -EINVAL;
1242 				goto out;
1243 			} else {
1244 				nb->bh[nbh++] = bh;
1245 				nb->nbufs = nbh;
1246 			}
1247 
1248 			bytes -= op;
1249 			if (!bytes)
1250 				return 0;
1251 			len32 -= op;
1252 			block += 1;
1253 			off = 0;
1254 
1255 		} while (len32);
1256 
1257 		vcn_next = vcn + clen;
1258 		if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
1259 		    vcn != vcn_next) {
1260 			err = -ENOENT;
1261 			goto out;
1262 		}
1263 
1264 		if (lcn == SPARSE_LCN) {
1265 			err = -EINVAL;
1266 			goto out;
1267 		}
1268 
1269 		lbo = ((u64)lcn << cluster_bits);
1270 		len = ((u64)clen << cluster_bits);
1271 	}
1272 
1273 out:
1274 	if (!nbh)
1275 		return err;
1276 
1277 	while (nbh) {
1278 		put_bh(nb->bh[--nbh]);
1279 		nb->bh[nbh] = NULL;
1280 	}
1281 
1282 	nb->nbufs = 0;
1283 	return err;
1284 }
1285 
1286 /*
1287  * ntfs_read_bh
1288  *
1289  * Return: < 0 if error, 0 if ok, -E_NTFS_FIXUP if need to update fixups.
1290  */
1291 int ntfs_read_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
1292 		 struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
1293 		 struct ntfs_buffers *nb)
1294 {
1295 	int err = ntfs_read_run_nb(sbi, run, vbo, rhdr, bytes, nb);
1296 
1297 	if (err)
1298 		return err;
1299 	return ntfs_fix_post_read(rhdr, nb->bytes, true);
1300 }
1301 
1302 int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
1303 		u32 bytes, struct ntfs_buffers *nb)
1304 {
1305 	int err = 0;
1306 	struct super_block *sb = sbi->sb;
1307 	u32 blocksize = sb->s_blocksize;
1308 	u8 cluster_bits = sbi->cluster_bits;
1309 	CLST vcn_next, vcn = vbo >> cluster_bits;
1310 	u32 off;
1311 	u32 nbh = 0;
1312 	CLST lcn, clen;
1313 	u64 lbo, len;
1314 	size_t idx;
1315 
1316 	nb->bytes = bytes;
1317 
1318 	if (!run_lookup_entry(run, vcn, &lcn, &clen, &idx)) {
1319 		err = -ENOENT;
1320 		goto out;
1321 	}
1322 
1323 	off = vbo & sbi->cluster_mask;
1324 	lbo = ((u64)lcn << cluster_bits) + off;
1325 	len = ((u64)clen << cluster_bits) - off;
1326 
1327 	nb->off = off = lbo & (blocksize - 1);
1328 
1329 	for (;;) {
1330 		u32 len32 = min_t(u64, len, bytes);
1331 		sector_t block = lbo >> sb->s_blocksize_bits;
1332 
1333 		do {
1334 			u32 op;
1335 			struct buffer_head *bh;
1336 
1337 			if (nbh >= ARRAY_SIZE(nb->bh)) {
1338 				err = -EINVAL;
1339 				goto out;
1340 			}
1341 
1342 			op = blocksize - off;
1343 			if (op > len32)
1344 				op = len32;
1345 
1346 			if (op == blocksize) {
1347 				bh = sb_getblk(sb, block);
1348 				if (!bh) {
1349 					err = -ENOMEM;
1350 					goto out;
1351 				}
1352 				if (buffer_locked(bh))
1353 					__wait_on_buffer(bh);
1354 				set_buffer_uptodate(bh);
1355 			} else {
1356 				bh = ntfs_bread(sb, block);
1357 				if (!bh) {
1358 					err = -EIO;
1359 					goto out;
1360 				}
1361 			}
1362 
1363 			nb->bh[nbh++] = bh;
1364 			bytes -= op;
1365 			if (!bytes) {
1366 				nb->nbufs = nbh;
1367 				return 0;
1368 			}
1369 
1370 			block += 1;
1371 			len32 -= op;
1372 			off = 0;
1373 		} while (len32);
1374 
1375 		vcn_next = vcn + clen;
1376 		if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) ||
1377 		    vcn != vcn_next) {
1378 			err = -ENOENT;
1379 			goto out;
1380 		}
1381 
1382 		lbo = ((u64)lcn << cluster_bits);
1383 		len = ((u64)clen << cluster_bits);
1384 	}
1385 
1386 out:
1387 	while (nbh) {
1388 		put_bh(nb->bh[--nbh]);
1389 		nb->bh[nbh] = NULL;
1390 	}
1391 
1392 	nb->nbufs = 0;
1393 
1394 	return err;
1395 }
1396 
1397 int ntfs_write_bh(struct ntfs_sb_info *sbi, struct NTFS_RECORD_HEADER *rhdr,
1398 		  struct ntfs_buffers *nb, int sync)
1399 {
1400 	int err = 0;
1401 	struct super_block *sb = sbi->sb;
1402 	u32 block_size = sb->s_blocksize;
1403 	u32 bytes = nb->bytes;
1404 	u32 off = nb->off;
1405 	u16 fo = le16_to_cpu(rhdr->fix_off);
1406 	u16 fn = le16_to_cpu(rhdr->fix_num);
1407 	u32 idx;
1408 	__le16 *fixup;
1409 	__le16 sample;
1410 
1411 	if ((fo & 1) || fo + fn * sizeof(short) > SECTOR_SIZE || !fn-- ||
1412 	    fn * SECTOR_SIZE > bytes) {
1413 		return -EINVAL;
1414 	}
1415 
1416 	for (idx = 0; bytes && idx < nb->nbufs; idx += 1, off = 0) {
1417 		u32 op = block_size - off;
1418 		char *bh_data;
1419 		struct buffer_head *bh = nb->bh[idx];
1420 		__le16 *ptr, *end_data;
1421 
1422 		if (op > bytes)
1423 			op = bytes;
1424 
1425 		if (buffer_locked(bh))
1426 			__wait_on_buffer(bh);
1427 
1428 		lock_buffer(bh);
1429 
1430 		bh_data = bh->b_data + off;
1431 		end_data = Add2Ptr(bh_data, op);
1432 		memcpy(bh_data, rhdr, op);
1433 
1434 		if (!idx) {
1435 			u16 t16;
1436 
1437 			fixup = Add2Ptr(bh_data, fo);
1438 			sample = *fixup;
1439 			t16 = le16_to_cpu(sample);
1440 			if (t16 >= 0x7FFF) {
1441 				sample = *fixup = cpu_to_le16(1);
1442 			} else {
1443 				sample = cpu_to_le16(t16 + 1);
1444 				*fixup = sample;
1445 			}
1446 
1447 			*(__le16 *)Add2Ptr(rhdr, fo) = sample;
1448 		}
1449 
1450 		ptr = Add2Ptr(bh_data, SECTOR_SIZE - sizeof(short));
1451 
1452 		do {
1453 			*++fixup = *ptr;
1454 			*ptr = sample;
1455 			ptr += SECTOR_SIZE / sizeof(short);
1456 		} while (ptr < end_data);
1457 
1458 		set_buffer_uptodate(bh);
1459 		mark_buffer_dirty(bh);
1460 		unlock_buffer(bh);
1461 
1462 		if (sync) {
1463 			int err2 = sync_dirty_buffer(bh);
1464 
1465 			if (!err && err2)
1466 				err = err2;
1467 		}
1468 
1469 		bytes -= op;
1470 		rhdr = Add2Ptr(rhdr, op);
1471 	}
1472 
1473 	return err;
1474 }
1475 
1476 /*
1477  * ntfs_bio_pages - Read/write pages from/to disk.
1478  */
1479 int ntfs_bio_pages(struct ntfs_sb_info *sbi, const struct runs_tree *run,
1480 		   struct page **pages, u32 nr_pages, u64 vbo, u32 bytes,
1481 		   enum req_op op)
1482 {
1483 	int err = 0;
1484 	struct bio *new, *bio = NULL;
1485 	struct super_block *sb = sbi->sb;
1486 	struct block_device *bdev = sb->s_bdev;
1487 	struct page *page;
1488 	u8 cluster_bits = sbi->cluster_bits;
1489 	CLST lcn, clen, vcn, vcn_next;
1490 	u32 add, off, page_idx;
1491 	u64 lbo, len;
1492 	size_t run_idx;
1493 	struct blk_plug plug;
1494 
1495 	if (!bytes)
1496 		return 0;
1497 
1498 	blk_start_plug(&plug);
1499 
1500 	/* Align vbo and bytes to be 512 bytes aligned. */
1501 	lbo = (vbo + bytes + 511) & ~511ull;
1502 	vbo = vbo & ~511ull;
1503 	bytes = lbo - vbo;
1504 
1505 	vcn = vbo >> cluster_bits;
1506 	if (!run_lookup_entry(run, vcn, &lcn, &clen, &run_idx)) {
1507 		err = -ENOENT;
1508 		goto out;
1509 	}
1510 	off = vbo & sbi->cluster_mask;
1511 	page_idx = 0;
1512 	page = pages[0];
1513 
1514 	for (;;) {
1515 		lbo = ((u64)lcn << cluster_bits) + off;
1516 		len = ((u64)clen << cluster_bits) - off;
1517 new_bio:
1518 		new = bio_alloc(bdev, nr_pages - page_idx, op, GFP_NOFS);
1519 		if (bio) {
1520 			bio_chain(bio, new);
1521 			submit_bio(bio);
1522 		}
1523 		bio = new;
1524 		bio->bi_iter.bi_sector = lbo >> 9;
1525 
1526 		while (len) {
1527 			off = vbo & (PAGE_SIZE - 1);
1528 			add = off + len > PAGE_SIZE ? (PAGE_SIZE - off) : len;
1529 
1530 			if (bio_add_page(bio, page, add, off) < add)
1531 				goto new_bio;
1532 
1533 			if (bytes <= add)
1534 				goto out;
1535 			bytes -= add;
1536 			vbo += add;
1537 
1538 			if (add + off == PAGE_SIZE) {
1539 				page_idx += 1;
1540 				if (WARN_ON(page_idx >= nr_pages)) {
1541 					err = -EINVAL;
1542 					goto out;
1543 				}
1544 				page = pages[page_idx];
1545 			}
1546 
1547 			if (len <= add)
1548 				break;
1549 			len -= add;
1550 			lbo += add;
1551 		}
1552 
1553 		vcn_next = vcn + clen;
1554 		if (!run_get_entry(run, ++run_idx, &vcn, &lcn, &clen) ||
1555 		    vcn != vcn_next) {
1556 			err = -ENOENT;
1557 			goto out;
1558 		}
1559 		off = 0;
1560 	}
1561 out:
1562 	if (bio) {
1563 		if (!err)
1564 			err = submit_bio_wait(bio);
1565 		bio_put(bio);
1566 	}
1567 	blk_finish_plug(&plug);
1568 
1569 	return err;
1570 }
1571 
1572 /*
1573  * ntfs_bio_fill_1 - Helper for ntfs_loadlog_and_replay().
1574  *
1575  * Fill on-disk logfile range by (-1)
1576  * this means empty logfile.
1577  */
1578 int ntfs_bio_fill_1(struct ntfs_sb_info *sbi, const struct runs_tree *run)
1579 {
1580 	int err = 0;
1581 	struct super_block *sb = sbi->sb;
1582 	struct block_device *bdev = sb->s_bdev;
1583 	u8 cluster_bits = sbi->cluster_bits;
1584 	struct bio *new, *bio = NULL;
1585 	CLST lcn, clen;
1586 	u64 lbo, len;
1587 	size_t run_idx;
1588 	struct page *fill;
1589 	void *kaddr;
1590 	struct blk_plug plug;
1591 
1592 	fill = alloc_page(GFP_KERNEL);
1593 	if (!fill)
1594 		return -ENOMEM;
1595 
1596 	kaddr = kmap_atomic(fill);
1597 	memset(kaddr, -1, PAGE_SIZE);
1598 	kunmap_atomic(kaddr);
1599 	flush_dcache_page(fill);
1600 	lock_page(fill);
1601 
1602 	if (!run_lookup_entry(run, 0, &lcn, &clen, &run_idx)) {
1603 		err = -ENOENT;
1604 		goto out;
1605 	}
1606 
1607 	/*
1608 	 * TODO: Try blkdev_issue_write_same.
1609 	 */
1610 	blk_start_plug(&plug);
1611 	do {
1612 		lbo = (u64)lcn << cluster_bits;
1613 		len = (u64)clen << cluster_bits;
1614 new_bio:
1615 		new = bio_alloc(bdev, BIO_MAX_VECS, REQ_OP_WRITE, GFP_NOFS);
1616 		if (bio) {
1617 			bio_chain(bio, new);
1618 			submit_bio(bio);
1619 		}
1620 		bio = new;
1621 		bio->bi_iter.bi_sector = lbo >> 9;
1622 
1623 		for (;;) {
1624 			u32 add = len > PAGE_SIZE ? PAGE_SIZE : len;
1625 
1626 			if (bio_add_page(bio, fill, add, 0) < add)
1627 				goto new_bio;
1628 
1629 			lbo += add;
1630 			if (len <= add)
1631 				break;
1632 			len -= add;
1633 		}
1634 	} while (run_get_entry(run, ++run_idx, NULL, &lcn, &clen));
1635 
1636 	if (!err)
1637 		err = submit_bio_wait(bio);
1638 	bio_put(bio);
1639 
1640 	blk_finish_plug(&plug);
1641 out:
1642 	unlock_page(fill);
1643 	put_page(fill);
1644 
1645 	return err;
1646 }
1647 
1648 int ntfs_vbo_to_lbo(struct ntfs_sb_info *sbi, const struct runs_tree *run,
1649 		    u64 vbo, u64 *lbo, u64 *bytes)
1650 {
1651 	u32 off;
1652 	CLST lcn, len;
1653 	u8 cluster_bits = sbi->cluster_bits;
1654 
1655 	if (!run_lookup_entry(run, vbo >> cluster_bits, &lcn, &len, NULL))
1656 		return -ENOENT;
1657 
1658 	off = vbo & sbi->cluster_mask;
1659 	*lbo = lcn == SPARSE_LCN ? -1 : (((u64)lcn << cluster_bits) + off);
1660 	*bytes = ((u64)len << cluster_bits) - off;
1661 
1662 	return 0;
1663 }
1664 
1665 struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST rno, bool dir)
1666 {
1667 	int err = 0;
1668 	struct super_block *sb = sbi->sb;
1669 	struct inode *inode = new_inode(sb);
1670 	struct ntfs_inode *ni;
1671 
1672 	if (!inode)
1673 		return ERR_PTR(-ENOMEM);
1674 
1675 	ni = ntfs_i(inode);
1676 
1677 	err = mi_format_new(&ni->mi, sbi, rno, dir ? RECORD_FLAG_DIR : 0,
1678 			    false);
1679 	if (err)
1680 		goto out;
1681 
1682 	inode->i_ino = rno;
1683 	if (insert_inode_locked(inode) < 0) {
1684 		err = -EIO;
1685 		goto out;
1686 	}
1687 
1688 out:
1689 	if (err) {
1690 		make_bad_inode(inode);
1691 		iput(inode);
1692 		ni = ERR_PTR(err);
1693 	}
1694 	return ni;
1695 }
1696 
1697 /*
1698  * O:BAG:BAD:(A;OICI;FA;;;WD)
1699  * Owner S-1-5-32-544 (Administrators)
1700  * Group S-1-5-32-544 (Administrators)
1701  * ACE: allow S-1-1-0 (Everyone) with FILE_ALL_ACCESS
1702  */
1703 const u8 s_default_security[] __aligned(8) = {
1704 	0x01, 0x00, 0x04, 0x80, 0x30, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
1705 	0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x02, 0x00, 0x1C, 0x00,
1706 	0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x14, 0x00, 0xFF, 0x01, 0x1F, 0x00,
1707 	0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
1708 	0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x20, 0x00, 0x00, 0x00,
1709 	0x20, 0x02, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
1710 	0x20, 0x00, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00,
1711 };
1712 
1713 static_assert(sizeof(s_default_security) == 0x50);
1714 
1715 static inline u32 sid_length(const struct SID *sid)
1716 {
1717 	return struct_size(sid, SubAuthority, sid->SubAuthorityCount);
1718 }
1719 
1720 /*
1721  * is_acl_valid
1722  *
1723  * Thanks Mark Harmstone for idea.
1724  */
1725 static bool is_acl_valid(const struct ACL *acl, u32 len)
1726 {
1727 	const struct ACE_HEADER *ace;
1728 	u32 i;
1729 	u16 ace_count, ace_size;
1730 
1731 	if (acl->AclRevision != ACL_REVISION &&
1732 	    acl->AclRevision != ACL_REVISION_DS) {
1733 		/*
1734 		 * This value should be ACL_REVISION, unless the ACL contains an
1735 		 * object-specific ACE, in which case this value must be ACL_REVISION_DS.
1736 		 * All ACEs in an ACL must be at the same revision level.
1737 		 */
1738 		return false;
1739 	}
1740 
1741 	if (acl->Sbz1)
1742 		return false;
1743 
1744 	if (le16_to_cpu(acl->AclSize) > len)
1745 		return false;
1746 
1747 	if (acl->Sbz2)
1748 		return false;
1749 
1750 	len -= sizeof(struct ACL);
1751 	ace = (struct ACE_HEADER *)&acl[1];
1752 	ace_count = le16_to_cpu(acl->AceCount);
1753 
1754 	for (i = 0; i < ace_count; i++) {
1755 		if (len < sizeof(struct ACE_HEADER))
1756 			return false;
1757 
1758 		ace_size = le16_to_cpu(ace->AceSize);
1759 		if (len < ace_size)
1760 			return false;
1761 
1762 		len -= ace_size;
1763 		ace = Add2Ptr(ace, ace_size);
1764 	}
1765 
1766 	return true;
1767 }
1768 
1769 bool is_sd_valid(const struct SECURITY_DESCRIPTOR_RELATIVE *sd, u32 len)
1770 {
1771 	u32 sd_owner, sd_group, sd_sacl, sd_dacl;
1772 
1773 	if (len < sizeof(struct SECURITY_DESCRIPTOR_RELATIVE))
1774 		return false;
1775 
1776 	if (sd->Revision != 1)
1777 		return false;
1778 
1779 	if (sd->Sbz1)
1780 		return false;
1781 
1782 	if (!(sd->Control & SE_SELF_RELATIVE))
1783 		return false;
1784 
1785 	sd_owner = le32_to_cpu(sd->Owner);
1786 	if (sd_owner) {
1787 		const struct SID *owner = Add2Ptr(sd, sd_owner);
1788 
1789 		if (sd_owner + offsetof(struct SID, SubAuthority) > len)
1790 			return false;
1791 
1792 		if (owner->Revision != 1)
1793 			return false;
1794 
1795 		if (sd_owner + sid_length(owner) > len)
1796 			return false;
1797 	}
1798 
1799 	sd_group = le32_to_cpu(sd->Group);
1800 	if (sd_group) {
1801 		const struct SID *group = Add2Ptr(sd, sd_group);
1802 
1803 		if (sd_group + offsetof(struct SID, SubAuthority) > len)
1804 			return false;
1805 
1806 		if (group->Revision != 1)
1807 			return false;
1808 
1809 		if (sd_group + sid_length(group) > len)
1810 			return false;
1811 	}
1812 
1813 	sd_sacl = le32_to_cpu(sd->Sacl);
1814 	if (sd_sacl) {
1815 		const struct ACL *sacl = Add2Ptr(sd, sd_sacl);
1816 
1817 		if (sd_sacl + sizeof(struct ACL) > len)
1818 			return false;
1819 
1820 		if (!is_acl_valid(sacl, len - sd_sacl))
1821 			return false;
1822 	}
1823 
1824 	sd_dacl = le32_to_cpu(sd->Dacl);
1825 	if (sd_dacl) {
1826 		const struct ACL *dacl = Add2Ptr(sd, sd_dacl);
1827 
1828 		if (sd_dacl + sizeof(struct ACL) > len)
1829 			return false;
1830 
1831 		if (!is_acl_valid(dacl, len - sd_dacl))
1832 			return false;
1833 	}
1834 
1835 	return true;
1836 }
1837 
1838 /*
1839  * ntfs_security_init - Load and parse $Secure.
1840  */
1841 int ntfs_security_init(struct ntfs_sb_info *sbi)
1842 {
1843 	int err;
1844 	struct super_block *sb = sbi->sb;
1845 	struct inode *inode;
1846 	struct ntfs_inode *ni;
1847 	struct MFT_REF ref;
1848 	struct ATTRIB *attr;
1849 	struct ATTR_LIST_ENTRY *le;
1850 	u64 sds_size;
1851 	size_t off;
1852 	struct NTFS_DE *ne;
1853 	struct NTFS_DE_SII *sii_e;
1854 	struct ntfs_fnd *fnd_sii = NULL;
1855 	const struct INDEX_ROOT *root_sii;
1856 	const struct INDEX_ROOT *root_sdh;
1857 	struct ntfs_index *indx_sdh = &sbi->security.index_sdh;
1858 	struct ntfs_index *indx_sii = &sbi->security.index_sii;
1859 
1860 	ref.low = cpu_to_le32(MFT_REC_SECURE);
1861 	ref.high = 0;
1862 	ref.seq = cpu_to_le16(MFT_REC_SECURE);
1863 
1864 	inode = ntfs_iget5(sb, &ref, &NAME_SECURE);
1865 	if (IS_ERR(inode)) {
1866 		err = PTR_ERR(inode);
1867 		ntfs_err(sb, "Failed to load $Secure.");
1868 		inode = NULL;
1869 		goto out;
1870 	}
1871 
1872 	ni = ntfs_i(inode);
1873 
1874 	le = NULL;
1875 
1876 	attr = ni_find_attr(ni, NULL, &le, ATTR_ROOT, SDH_NAME,
1877 			    ARRAY_SIZE(SDH_NAME), NULL, NULL);
1878 	if (!attr) {
1879 		err = -EINVAL;
1880 		goto out;
1881 	}
1882 
1883 	if(!(root_sdh = resident_data_ex(attr, sizeof(struct INDEX_ROOT))) ||
1884 	    root_sdh->type != ATTR_ZERO ||
1885 	    root_sdh->rule != NTFS_COLLATION_TYPE_SECURITY_HASH ||
1886 	    offsetof(struct INDEX_ROOT, ihdr) +
1887 			le32_to_cpu(root_sdh->ihdr.used) >
1888 			le32_to_cpu(attr->res.data_size)) {
1889 		err = -EINVAL;
1890 		goto out;
1891 	}
1892 
1893 	err = indx_init(indx_sdh, sbi, attr, INDEX_MUTEX_SDH);
1894 	if (err)
1895 		goto out;
1896 
1897 	attr = ni_find_attr(ni, attr, &le, ATTR_ROOT, SII_NAME,
1898 			    ARRAY_SIZE(SII_NAME), NULL, NULL);
1899 	if (!attr) {
1900 		err = -EINVAL;
1901 		goto out;
1902 	}
1903 
1904 	if(!(root_sii = resident_data_ex(attr, sizeof(struct INDEX_ROOT))) ||
1905 	    root_sii->type != ATTR_ZERO ||
1906 	    root_sii->rule != NTFS_COLLATION_TYPE_UINT ||
1907 	    offsetof(struct INDEX_ROOT, ihdr) +
1908 			le32_to_cpu(root_sii->ihdr.used) >
1909 			le32_to_cpu(attr->res.data_size)) {
1910 		err = -EINVAL;
1911 		goto out;
1912 	}
1913 
1914 	err = indx_init(indx_sii, sbi, attr, INDEX_MUTEX_SII);
1915 	if (err)
1916 		goto out;
1917 
1918 	fnd_sii = fnd_get();
1919 	if (!fnd_sii) {
1920 		err = -ENOMEM;
1921 		goto out;
1922 	}
1923 
1924 	sds_size = inode->i_size;
1925 
1926 	/* Find the last valid Id. */
1927 	sbi->security.next_id = SECURITY_ID_FIRST;
1928 	/* Always write new security at the end of bucket. */
1929 	sbi->security.next_off =
1930 		ALIGN(sds_size - SecurityDescriptorsBlockSize, 16);
1931 
1932 	off = 0;
1933 	ne = NULL;
1934 
1935 	for (;;) {
1936 		u32 next_id;
1937 
1938 		err = indx_find_raw(indx_sii, ni, root_sii, &ne, &off, fnd_sii);
1939 		if (err || !ne)
1940 			break;
1941 
1942 		sii_e = (struct NTFS_DE_SII *)ne;
1943 		if (le16_to_cpu(ne->view.data_size) < SIZEOF_SECURITY_HDR)
1944 			continue;
1945 
1946 		next_id = le32_to_cpu(sii_e->sec_id) + 1;
1947 		if (next_id >= sbi->security.next_id)
1948 			sbi->security.next_id = next_id;
1949 	}
1950 
1951 	sbi->security.ni = ni;
1952 	inode = NULL;
1953 out:
1954 	iput(inode);
1955 	fnd_put(fnd_sii);
1956 
1957 	return err;
1958 }
1959 
1960 /*
1961  * ntfs_get_security_by_id - Read security descriptor by id.
1962  */
1963 int ntfs_get_security_by_id(struct ntfs_sb_info *sbi, __le32 security_id,
1964 			    struct SECURITY_DESCRIPTOR_RELATIVE **sd,
1965 			    size_t *size)
1966 {
1967 	int err;
1968 	int diff;
1969 	struct ntfs_inode *ni = sbi->security.ni;
1970 	struct ntfs_index *indx = &sbi->security.index_sii;
1971 	void *p = NULL;
1972 	struct NTFS_DE_SII *sii_e;
1973 	struct ntfs_fnd *fnd_sii;
1974 	struct SECURITY_HDR d_security;
1975 	const struct INDEX_ROOT *root_sii;
1976 	u32 t32;
1977 
1978 	*sd = NULL;
1979 
1980 	mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_SECURITY);
1981 
1982 	fnd_sii = fnd_get();
1983 	if (!fnd_sii) {
1984 		err = -ENOMEM;
1985 		goto out;
1986 	}
1987 
1988 	root_sii = indx_get_root(indx, ni, NULL, NULL);
1989 	if (!root_sii) {
1990 		err = -EINVAL;
1991 		goto out;
1992 	}
1993 
1994 	/* Try to find this SECURITY descriptor in SII indexes. */
1995 	err = indx_find(indx, ni, root_sii, &security_id, sizeof(security_id),
1996 			NULL, &diff, (struct NTFS_DE **)&sii_e, fnd_sii);
1997 	if (err)
1998 		goto out;
1999 
2000 	if (diff)
2001 		goto out;
2002 
2003 	t32 = le32_to_cpu(sii_e->sec_hdr.size);
2004 	if (t32 < SIZEOF_SECURITY_HDR) {
2005 		err = -EINVAL;
2006 		goto out;
2007 	}
2008 
2009 	if (t32 > SIZEOF_SECURITY_HDR + 0x10000) {
2010 		/* Looks like too big security. 0x10000 - is arbitrary big number. */
2011 		err = -EFBIG;
2012 		goto out;
2013 	}
2014 
2015 	*size = t32 - SIZEOF_SECURITY_HDR;
2016 
2017 	p = kmalloc(*size, GFP_NOFS);
2018 	if (!p) {
2019 		err = -ENOMEM;
2020 		goto out;
2021 	}
2022 
2023 	err = ntfs_read_run_nb(sbi, &ni->file.run,
2024 			       le64_to_cpu(sii_e->sec_hdr.off), &d_security,
2025 			       sizeof(d_security), NULL);
2026 	if (err)
2027 		goto out;
2028 
2029 	if (memcmp(&d_security, &sii_e->sec_hdr, SIZEOF_SECURITY_HDR)) {
2030 		err = -EINVAL;
2031 		goto out;
2032 	}
2033 
2034 	err = ntfs_read_run_nb(sbi, &ni->file.run,
2035 			       le64_to_cpu(sii_e->sec_hdr.off) +
2036 				       SIZEOF_SECURITY_HDR,
2037 			       p, *size, NULL);
2038 	if (err)
2039 		goto out;
2040 
2041 	*sd = p;
2042 	p = NULL;
2043 
2044 out:
2045 	kfree(p);
2046 	fnd_put(fnd_sii);
2047 	ni_unlock(ni);
2048 
2049 	return err;
2050 }
2051 
2052 /*
2053  * ntfs_insert_security - Insert security descriptor into $Secure::SDS.
2054  *
2055  * SECURITY Descriptor Stream data is organized into chunks of 256K bytes
2056  * and it contains a mirror copy of each security descriptor.  When writing
2057  * to a security descriptor at location X, another copy will be written at
2058  * location (X+256K).
2059  * When writing a security descriptor that will cross the 256K boundary,
2060  * the pointer will be advanced by 256K to skip
2061  * over the mirror portion.
2062  */
2063 int ntfs_insert_security(struct ntfs_sb_info *sbi,
2064 			 const struct SECURITY_DESCRIPTOR_RELATIVE *sd,
2065 			 u32 size_sd, __le32 *security_id, bool *inserted)
2066 {
2067 	int err, diff;
2068 	struct ntfs_inode *ni = sbi->security.ni;
2069 	struct ntfs_index *indx_sdh = &sbi->security.index_sdh;
2070 	struct ntfs_index *indx_sii = &sbi->security.index_sii;
2071 	struct NTFS_DE_SDH *e;
2072 	struct NTFS_DE_SDH sdh_e;
2073 	struct NTFS_DE_SII sii_e;
2074 	struct SECURITY_HDR *d_security;
2075 	u32 new_sec_size = size_sd + SIZEOF_SECURITY_HDR;
2076 	u32 aligned_sec_size = ALIGN(new_sec_size, 16);
2077 	struct SECURITY_KEY hash_key;
2078 	struct ntfs_fnd *fnd_sdh = NULL;
2079 	const struct INDEX_ROOT *root_sdh;
2080 	const struct INDEX_ROOT *root_sii;
2081 	u64 mirr_off, new_sds_size;
2082 	u32 next, left;
2083 
2084 	static_assert((1 << Log2OfSecurityDescriptorsBlockSize) ==
2085 		      SecurityDescriptorsBlockSize);
2086 
2087 	hash_key.hash = security_hash(sd, size_sd);
2088 	hash_key.sec_id = SECURITY_ID_INVALID;
2089 
2090 	if (inserted)
2091 		*inserted = false;
2092 	*security_id = SECURITY_ID_INVALID;
2093 
2094 	/* Allocate a temporal buffer. */
2095 	d_security = kzalloc(aligned_sec_size, GFP_NOFS);
2096 	if (!d_security)
2097 		return -ENOMEM;
2098 
2099 	mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_SECURITY);
2100 
2101 	fnd_sdh = fnd_get();
2102 	if (!fnd_sdh) {
2103 		err = -ENOMEM;
2104 		goto out;
2105 	}
2106 
2107 	root_sdh = indx_get_root(indx_sdh, ni, NULL, NULL);
2108 	if (!root_sdh) {
2109 		err = -EINVAL;
2110 		goto out;
2111 	}
2112 
2113 	root_sii = indx_get_root(indx_sii, ni, NULL, NULL);
2114 	if (!root_sii) {
2115 		err = -EINVAL;
2116 		goto out;
2117 	}
2118 
2119 	/*
2120 	 * Check if such security already exists.
2121 	 * Use "SDH" and hash -> to get the offset in "SDS".
2122 	 */
2123 	err = indx_find(indx_sdh, ni, root_sdh, &hash_key, sizeof(hash_key),
2124 			&d_security->key.sec_id, &diff, (struct NTFS_DE **)&e,
2125 			fnd_sdh);
2126 	if (err)
2127 		goto out;
2128 
2129 	while (e) {
2130 		if (le32_to_cpu(e->sec_hdr.size) == new_sec_size) {
2131 			err = ntfs_read_run_nb(sbi, &ni->file.run,
2132 					       le64_to_cpu(e->sec_hdr.off),
2133 					       d_security, new_sec_size, NULL);
2134 			if (err)
2135 				goto out;
2136 
2137 			if (le32_to_cpu(d_security->size) == new_sec_size &&
2138 			    d_security->key.hash == hash_key.hash &&
2139 			    !memcmp(d_security + 1, sd, size_sd)) {
2140 				*security_id = d_security->key.sec_id;
2141 				/* Such security already exists. */
2142 				err = 0;
2143 				goto out;
2144 			}
2145 		}
2146 
2147 		err = indx_find_sort(indx_sdh, ni, root_sdh,
2148 				     (struct NTFS_DE **)&e, fnd_sdh);
2149 		if (err)
2150 			goto out;
2151 
2152 		if (!e || e->key.hash != hash_key.hash)
2153 			break;
2154 	}
2155 
2156 	/* Zero unused space. */
2157 	next = sbi->security.next_off & (SecurityDescriptorsBlockSize - 1);
2158 	left = SecurityDescriptorsBlockSize - next;
2159 
2160 	/* Zero gap until SecurityDescriptorsBlockSize. */
2161 	if (left < new_sec_size) {
2162 		/* Zero "left" bytes from sbi->security.next_off. */
2163 		sbi->security.next_off += SecurityDescriptorsBlockSize + left;
2164 	}
2165 
2166 	/* Zero tail of previous security. */
2167 	//used = ni->vfs_inode.i_size & (SecurityDescriptorsBlockSize - 1);
2168 
2169 	/*
2170 	 * Example:
2171 	 * 0x40438 == ni->vfs_inode.i_size
2172 	 * 0x00440 == sbi->security.next_off
2173 	 * need to zero [0x438-0x440)
2174 	 * if (next > used) {
2175 	 *  u32 tozero = next - used;
2176 	 *  zero "tozero" bytes from sbi->security.next_off - tozero
2177 	 */
2178 
2179 	/* Format new security descriptor. */
2180 	d_security->key.hash = hash_key.hash;
2181 	d_security->key.sec_id = cpu_to_le32(sbi->security.next_id);
2182 	d_security->off = cpu_to_le64(sbi->security.next_off);
2183 	d_security->size = cpu_to_le32(new_sec_size);
2184 	memcpy(d_security + 1, sd, size_sd);
2185 
2186 	/* Write main SDS bucket. */
2187 	err = ntfs_sb_write_run(sbi, &ni->file.run, sbi->security.next_off,
2188 				d_security, aligned_sec_size, 0);
2189 
2190 	if (err)
2191 		goto out;
2192 
2193 	mirr_off = sbi->security.next_off + SecurityDescriptorsBlockSize;
2194 	new_sds_size = mirr_off + aligned_sec_size;
2195 
2196 	if (new_sds_size > ni->vfs_inode.i_size) {
2197 		err = attr_set_size(ni, ATTR_DATA, SDS_NAME,
2198 				    ARRAY_SIZE(SDS_NAME), &ni->file.run,
2199 				    new_sds_size, &new_sds_size, false, NULL);
2200 		if (err)
2201 			goto out;
2202 	}
2203 
2204 	/* Write copy SDS bucket. */
2205 	err = ntfs_sb_write_run(sbi, &ni->file.run, mirr_off, d_security,
2206 				aligned_sec_size, 0);
2207 	if (err)
2208 		goto out;
2209 
2210 	/* Fill SII entry. */
2211 	sii_e.de.view.data_off =
2212 		cpu_to_le16(offsetof(struct NTFS_DE_SII, sec_hdr));
2213 	sii_e.de.view.data_size = cpu_to_le16(SIZEOF_SECURITY_HDR);
2214 	sii_e.de.view.res = 0;
2215 	sii_e.de.size = cpu_to_le16(SIZEOF_SII_DIRENTRY);
2216 	sii_e.de.key_size = cpu_to_le16(sizeof(d_security->key.sec_id));
2217 	sii_e.de.flags = 0;
2218 	sii_e.de.res = 0;
2219 	sii_e.sec_id = d_security->key.sec_id;
2220 	memcpy(&sii_e.sec_hdr, d_security, SIZEOF_SECURITY_HDR);
2221 
2222 	err = indx_insert_entry(indx_sii, ni, &sii_e.de, NULL, NULL, 0);
2223 	if (err)
2224 		goto out;
2225 
2226 	/* Fill SDH entry. */
2227 	sdh_e.de.view.data_off =
2228 		cpu_to_le16(offsetof(struct NTFS_DE_SDH, sec_hdr));
2229 	sdh_e.de.view.data_size = cpu_to_le16(SIZEOF_SECURITY_HDR);
2230 	sdh_e.de.view.res = 0;
2231 	sdh_e.de.size = cpu_to_le16(SIZEOF_SDH_DIRENTRY);
2232 	sdh_e.de.key_size = cpu_to_le16(sizeof(sdh_e.key));
2233 	sdh_e.de.flags = 0;
2234 	sdh_e.de.res = 0;
2235 	sdh_e.key.hash = d_security->key.hash;
2236 	sdh_e.key.sec_id = d_security->key.sec_id;
2237 	memcpy(&sdh_e.sec_hdr, d_security, SIZEOF_SECURITY_HDR);
2238 	sdh_e.magic[0] = cpu_to_le16('I');
2239 	sdh_e.magic[1] = cpu_to_le16('I');
2240 
2241 	fnd_clear(fnd_sdh);
2242 	err = indx_insert_entry(indx_sdh, ni, &sdh_e.de, (void *)(size_t)1,
2243 				fnd_sdh, 0);
2244 	if (err)
2245 		goto out;
2246 
2247 	*security_id = d_security->key.sec_id;
2248 	if (inserted)
2249 		*inserted = true;
2250 
2251 	/* Update Id and offset for next descriptor. */
2252 	sbi->security.next_id += 1;
2253 	sbi->security.next_off += aligned_sec_size;
2254 
2255 out:
2256 	fnd_put(fnd_sdh);
2257 	mark_inode_dirty(&ni->vfs_inode);
2258 	ni_unlock(ni);
2259 	kfree(d_security);
2260 
2261 	return err;
2262 }
2263 
2264 /*
2265  * ntfs_reparse_init - Load and parse $Extend/$Reparse.
2266  */
2267 int ntfs_reparse_init(struct ntfs_sb_info *sbi)
2268 {
2269 	int err;
2270 	struct ntfs_inode *ni = sbi->reparse.ni;
2271 	struct ntfs_index *indx = &sbi->reparse.index_r;
2272 	struct ATTRIB *attr;
2273 	struct ATTR_LIST_ENTRY *le;
2274 	const struct INDEX_ROOT *root_r;
2275 
2276 	if (!ni)
2277 		return 0;
2278 
2279 	le = NULL;
2280 	attr = ni_find_attr(ni, NULL, &le, ATTR_ROOT, SR_NAME,
2281 			    ARRAY_SIZE(SR_NAME), NULL, NULL);
2282 	if (!attr) {
2283 		err = -EINVAL;
2284 		goto out;
2285 	}
2286 
2287 	root_r = resident_data(attr);
2288 	if (root_r->type != ATTR_ZERO ||
2289 	    root_r->rule != NTFS_COLLATION_TYPE_UINTS) {
2290 		err = -EINVAL;
2291 		goto out;
2292 	}
2293 
2294 	err = indx_init(indx, sbi, attr, INDEX_MUTEX_SR);
2295 	if (err)
2296 		goto out;
2297 
2298 out:
2299 	return err;
2300 }
2301 
2302 /*
2303  * ntfs_objid_init - Load and parse $Extend/$ObjId.
2304  */
2305 int ntfs_objid_init(struct ntfs_sb_info *sbi)
2306 {
2307 	int err;
2308 	struct ntfs_inode *ni = sbi->objid.ni;
2309 	struct ntfs_index *indx = &sbi->objid.index_o;
2310 	struct ATTRIB *attr;
2311 	struct ATTR_LIST_ENTRY *le;
2312 	const struct INDEX_ROOT *root;
2313 
2314 	if (!ni)
2315 		return 0;
2316 
2317 	le = NULL;
2318 	attr = ni_find_attr(ni, NULL, &le, ATTR_ROOT, SO_NAME,
2319 			    ARRAY_SIZE(SO_NAME), NULL, NULL);
2320 	if (!attr) {
2321 		err = -EINVAL;
2322 		goto out;
2323 	}
2324 
2325 	root = resident_data(attr);
2326 	if (root->type != ATTR_ZERO ||
2327 	    root->rule != NTFS_COLLATION_TYPE_UINTS) {
2328 		err = -EINVAL;
2329 		goto out;
2330 	}
2331 
2332 	err = indx_init(indx, sbi, attr, INDEX_MUTEX_SO);
2333 	if (err)
2334 		goto out;
2335 
2336 out:
2337 	return err;
2338 }
2339 
2340 int ntfs_objid_remove(struct ntfs_sb_info *sbi, struct GUID *guid)
2341 {
2342 	int err;
2343 	struct ntfs_inode *ni = sbi->objid.ni;
2344 	struct ntfs_index *indx = &sbi->objid.index_o;
2345 
2346 	if (!ni)
2347 		return -EINVAL;
2348 
2349 	mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_OBJID);
2350 
2351 	err = indx_delete_entry(indx, ni, guid, sizeof(*guid), NULL);
2352 
2353 	mark_inode_dirty(&ni->vfs_inode);
2354 	ni_unlock(ni);
2355 
2356 	return err;
2357 }
2358 
2359 int ntfs_insert_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
2360 			const struct MFT_REF *ref)
2361 {
2362 	int err;
2363 	struct ntfs_inode *ni = sbi->reparse.ni;
2364 	struct ntfs_index *indx = &sbi->reparse.index_r;
2365 	struct NTFS_DE_R re;
2366 
2367 	if (!ni)
2368 		return -EINVAL;
2369 
2370 	memset(&re, 0, sizeof(re));
2371 
2372 	re.de.view.data_off = cpu_to_le16(offsetof(struct NTFS_DE_R, zero));
2373 	re.de.size = cpu_to_le16(sizeof(struct NTFS_DE_R));
2374 	re.de.key_size = cpu_to_le16(sizeof(re.key));
2375 
2376 	re.key.ReparseTag = rtag;
2377 	memcpy(&re.key.ref, ref, sizeof(*ref));
2378 
2379 	mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_REPARSE);
2380 
2381 	err = indx_insert_entry(indx, ni, &re.de, NULL, NULL, 0);
2382 
2383 	mark_inode_dirty(&ni->vfs_inode);
2384 	ni_unlock(ni);
2385 
2386 	return err;
2387 }
2388 
2389 int ntfs_remove_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
2390 			const struct MFT_REF *ref)
2391 {
2392 	int err, diff;
2393 	struct ntfs_inode *ni = sbi->reparse.ni;
2394 	struct ntfs_index *indx = &sbi->reparse.index_r;
2395 	struct ntfs_fnd *fnd = NULL;
2396 	struct REPARSE_KEY rkey;
2397 	struct NTFS_DE_R *re;
2398 	struct INDEX_ROOT *root_r;
2399 
2400 	if (!ni)
2401 		return -EINVAL;
2402 
2403 	rkey.ReparseTag = rtag;
2404 	rkey.ref = *ref;
2405 
2406 	mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_REPARSE);
2407 
2408 	if (rtag) {
2409 		err = indx_delete_entry(indx, ni, &rkey, sizeof(rkey), NULL);
2410 		goto out1;
2411 	}
2412 
2413 	fnd = fnd_get();
2414 	if (!fnd) {
2415 		err = -ENOMEM;
2416 		goto out1;
2417 	}
2418 
2419 	root_r = indx_get_root(indx, ni, NULL, NULL);
2420 	if (!root_r) {
2421 		err = -EINVAL;
2422 		goto out;
2423 	}
2424 
2425 	/* 1 - forces to ignore rkey.ReparseTag when comparing keys. */
2426 	err = indx_find(indx, ni, root_r, &rkey, sizeof(rkey), (void *)1, &diff,
2427 			(struct NTFS_DE **)&re, fnd);
2428 	if (err)
2429 		goto out;
2430 
2431 	if (memcmp(&re->key.ref, ref, sizeof(*ref))) {
2432 		/* Impossible. Looks like volume corrupt? */
2433 		goto out;
2434 	}
2435 
2436 	memcpy(&rkey, &re->key, sizeof(rkey));
2437 
2438 	fnd_put(fnd);
2439 	fnd = NULL;
2440 
2441 	err = indx_delete_entry(indx, ni, &rkey, sizeof(rkey), NULL);
2442 	if (err)
2443 		goto out;
2444 
2445 out:
2446 	fnd_put(fnd);
2447 
2448 out1:
2449 	mark_inode_dirty(&ni->vfs_inode);
2450 	ni_unlock(ni);
2451 
2452 	return err;
2453 }
2454 
2455 static inline void ntfs_unmap_and_discard(struct ntfs_sb_info *sbi, CLST lcn,
2456 					  CLST len)
2457 {
2458 	ntfs_unmap_meta(sbi->sb, lcn, len);
2459 	ntfs_discard(sbi, lcn, len);
2460 }
2461 
2462 void mark_as_free_ex(struct ntfs_sb_info *sbi, CLST lcn, CLST len, bool trim)
2463 {
2464 	CLST end, i, zone_len, zlen;
2465 	struct wnd_bitmap *wnd = &sbi->used.bitmap;
2466 
2467 	down_write_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS);
2468 	if (!wnd_is_used(wnd, lcn, len)) {
2469 		ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
2470 
2471 		end = lcn + len;
2472 		len = 0;
2473 		for (i = lcn; i < end; i++) {
2474 			if (wnd_is_used(wnd, i, 1)) {
2475 				if (!len)
2476 					lcn = i;
2477 				len += 1;
2478 				continue;
2479 			}
2480 
2481 			if (!len)
2482 				continue;
2483 
2484 			if (trim)
2485 				ntfs_unmap_and_discard(sbi, lcn, len);
2486 
2487 			wnd_set_free(wnd, lcn, len);
2488 			len = 0;
2489 		}
2490 
2491 		if (!len)
2492 			goto out;
2493 	}
2494 
2495 	if (trim)
2496 		ntfs_unmap_and_discard(sbi, lcn, len);
2497 	wnd_set_free(wnd, lcn, len);
2498 
2499 	/* append to MFT zone, if possible. */
2500 	zone_len = wnd_zone_len(wnd);
2501 	zlen = min(zone_len + len, sbi->zone_max);
2502 
2503 	if (zlen == zone_len) {
2504 		/* MFT zone already has maximum size. */
2505 	} else if (!zone_len) {
2506 		/* Create MFT zone only if 'zlen' is large enough. */
2507 		if (zlen == sbi->zone_max)
2508 			wnd_zone_set(wnd, lcn, zlen);
2509 	} else {
2510 		CLST zone_lcn = wnd_zone_bit(wnd);
2511 
2512 		if (lcn + len == zone_lcn) {
2513 			/* Append into head MFT zone. */
2514 			wnd_zone_set(wnd, lcn, zlen);
2515 		} else if (zone_lcn + zone_len == lcn) {
2516 			/* Append into tail MFT zone. */
2517 			wnd_zone_set(wnd, zone_lcn, zlen);
2518 		}
2519 	}
2520 
2521 out:
2522 	up_write(&wnd->rw_lock);
2523 }
2524 
2525 /*
2526  * run_deallocate - Deallocate clusters.
2527  */
2528 int run_deallocate(struct ntfs_sb_info *sbi, struct runs_tree *run, bool trim)
2529 {
2530 	CLST lcn, len;
2531 	size_t idx = 0;
2532 
2533 	while (run_get_entry(run, idx++, NULL, &lcn, &len)) {
2534 		if (lcn == SPARSE_LCN)
2535 			continue;
2536 
2537 		mark_as_free_ex(sbi, lcn, len, trim);
2538 	}
2539 
2540 	return 0;
2541 }
2542 
2543 static inline bool name_has_forbidden_chars(const struct le_str *fname)
2544 {
2545 	int i, ch;
2546 
2547 	/* check for forbidden chars */
2548 	for (i = 0; i < fname->len; ++i) {
2549 		ch = le16_to_cpu(fname->name[i]);
2550 
2551 		/* control chars */
2552 		if (ch < 0x20)
2553 			return true;
2554 
2555 		switch (ch) {
2556 		/* disallowed by Windows */
2557 		case '\\':
2558 		case '/':
2559 		case ':':
2560 		case '*':
2561 		case '?':
2562 		case '<':
2563 		case '>':
2564 		case '|':
2565 		case '\"':
2566 			return true;
2567 
2568 		default:
2569 			/* allowed char */
2570 			break;
2571 		}
2572 	}
2573 
2574 	/* file names cannot end with space or . */
2575 	if (fname->len > 0) {
2576 		ch = le16_to_cpu(fname->name[fname->len - 1]);
2577 		if (ch == ' ' || ch == '.')
2578 			return true;
2579 	}
2580 
2581 	return false;
2582 }
2583 
2584 static inline bool is_reserved_name(struct ntfs_sb_info *sbi,
2585 				    const struct le_str *fname)
2586 {
2587 	int port_digit;
2588 	const __le16 *name = fname->name;
2589 	int len = fname->len;
2590 	u16 *upcase = sbi->upcase;
2591 
2592 	/* check for 3 chars reserved names (device names) */
2593 	/* name by itself or with any extension is forbidden */
2594 	if (len == 3 || (len > 3 && le16_to_cpu(name[3]) == '.'))
2595 		if (!ntfs_cmp_names(name, 3, CON_NAME, 3, upcase, false) ||
2596 		    !ntfs_cmp_names(name, 3, NUL_NAME, 3, upcase, false) ||
2597 		    !ntfs_cmp_names(name, 3, AUX_NAME, 3, upcase, false) ||
2598 		    !ntfs_cmp_names(name, 3, PRN_NAME, 3, upcase, false))
2599 			return true;
2600 
2601 	/* check for 4 chars reserved names (port name followed by 1..9) */
2602 	/* name by itself or with any extension is forbidden */
2603 	if (len == 4 || (len > 4 && le16_to_cpu(name[4]) == '.')) {
2604 		port_digit = le16_to_cpu(name[3]);
2605 		if (port_digit >= '1' && port_digit <= '9')
2606 			if (!ntfs_cmp_names(name, 3, COM_NAME, 3, upcase,
2607 					    false) ||
2608 			    !ntfs_cmp_names(name, 3, LPT_NAME, 3, upcase,
2609 					    false))
2610 				return true;
2611 	}
2612 
2613 	return false;
2614 }
2615 
2616 /*
2617  * valid_windows_name - Check if a file name is valid in Windows.
2618  */
2619 bool valid_windows_name(struct ntfs_sb_info *sbi, const struct le_str *fname)
2620 {
2621 	return !name_has_forbidden_chars(fname) &&
2622 	       !is_reserved_name(sbi, fname);
2623 }
2624