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