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