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