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

source code of linux/fs/ntfs3/inode.c