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