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