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