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