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