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