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