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