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