blob: a87ab3ad3cd386397e9c210c691824fe83b647a9 [file] [log] [blame]
Konstantin Komarov82cae262021-08-13 17:21:29 +03001// SPDX-License-Identifier: GPL-2.0
2/*
3 *
4 * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5 *
6 */
7
Konstantin Komarov82cae262021-08-13 17:21:29 +03008#include <linux/buffer_head.h>
9#include <linux/fs.h>
Konstantin Komarov82cae262021-08-13 17:21:29 +030010#include <linux/mpage.h>
11#include <linux/namei.h>
12#include <linux/nls.h>
13#include <linux/uio.h>
Konstantin Komarov82cae262021-08-13 17:21:29 +030014#include <linux/writeback.h>
15
16#include "debug.h"
17#include "ntfs.h"
18#include "ntfs_fs.h"
19
20/*
Kari Argillandere8b8e972021-08-03 14:57:09 +030021 * ntfs_read_mft - Read record and parses MFT.
Konstantin Komarov82cae262021-08-13 17:21:29 +030022 */
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' */
Kari Argillander564c97b2021-09-07 18:35:51 +030050 inode->i_uid = sbi->options->fs_uid;
51 inode->i_gid = sbi->options->fs_gid;
Konstantin Komarov82cae262021-08-13 17:21:29 +030052
53 err = mi_init(&ni->mi, sbi, ino);
54 if (err)
55 goto out;
56
57 if (!sbi->mft.ni && ino == MFT_REC_MFT && !sb->s_root) {
58 t64 = sbi->mft.lbo >> sbi->cluster_bits;
59 t32 = bytes_to_cluster(sbi, MFT_REC_VOL * sbi->record_size);
60 sbi->mft.ni = ni;
61 init_rwsem(&ni->file.run_lock);
62
63 if (!run_add_entry(&ni->file.run, 0, t64, t32, true)) {
64 err = -ENOMEM;
65 goto out;
66 }
67 }
68
69 err = mi_read(&ni->mi, ino == MFT_REC_MFT);
70
71 if (err)
72 goto out;
73
74 rec = ni->mi.mrec;
75
76 if (sbi->flags & NTFS_FLAGS_LOG_REPLAYING) {
77 ;
78 } else if (ref->seq != rec->seq) {
79 err = -EINVAL;
80 ntfs_err(sb, "MFT: r=%lx, expect seq=%x instead of %x!", ino,
81 le16_to_cpu(ref->seq), le16_to_cpu(rec->seq));
82 goto out;
83 } else if (!is_rec_inuse(rec)) {
84 err = -EINVAL;
85 ntfs_err(sb, "Inode r=%x is not in use!", (u32)ino);
86 goto out;
87 }
88
89 if (le32_to_cpu(rec->total) != sbi->record_size) {
Konstantin Komarovd3624462021-08-31 16:57:40 +030090 /* Bad inode? */
Konstantin Komarov82cae262021-08-13 17:21:29 +030091 err = -EINVAL;
92 goto out;
93 }
94
95 if (!is_rec_base(rec))
96 goto Ok;
97
Kari Argillandere8b8e972021-08-03 14:57:09 +030098 /* Record should contain $I30 root. */
Konstantin Komarov82cae262021-08-13 17:21:29 +030099 is_dir = rec->flags & RECORD_FLAG_DIR;
100
101 inode->i_generation = le16_to_cpu(rec->seq);
102
Kari Argillandere8b8e972021-08-03 14:57:09 +0300103 /* Enumerate all struct Attributes MFT. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300104 le = NULL;
105 attr = NULL;
106
107 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300108 * To reduce tab pressure use goto instead of
Konstantin Komarov82cae262021-08-13 17:21:29 +0300109 * while( (attr = ni_enum_attr_ex(ni, attr, &le, NULL) ))
110 */
111next_attr:
112 run = NULL;
113 err = -EINVAL;
114 attr = ni_enum_attr_ex(ni, attr, &le, NULL);
115 if (!attr)
116 goto end_enum;
117
118 if (le && le->vcn) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300119 /* This is non primary attribute segment. Ignore if not MFT. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300120 if (ino != MFT_REC_MFT || attr->type != ATTR_DATA)
121 goto next_attr;
122
123 run = &ni->file.run;
124 asize = le32_to_cpu(attr->size);
125 goto attr_unpack_run;
126 }
127
128 roff = attr->non_res ? 0 : le16_to_cpu(attr->res.data_off);
129 rsize = attr->non_res ? 0 : le32_to_cpu(attr->res.data_size);
130 asize = le32_to_cpu(attr->size);
131
132 switch (attr->type) {
133 case ATTR_STD:
134 if (attr->non_res ||
135 asize < sizeof(struct ATTR_STD_INFO) + roff ||
136 rsize < sizeof(struct ATTR_STD_INFO))
137 goto out;
138
139 if (std5)
140 goto next_attr;
141
142 std5 = Add2Ptr(attr, roff);
143
144#ifdef STATX_BTIME
145 nt2kernel(std5->cr_time, &ni->i_crtime);
146#endif
147 nt2kernel(std5->a_time, &inode->i_atime);
148 nt2kernel(std5->c_time, &inode->i_ctime);
149 nt2kernel(std5->m_time, &inode->i_mtime);
150
151 ni->std_fa = std5->fa;
152
153 if (asize >= sizeof(struct ATTR_STD_INFO5) + roff &&
154 rsize >= sizeof(struct ATTR_STD_INFO5))
155 ni->std_security_id = std5->security_id;
156 goto next_attr;
157
158 case ATTR_LIST:
159 if (attr->name_len || le || ino == MFT_REC_LOG)
160 goto out;
161
162 err = ntfs_load_attr_list(ni, attr);
163 if (err)
164 goto out;
165
166 le = NULL;
167 attr = NULL;
168 goto next_attr;
169
170 case ATTR_NAME:
171 if (attr->non_res || asize < SIZEOF_ATTRIBUTE_FILENAME + roff ||
172 rsize < SIZEOF_ATTRIBUTE_FILENAME)
173 goto out;
174
175 fname = Add2Ptr(attr, roff);
176 if (fname->type == FILE_NAME_DOS)
177 goto next_attr;
178
179 names += 1;
180 if (name && name->len == fname->name_len &&
181 !ntfs_cmp_names_cpu(name, (struct le_str *)&fname->name_len,
182 NULL, false))
183 is_match = true;
184
185 goto next_attr;
186
187 case ATTR_DATA:
188 if (is_dir) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300189 /* Ignore data attribute in dir record. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300190 goto next_attr;
191 }
192
193 if (ino == MFT_REC_BADCLUST && !attr->non_res)
194 goto next_attr;
195
196 if (attr->name_len &&
197 ((ino != MFT_REC_BADCLUST || !attr->non_res ||
198 attr->name_len != ARRAY_SIZE(BAD_NAME) ||
199 memcmp(attr_name(attr), BAD_NAME, sizeof(BAD_NAME))) &&
200 (ino != MFT_REC_SECURE || !attr->non_res ||
201 attr->name_len != ARRAY_SIZE(SDS_NAME) ||
202 memcmp(attr_name(attr), SDS_NAME, sizeof(SDS_NAME))))) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300203 /* File contains stream attribute. Ignore it. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300204 goto next_attr;
205 }
206
207 if (is_attr_sparsed(attr))
208 ni->std_fa |= FILE_ATTRIBUTE_SPARSE_FILE;
209 else
210 ni->std_fa &= ~FILE_ATTRIBUTE_SPARSE_FILE;
211
212 if (is_attr_compressed(attr))
213 ni->std_fa |= FILE_ATTRIBUTE_COMPRESSED;
214 else
215 ni->std_fa &= ~FILE_ATTRIBUTE_COMPRESSED;
216
217 if (is_attr_encrypted(attr))
218 ni->std_fa |= FILE_ATTRIBUTE_ENCRYPTED;
219 else
220 ni->std_fa &= ~FILE_ATTRIBUTE_ENCRYPTED;
221
222 if (!attr->non_res) {
223 ni->i_valid = inode->i_size = rsize;
224 inode_set_bytes(inode, rsize);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300225 }
226
Kari Argillander564c97b2021-09-07 18:35:51 +0300227 mode = S_IFREG | (0777 & sbi->options->fs_fmask_inv);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300228
229 if (!attr->non_res) {
230 ni->ni_flags |= NI_FLAG_RESIDENT;
231 goto next_attr;
232 }
233
234 inode_set_bytes(inode, attr_ondisk_size(attr));
235
236 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
237 inode->i_size = le64_to_cpu(attr->nres.data_size);
238 if (!attr->nres.alloc_size)
239 goto next_attr;
240
241 run = ino == MFT_REC_BITMAP ? &sbi->used.bitmap.run
242 : &ni->file.run;
243 break;
244
245 case ATTR_ROOT:
246 if (attr->non_res)
247 goto out;
248
249 root = Add2Ptr(attr, roff);
250 is_root = true;
251
252 if (attr->name_len != ARRAY_SIZE(I30_NAME) ||
253 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
254 goto next_attr;
255
256 if (root->type != ATTR_NAME ||
257 root->rule != NTFS_COLLATION_TYPE_FILENAME)
258 goto out;
259
260 if (!is_dir)
261 goto next_attr;
262
263 ni->ni_flags |= NI_FLAG_DIR;
264
265 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
266 if (err)
267 goto out;
268
269 mode = sb->s_root
Kari Argillander564c97b2021-09-07 18:35:51 +0300270 ? (S_IFDIR | (0777 & sbi->options->fs_dmask_inv))
Konstantin Komarov82cae262021-08-13 17:21:29 +0300271 : (S_IFDIR | 0777);
272 goto next_attr;
273
274 case ATTR_ALLOC:
275 if (!is_root || attr->name_len != ARRAY_SIZE(I30_NAME) ||
276 memcmp(attr_name(attr), I30_NAME, sizeof(I30_NAME)))
277 goto next_attr;
278
279 inode->i_size = le64_to_cpu(attr->nres.data_size);
280 ni->i_valid = le64_to_cpu(attr->nres.valid_size);
281 inode_set_bytes(inode, le64_to_cpu(attr->nres.alloc_size));
282
283 run = &ni->dir.alloc_run;
284 break;
285
286 case ATTR_BITMAP:
287 if (ino == MFT_REC_MFT) {
288 if (!attr->non_res)
289 goto out;
290#ifndef CONFIG_NTFS3_64BIT_CLUSTER
291 /* 0x20000000 = 2^32 / 8 */
292 if (le64_to_cpu(attr->nres.alloc_size) >= 0x20000000)
293 goto out;
294#endif
295 run = &sbi->mft.bitmap.run;
296 break;
297 } else if (is_dir && attr->name_len == ARRAY_SIZE(I30_NAME) &&
298 !memcmp(attr_name(attr), I30_NAME,
299 sizeof(I30_NAME)) &&
300 attr->non_res) {
301 run = &ni->dir.bitmap_run;
302 break;
303 }
304 goto next_attr;
305
306 case ATTR_REPARSE:
307 if (attr->name_len)
308 goto next_attr;
309
310 rp_fa = ni_parse_reparse(ni, attr, &rp);
311 switch (rp_fa) {
312 case REPARSE_LINK:
Konstantin Komarov22b05f12021-10-05 19:33:26 +0300313 /*
314 * Normal symlink.
315 * Assume one unicode symbol == one utf8.
316 */
317 inode->i_size = le16_to_cpu(rp.SymbolicLinkReparseBuffer
318 .PrintNameLength) /
319 sizeof(u16);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300320
Konstantin Komarov82cae262021-08-13 17:21:29 +0300321 ni->i_valid = inode->i_size;
322
Kari Argillandere8b8e972021-08-03 14:57:09 +0300323 /* Clear directory bit. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300324 if (ni->ni_flags & NI_FLAG_DIR) {
325 indx_clear(&ni->dir);
326 memset(&ni->dir, 0, sizeof(ni->dir));
327 ni->ni_flags &= ~NI_FLAG_DIR;
328 } else {
329 run_close(&ni->file.run);
330 }
331 mode = S_IFLNK | 0777;
332 is_dir = false;
333 if (attr->non_res) {
334 run = &ni->file.run;
Kari Argillandere8b8e972021-08-03 14:57:09 +0300335 goto attr_unpack_run; // Double break.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300336 }
337 break;
338
339 case REPARSE_COMPRESSED:
340 break;
341
342 case REPARSE_DEDUPLICATED:
343 break;
344 }
345 goto next_attr;
346
347 case ATTR_EA_INFO:
348 if (!attr->name_len &&
349 resident_data_ex(attr, sizeof(struct EA_INFO))) {
350 ni->ni_flags |= NI_FLAG_EA;
351 /*
352 * ntfs_get_wsl_perm updates inode->i_uid, inode->i_gid, inode->i_mode
353 */
354 inode->i_mode = mode;
355 ntfs_get_wsl_perm(inode);
356 mode = inode->i_mode;
357 }
358 goto next_attr;
359
360 default:
361 goto next_attr;
362 }
363
364attr_unpack_run:
365 roff = le16_to_cpu(attr->nres.run_off);
366
367 t64 = le64_to_cpu(attr->nres.svcn);
368 err = run_unpack_ex(run, sbi, ino, t64, le64_to_cpu(attr->nres.evcn),
369 t64, Add2Ptr(attr, roff), asize - roff);
370 if (err < 0)
371 goto out;
372 err = 0;
373 goto next_attr;
374
375end_enum:
376
377 if (!std5)
378 goto out;
379
380 if (!is_match && name) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300381 /* Reuse rec as buffer for ascii name. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300382 err = -ENOENT;
383 goto out;
384 }
385
386 if (std5->fa & FILE_ATTRIBUTE_READONLY)
387 mode &= ~0222;
388
389 if (!names) {
390 err = -EINVAL;
391 goto out;
392 }
393
Konstantin Komarov78ab59f2021-08-31 18:52:39 +0300394 if (names != le16_to_cpu(rec->hard_links)) {
395 /* Correct minor error on the fly. Do not mark inode as dirty. */
396 rec->hard_links = cpu_to_le16(names);
397 ni->mi.dirty = true;
398 }
399
Konstantin Komarov82cae262021-08-13 17:21:29 +0300400 set_nlink(inode, names);
401
402 if (S_ISDIR(mode)) {
403 ni->std_fa |= FILE_ATTRIBUTE_DIRECTORY;
404
405 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300406 * Dot and dot-dot should be included in count but was not
Konstantin Komarov82cae262021-08-13 17:21:29 +0300407 * included in enumeration.
Kari Argillandere8b8e972021-08-03 14:57:09 +0300408 * Usually a hard links to directories are disabled.
Konstantin Komarov82cae262021-08-13 17:21:29 +0300409 */
410 inode->i_op = &ntfs_dir_inode_operations;
411 inode->i_fop = &ntfs_dir_operations;
412 ni->i_valid = 0;
413 } else if (S_ISLNK(mode)) {
414 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
415 inode->i_op = &ntfs_link_inode_operations;
416 inode->i_fop = NULL;
Konstantin Komarov22b05f12021-10-05 19:33:26 +0300417 inode_nohighmem(inode);
Konstantin Komarov82cae262021-08-13 17:21:29 +0300418 } else if (S_ISREG(mode)) {
419 ni->std_fa &= ~FILE_ATTRIBUTE_DIRECTORY;
420 inode->i_op = &ntfs_file_inode_operations;
421 inode->i_fop = &ntfs_file_operations;
422 inode->i_mapping->a_ops =
423 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
424 if (ino != MFT_REC_MFT)
425 init_rwsem(&ni->file.run_lock);
426 } else if (S_ISCHR(mode) || S_ISBLK(mode) || S_ISFIFO(mode) ||
427 S_ISSOCK(mode)) {
428 inode->i_op = &ntfs_special_inode_operations;
429 init_special_inode(inode, mode, inode->i_rdev);
430 } else if (fname && fname->home.low == cpu_to_le32(MFT_REC_EXTEND) &&
431 fname->home.seq == cpu_to_le16(MFT_REC_EXTEND)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300432 /* Records in $Extend are not a files or general directories. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300433 } else {
434 err = -EINVAL;
435 goto out;
436 }
437
Kari Argillander564c97b2021-09-07 18:35:51 +0300438 if ((sbi->options->sys_immutable &&
Konstantin Komarov82cae262021-08-13 17:21:29 +0300439 (std5->fa & FILE_ATTRIBUTE_SYSTEM)) &&
440 !S_ISFIFO(mode) && !S_ISSOCK(mode) && !S_ISLNK(mode)) {
441 inode->i_flags |= S_IMMUTABLE;
442 } else {
443 inode->i_flags &= ~S_IMMUTABLE;
444 }
445
446 inode->i_mode = mode;
447 if (!(ni->ni_flags & NI_FLAG_EA)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300448 /* If no xattr then no security (stored in xattr). */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300449 inode->i_flags |= S_NOSEC;
450 }
451
452Ok:
453 if (ino == MFT_REC_MFT && !sb->s_root)
454 sbi->mft.ni = NULL;
455
456 unlock_new_inode(inode);
457
458 return inode;
459
460out:
461 if (ino == MFT_REC_MFT && !sb->s_root)
462 sbi->mft.ni = NULL;
463
464 iget_failed(inode);
465 return ERR_PTR(err);
466}
467
Kari Argillandere8b8e972021-08-03 14:57:09 +0300468/*
469 * ntfs_test_inode
470 *
471 * Return: 1 if match.
472 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300473static 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) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300502 /* Inode overlaps? */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300503 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
Kari Argillandere8b8e972021-08-03 14:57:09 +0300533 /* Clear previous state. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300534 clear_buffer_new(bh);
535 clear_buffer_uptodate(bh);
536
Kari Argillandere8b8e972021-08-03 14:57:09 +0300537 /* Direct write uses 'create=0'. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300538 if (!create && vbo >= ni->i_valid) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300539 /* Out of valid. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300540 return 0;
541 }
542
543 if (vbo >= inode->i_size) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300544 /* Out of size. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300545 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) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300596 /* ntfs_direct_IO will update ni->i_valid. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300597 if (vbo >= valid)
598 set_buffer_new(bh);
599 } else if (create) {
Konstantin Komarovd3624462021-08-31 16:57:40 +0300600 /* Normal write. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300601 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) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300612 /* Read out of valid data. */
613 /* Should never be here 'cause already checked. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300614 clear_buffer_mapped(bh);
615 } else if (vbo + bytes <= valid) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300616 /* Normal read. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300617 } else if (vbo + block_size <= valid) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300618 /* Normal short read. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300619 bytes = block_size;
620 } else {
621 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +0300622 * Read across valid size: vbo < valid && valid < vbo + block_size
Konstantin Komarov82cae262021-08-13 17:21:29 +0300623 */
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
Kari Argillandere8b8e972021-08-03 14:57:09 +0300703 /* Normal + sparse files. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300704 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)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300716 /* No readahead for resident. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300717 return;
718 }
719
720 if (is_compressed(ni)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300721 /* No readahead for compressed. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300722 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)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300730 /* Range cross 'valid'. Read it page by page. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300731 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)) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300764 /* Switch to buffered write. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300765 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) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300784 /* Fix page. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300785 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
Kari Argillandere8b8e972021-08-03 14:57:09 +0300800 /* Check for maximum file size. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300801 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);
Kari Argillandere8b8e972021-08-03 14:57:09 +0300851 /* Redirect call to 'ntfs_writepage' for resident files. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300852 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
Kari Argillandere8b8e972021-08-03 14:57:09 +0300904/*
905 * ntfs_write_end - Address_space_operations::write_end.
906 */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300907static int ntfs_write_end(struct file *file, struct address_space *mapping,
908 loff_t pos, u32 len, u32 copied, struct page *page,
909 void *fsdata)
910
911{
912 struct inode *inode = mapping->host;
913 struct ntfs_inode *ni = ntfs_i(inode);
914 u64 valid = ni->i_valid;
915 bool dirty = false;
916 int err;
917
918 if (is_resident(ni)) {
919 ni_lock(ni);
920 err = attr_data_write_resident(ni, page);
921 ni_unlock(ni);
922 if (!err) {
923 dirty = true;
Kari Argillandere8b8e972021-08-03 14:57:09 +0300924 /* Clear any buffers in page. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300925 if (page_has_buffers(page)) {
926 struct buffer_head *head, *bh;
927
928 bh = head = page_buffers(page);
929 do {
930 clear_buffer_dirty(bh);
931 clear_buffer_mapped(bh);
932 set_buffer_uptodate(bh);
933 } while (head != (bh = bh->b_this_page));
934 }
935 SetPageUptodate(page);
936 err = copied;
937 }
938 unlock_page(page);
939 put_page(page);
940 } else {
941 err = generic_write_end(file, mapping, pos, len, copied, page,
942 fsdata);
943 }
944
945 if (err >= 0) {
946 if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) {
947 inode->i_ctime = inode->i_mtime = current_time(inode);
948 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
949 dirty = true;
950 }
951
952 if (valid != ni->i_valid) {
Kari Argillandere8b8e972021-08-03 14:57:09 +0300953 /* ni->i_valid is changed in ntfs_get_block_vbo. */
Konstantin Komarov82cae262021-08-13 17:21:29 +0300954 dirty = true;
955 }
956
957 if (dirty)
958 mark_inode_dirty(inode);
959 }
960
961 return err;
962}
963
964int reset_log_file(struct inode *inode)
965{
966 int err;
967 loff_t pos = 0;
968 u32 log_size = inode->i_size;
969 struct address_space *mapping = inode->i_mapping;
970
971 for (;;) {
972 u32 len;
973 void *kaddr;
974 struct page *page;
975
976 len = pos + PAGE_SIZE > log_size ? (log_size - pos) : PAGE_SIZE;
977
978 err = block_write_begin(mapping, pos, len, 0, &page,
979 ntfs_get_block_write_begin);
980 if (err)
981 goto out;
982
983 kaddr = kmap_atomic(page);
984 memset(kaddr, -1, len);
985 kunmap_atomic(kaddr);
986 flush_dcache_page(page);
987
988 err = block_write_end(NULL, mapping, pos, len, len, page, NULL);
989 if (err < 0)
990 goto out;
991 pos += len;
992
993 if (pos >= log_size)
994 break;
995 balance_dirty_pages_ratelimited(mapping);
996 }
997out:
998 mark_inode_dirty_sync(inode);
999
1000 return err;
1001}
1002
1003int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc)
1004{
1005 return _ni_write_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1006}
1007
1008int ntfs_sync_inode(struct inode *inode)
1009{
1010 return _ni_write_inode(inode, 1);
1011}
1012
1013/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001014 * writeback_inode - Helper function for ntfs_flush_inodes().
1015 *
1016 * This writes both the inode and the file data blocks, waiting
1017 * for in flight data blocks before the start of the call. It
1018 * does not wait for any io started during the call.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001019 */
1020static int writeback_inode(struct inode *inode)
1021{
1022 int ret = sync_inode_metadata(inode, 0);
1023
1024 if (!ret)
1025 ret = filemap_fdatawrite(inode->i_mapping);
1026 return ret;
1027}
1028
1029/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001030 * ntfs_flush_inodes
1031 *
1032 * Write data and metadata corresponding to i1 and i2. The io is
Konstantin Komarov82cae262021-08-13 17:21:29 +03001033 * started but we do not wait for any of it to finish.
1034 *
Kari Argillandere8b8e972021-08-03 14:57:09 +03001035 * filemap_flush() is used for the block device, so if there is a dirty
Konstantin Komarov82cae262021-08-13 17:21:29 +03001036 * page for a block already in flight, we will not wait and start the
Kari Argillandere8b8e972021-08-03 14:57:09 +03001037 * io over again.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001038 */
1039int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
1040 struct inode *i2)
1041{
1042 int ret = 0;
1043
1044 if (i1)
1045 ret = writeback_inode(i1);
1046 if (!ret && i2)
1047 ret = writeback_inode(i2);
1048 if (!ret)
Christoph Hellwig680e6672021-10-19 08:25:29 +02001049 ret = sync_blockdev_nowait(sb->s_bdev);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001050 return ret;
1051}
1052
1053int inode_write_data(struct inode *inode, const void *data, size_t bytes)
1054{
1055 pgoff_t idx;
1056
Kari Argillandere8b8e972021-08-03 14:57:09 +03001057 /* Write non resident data. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001058 for (idx = 0; bytes; idx++) {
1059 size_t op = bytes > PAGE_SIZE ? PAGE_SIZE : bytes;
1060 struct page *page = ntfs_map_page(inode->i_mapping, idx);
1061
1062 if (IS_ERR(page))
1063 return PTR_ERR(page);
1064
1065 lock_page(page);
1066 WARN_ON(!PageUptodate(page));
1067 ClearPageUptodate(page);
1068
1069 memcpy(page_address(page), data, op);
1070
1071 flush_dcache_page(page);
1072 SetPageUptodate(page);
1073 unlock_page(page);
1074
1075 ntfs_unmap_page(page);
1076
1077 bytes -= op;
1078 data = Add2Ptr(data, PAGE_SIZE);
1079 }
1080 return 0;
1081}
1082
1083/*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001084 * ntfs_reparse_bytes
1085 *
Konstantin Komarovd3624462021-08-31 16:57:40 +03001086 * Number of bytes for REPARSE_DATA_BUFFER(IO_REPARSE_TAG_SYMLINK)
Kari Argillandere8b8e972021-08-03 14:57:09 +03001087 * for unicode string of @uni_len length.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001088 */
1089static inline u32 ntfs_reparse_bytes(u32 uni_len)
1090{
Kari Argillandere8b8e972021-08-03 14:57:09 +03001091 /* Header + unicode string + decorated unicode string. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001092 return sizeof(short) * (2 * uni_len + 4) +
1093 offsetof(struct REPARSE_DATA_BUFFER,
1094 SymbolicLinkReparseBuffer.PathBuffer);
1095}
1096
1097static struct REPARSE_DATA_BUFFER *
1098ntfs_create_reparse_buffer(struct ntfs_sb_info *sbi, const char *symname,
1099 u32 size, u16 *nsize)
1100{
1101 int i, err;
1102 struct REPARSE_DATA_BUFFER *rp;
1103 __le16 *rp_name;
1104 typeof(rp->SymbolicLinkReparseBuffer) *rs;
1105
Kari Argillander195c52b2021-08-24 21:37:07 +03001106 rp = kzalloc(ntfs_reparse_bytes(2 * size + 2), GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001107 if (!rp)
1108 return ERR_PTR(-ENOMEM);
1109
1110 rs = &rp->SymbolicLinkReparseBuffer;
1111 rp_name = rs->PathBuffer;
1112
Kari Argillandere8b8e972021-08-03 14:57:09 +03001113 /* Convert link name to UTF-16. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001114 err = ntfs_nls_to_utf16(sbi, symname, size,
1115 (struct cpu_str *)(rp_name - 1), 2 * size,
1116 UTF16_LITTLE_ENDIAN);
1117 if (err < 0)
1118 goto out;
1119
Kari Argillandere8b8e972021-08-03 14:57:09 +03001120 /* err = the length of unicode name of symlink. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001121 *nsize = ntfs_reparse_bytes(err);
1122
1123 if (*nsize > sbi->reparse.max_size) {
1124 err = -EFBIG;
1125 goto out;
1126 }
1127
Kari Argillandere8b8e972021-08-03 14:57:09 +03001128 /* Translate Linux '/' into Windows '\'. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001129 for (i = 0; i < err; i++) {
1130 if (rp_name[i] == cpu_to_le16('/'))
1131 rp_name[i] = cpu_to_le16('\\');
1132 }
1133
1134 rp->ReparseTag = IO_REPARSE_TAG_SYMLINK;
1135 rp->ReparseDataLength =
1136 cpu_to_le16(*nsize - offsetof(struct REPARSE_DATA_BUFFER,
1137 SymbolicLinkReparseBuffer));
1138
Kari Argillandere8b8e972021-08-03 14:57:09 +03001139 /* PrintName + SubstituteName. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001140 rs->SubstituteNameOffset = cpu_to_le16(sizeof(short) * err);
1141 rs->SubstituteNameLength = cpu_to_le16(sizeof(short) * err + 8);
1142 rs->PrintNameLength = rs->SubstituteNameOffset;
1143
1144 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001145 * TODO: Use relative path if possible to allow Windows to
1146 * parse this path.
1147 * 0-absolute path 1- relative path (SYMLINK_FLAG_RELATIVE).
Konstantin Komarov82cae262021-08-13 17:21:29 +03001148 */
1149 rs->Flags = 0;
1150
1151 memmove(rp_name + err + 4, rp_name, sizeof(short) * err);
1152
Kari Argillandere8b8e972021-08-03 14:57:09 +03001153 /* Decorate SubstituteName. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001154 rp_name += err;
1155 rp_name[0] = cpu_to_le16('\\');
1156 rp_name[1] = cpu_to_le16('?');
1157 rp_name[2] = cpu_to_le16('?');
1158 rp_name[3] = cpu_to_le16('\\');
1159
1160 return rp;
1161out:
Kari Argillander195c52b2021-08-24 21:37:07 +03001162 kfree(rp);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001163 return ERR_PTR(err);
1164}
1165
1166struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
1167 struct inode *dir, struct dentry *dentry,
1168 const struct cpu_str *uni, umode_t mode,
1169 dev_t dev, const char *symname, u32 size,
1170 struct ntfs_fnd *fnd)
1171{
1172 int err;
1173 struct super_block *sb = dir->i_sb;
1174 struct ntfs_sb_info *sbi = sb->s_fs_info;
1175 const struct qstr *name = &dentry->d_name;
1176 CLST ino = 0;
1177 struct ntfs_inode *dir_ni = ntfs_i(dir);
1178 struct ntfs_inode *ni = NULL;
1179 struct inode *inode = NULL;
1180 struct ATTRIB *attr;
1181 struct ATTR_STD_INFO5 *std5;
1182 struct ATTR_FILE_NAME *fname;
1183 struct MFT_REC *rec;
1184 u32 asize, dsize, sd_size;
1185 enum FILE_ATTRIBUTE fa;
1186 __le32 security_id = SECURITY_ID_INVALID;
1187 CLST vcn;
1188 const void *sd;
1189 u16 t16, nsize = 0, aid = 0;
1190 struct INDEX_ROOT *root, *dir_root;
1191 struct NTFS_DE *e, *new_de = NULL;
1192 struct REPARSE_DATA_BUFFER *rp = NULL;
1193 bool rp_inserted = false;
1194
Konstantin Komarovd562e902021-09-23 18:04:26 +03001195 ni_lock_dir(dir_ni);
1196
Konstantin Komarov82cae262021-08-13 17:21:29 +03001197 dir_root = indx_get_root(&dir_ni->dir, dir_ni, NULL, NULL);
Konstantin Komarovd562e902021-09-23 18:04:26 +03001198 if (!dir_root) {
1199 err = -EINVAL;
1200 goto out1;
1201 }
Konstantin Komarov82cae262021-08-13 17:21:29 +03001202
1203 if (S_ISDIR(mode)) {
Konstantin Komarovd3624462021-08-31 16:57:40 +03001204 /* Use parent's directory attributes. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001205 fa = dir_ni->std_fa | FILE_ATTRIBUTE_DIRECTORY |
1206 FILE_ATTRIBUTE_ARCHIVE;
1207 /*
Konstantin Komarovd3624462021-08-31 16:57:40 +03001208 * By default child directory inherits parent attributes.
1209 * Root directory is hidden + system.
1210 * Make an exception for children in root.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001211 */
1212 if (dir->i_ino == MFT_REC_ROOT)
1213 fa &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
1214 } else if (S_ISLNK(mode)) {
1215 /* It is good idea that link should be the same type (file/dir) as target */
1216 fa = FILE_ATTRIBUTE_REPARSE_POINT;
1217
1218 /*
Konstantin Komarovd3624462021-08-31 16:57:40 +03001219 * Linux: there are dir/file/symlink and so on.
1220 * NTFS: symlinks are "dir + reparse" or "file + reparse"
Konstantin Komarov82cae262021-08-13 17:21:29 +03001221 * It is good idea to create:
1222 * dir + reparse if 'symname' points to directory
1223 * or
1224 * file + reparse if 'symname' points to file
Kari Argillandere8b8e972021-08-03 14:57:09 +03001225 * Unfortunately kern_path hangs if symname contains 'dir'.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001226 */
1227
1228 /*
1229 * struct path path;
1230 *
1231 * if (!kern_path(symname, LOOKUP_FOLLOW, &path)){
1232 * struct inode *target = d_inode(path.dentry);
1233 *
1234 * if (S_ISDIR(target->i_mode))
1235 * fa |= FILE_ATTRIBUTE_DIRECTORY;
1236 * // if ( target->i_sb == sb ){
1237 * // use relative path?
1238 * // }
1239 * path_put(&path);
1240 * }
1241 */
1242 } else if (S_ISREG(mode)) {
Kari Argillander564c97b2021-09-07 18:35:51 +03001243 if (sbi->options->sparse) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001244 /* Sparsed regular file, cause option 'sparse'. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001245 fa = FILE_ATTRIBUTE_SPARSE_FILE |
1246 FILE_ATTRIBUTE_ARCHIVE;
1247 } else if (dir_ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001248 /* Compressed regular file, if parent is compressed. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001249 fa = FILE_ATTRIBUTE_COMPRESSED | FILE_ATTRIBUTE_ARCHIVE;
1250 } else {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001251 /* Regular file, default attributes. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001252 fa = FILE_ATTRIBUTE_ARCHIVE;
1253 }
1254 } else {
1255 fa = FILE_ATTRIBUTE_ARCHIVE;
1256 }
1257
1258 if (!(mode & 0222))
1259 fa |= FILE_ATTRIBUTE_READONLY;
1260
Kari Argillandere8b8e972021-08-03 14:57:09 +03001261 /* Allocate PATH_MAX bytes. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001262 new_de = __getname();
1263 if (!new_de) {
1264 err = -ENOMEM;
1265 goto out1;
1266 }
1267
Kari Argillandere8b8e972021-08-03 14:57:09 +03001268 /* Mark rw ntfs as dirty. it will be cleared at umount. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001269 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
1270
Kari Argillandere8b8e972021-08-03 14:57:09 +03001271 /* Step 1: allocate and fill new mft record. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001272 err = ntfs_look_free_mft(sbi, &ino, false, NULL, NULL);
1273 if (err)
1274 goto out2;
1275
1276 ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1277 if (IS_ERR(ni)) {
1278 err = PTR_ERR(ni);
1279 ni = NULL;
1280 goto out3;
1281 }
1282 inode = &ni->vfs_inode;
1283 inode_init_owner(mnt_userns, inode, dir, mode);
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001284 mode = inode->i_mode;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001285
1286 inode->i_atime = inode->i_mtime = inode->i_ctime = ni->i_crtime =
1287 current_time(inode);
1288
1289 rec = ni->mi.mrec;
1290 rec->hard_links = cpu_to_le16(1);
1291 attr = Add2Ptr(rec, le16_to_cpu(rec->attr_off));
1292
Kari Argillandere8b8e972021-08-03 14:57:09 +03001293 /* Get default security id. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001294 sd = s_default_security;
1295 sd_size = sizeof(s_default_security);
1296
1297 if (is_ntfs3(sbi)) {
1298 security_id = dir_ni->std_security_id;
1299 if (le32_to_cpu(security_id) < SECURITY_ID_FIRST) {
1300 security_id = sbi->security.def_security_id;
1301
1302 if (security_id == SECURITY_ID_INVALID &&
1303 !ntfs_insert_security(sbi, sd, sd_size,
1304 &security_id, NULL))
1305 sbi->security.def_security_id = security_id;
1306 }
1307 }
1308
Kari Argillandere8b8e972021-08-03 14:57:09 +03001309 /* Insert standard info. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001310 std5 = Add2Ptr(attr, SIZEOF_RESIDENT);
1311
1312 if (security_id == SECURITY_ID_INVALID) {
1313 dsize = sizeof(struct ATTR_STD_INFO);
1314 } else {
1315 dsize = sizeof(struct ATTR_STD_INFO5);
1316 std5->security_id = security_id;
1317 ni->std_security_id = security_id;
1318 }
1319 asize = SIZEOF_RESIDENT + dsize;
1320
1321 attr->type = ATTR_STD;
1322 attr->size = cpu_to_le32(asize);
1323 attr->id = cpu_to_le16(aid++);
1324 attr->res.data_off = SIZEOF_RESIDENT_LE;
1325 attr->res.data_size = cpu_to_le32(dsize);
1326
1327 std5->cr_time = std5->m_time = std5->c_time = std5->a_time =
1328 kernel2nt(&inode->i_atime);
1329
1330 ni->std_fa = fa;
1331 std5->fa = fa;
1332
1333 attr = Add2Ptr(attr, asize);
1334
Kari Argillandere8b8e972021-08-03 14:57:09 +03001335 /* Insert file name. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001336 err = fill_name_de(sbi, new_de, name, uni);
1337 if (err)
1338 goto out4;
1339
1340 mi_get_ref(&ni->mi, &new_de->ref);
1341
1342 fname = (struct ATTR_FILE_NAME *)(new_de + 1);
1343 mi_get_ref(&dir_ni->mi, &fname->home);
1344 fname->dup.cr_time = fname->dup.m_time = fname->dup.c_time =
1345 fname->dup.a_time = std5->cr_time;
1346 fname->dup.alloc_size = fname->dup.data_size = 0;
1347 fname->dup.fa = std5->fa;
1348 fname->dup.ea_size = fname->dup.reparse = 0;
1349
1350 dsize = le16_to_cpu(new_de->key_size);
Kari Argillanderfa3cacf2021-08-26 11:56:29 +03001351 asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001352
1353 attr->type = ATTR_NAME;
1354 attr->size = cpu_to_le32(asize);
1355 attr->res.data_off = SIZEOF_RESIDENT_LE;
1356 attr->res.flags = RESIDENT_FLAG_INDEXED;
1357 attr->id = cpu_to_le16(aid++);
1358 attr->res.data_size = cpu_to_le32(dsize);
1359 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), fname, dsize);
1360
1361 attr = Add2Ptr(attr, asize);
1362
1363 if (security_id == SECURITY_ID_INVALID) {
Kari Argillandere8b8e972021-08-03 14:57:09 +03001364 /* Insert security attribute. */
Kari Argillanderfa3cacf2021-08-26 11:56:29 +03001365 asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001366
1367 attr->type = ATTR_SECURE;
1368 attr->size = cpu_to_le32(asize);
1369 attr->id = cpu_to_le16(aid++);
1370 attr->res.data_off = SIZEOF_RESIDENT_LE;
1371 attr->res.data_size = cpu_to_le32(sd_size);
1372 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), sd, sd_size);
1373
1374 attr = Add2Ptr(attr, asize);
1375 }
1376
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001377 attr->id = cpu_to_le16(aid++);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001378 if (fa & FILE_ATTRIBUTE_DIRECTORY) {
1379 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001380 * Regular directory or symlink to directory.
1381 * Create root attribute.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001382 */
1383 dsize = sizeof(struct INDEX_ROOT) + sizeof(struct NTFS_DE);
1384 asize = sizeof(I30_NAME) + SIZEOF_RESIDENT + dsize;
1385
1386 attr->type = ATTR_ROOT;
1387 attr->size = cpu_to_le32(asize);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001388
1389 attr->name_len = ARRAY_SIZE(I30_NAME);
1390 attr->name_off = SIZEOF_RESIDENT_LE;
1391 attr->res.data_off =
1392 cpu_to_le16(sizeof(I30_NAME) + SIZEOF_RESIDENT);
1393 attr->res.data_size = cpu_to_le32(dsize);
1394 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), I30_NAME,
1395 sizeof(I30_NAME));
1396
1397 root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
1398 memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1399 root->ihdr.de_off =
1400 cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1401 root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
1402 sizeof(struct NTFS_DE));
1403 root->ihdr.total = root->ihdr.used;
1404
1405 e = Add2Ptr(root, sizeof(struct INDEX_ROOT));
1406 e->size = cpu_to_le16(sizeof(struct NTFS_DE));
1407 e->flags = NTFS_IE_LAST;
1408 } else if (S_ISLNK(mode)) {
1409 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001410 * Symlink to file.
1411 * Create empty resident data attribute.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001412 */
1413 asize = SIZEOF_RESIDENT;
1414
Kari Argillandere8b8e972021-08-03 14:57:09 +03001415 /* Insert empty ATTR_DATA */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001416 attr->type = ATTR_DATA;
1417 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001418 attr->name_off = SIZEOF_RESIDENT_LE;
1419 attr->res.data_off = SIZEOF_RESIDENT_LE;
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001420 } else if (S_ISREG(mode)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001421 /*
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001422 * Regular file. Create empty non resident data attribute.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001423 */
1424 attr->type = ATTR_DATA;
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001425 attr->non_res = 1;
1426 attr->nres.evcn = cpu_to_le64(-1ll);
1427 if (fa & FILE_ATTRIBUTE_SPARSE_FILE) {
1428 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1429 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1430 attr->flags = ATTR_FLAG_SPARSED;
1431 asize = SIZEOF_NONRESIDENT_EX + 8;
1432 } else if (fa & FILE_ATTRIBUTE_COMPRESSED) {
1433 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT_EX + 8);
1434 attr->name_off = SIZEOF_NONRESIDENT_EX_LE;
1435 attr->flags = ATTR_FLAG_COMPRESSED;
1436 attr->nres.c_unit = COMPRESSION_UNIT;
1437 asize = SIZEOF_NONRESIDENT_EX + 8;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001438 } else {
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001439 attr->size = cpu_to_le32(SIZEOF_NONRESIDENT + 8);
1440 attr->name_off = SIZEOF_NONRESIDENT_LE;
1441 asize = SIZEOF_NONRESIDENT + 8;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001442 }
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001443 attr->nres.run_off = attr->name_off;
1444 } else {
1445 /*
1446 * Node. Create empty resident data attribute.
1447 */
1448 attr->type = ATTR_DATA;
1449 attr->size = cpu_to_le32(SIZEOF_RESIDENT);
1450 attr->name_off = SIZEOF_RESIDENT_LE;
1451 if (fa & FILE_ATTRIBUTE_SPARSE_FILE)
1452 attr->flags = ATTR_FLAG_SPARSED;
1453 else if (fa & FILE_ATTRIBUTE_COMPRESSED)
1454 attr->flags = ATTR_FLAG_COMPRESSED;
1455 attr->res.data_off = SIZEOF_RESIDENT_LE;
1456 asize = SIZEOF_RESIDENT;
1457 ni->ni_flags |= NI_FLAG_RESIDENT;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001458 }
1459
1460 if (S_ISDIR(mode)) {
1461 ni->ni_flags |= NI_FLAG_DIR;
1462 err = indx_init(&ni->dir, sbi, attr, INDEX_MUTEX_I30);
1463 if (err)
1464 goto out4;
1465 } else if (S_ISLNK(mode)) {
1466 rp = ntfs_create_reparse_buffer(sbi, symname, size, &nsize);
1467
1468 if (IS_ERR(rp)) {
1469 err = PTR_ERR(rp);
1470 rp = NULL;
1471 goto out4;
1472 }
1473
1474 /*
Kari Argillandere8b8e972021-08-03 14:57:09 +03001475 * Insert ATTR_REPARSE.
Konstantin Komarov82cae262021-08-13 17:21:29 +03001476 */
1477 attr = Add2Ptr(attr, asize);
1478 attr->type = ATTR_REPARSE;
1479 attr->id = cpu_to_le16(aid++);
1480
Kari Argillandere8b8e972021-08-03 14:57:09 +03001481 /* Resident or non resident? */
Kari Argillanderfa3cacf2021-08-26 11:56:29 +03001482 asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001483 t16 = PtrOffset(rec, attr);
1484
Konstantin Komarov14a98112021-10-04 18:52:24 +03001485 /*
1486 * Below function 'ntfs_save_wsl_perm' requires 0x78 bytes.
1487 * It is good idea to keep extened attributes resident.
1488 */
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001489 if (asize + t16 + 0x78 + 8 > sbi->record_size) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001490 CLST alen;
1491 CLST clst = bytes_to_cluster(sbi, nsize);
1492
Kari Argillandere8b8e972021-08-03 14:57:09 +03001493 /* Bytes per runs. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001494 t16 = sbi->record_size - t16 - SIZEOF_NONRESIDENT;
1495
1496 attr->non_res = 1;
1497 attr->nres.evcn = cpu_to_le64(clst - 1);
1498 attr->name_off = SIZEOF_NONRESIDENT_LE;
1499 attr->nres.run_off = attr->name_off;
1500 attr->nres.data_size = cpu_to_le64(nsize);
1501 attr->nres.valid_size = attr->nres.data_size;
1502 attr->nres.alloc_size =
1503 cpu_to_le64(ntfs_up_cluster(sbi, nsize));
1504
1505 err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0,
1506 clst, NULL, 0, &alen, 0,
1507 NULL);
1508 if (err)
1509 goto out5;
1510
1511 err = run_pack(&ni->file.run, 0, clst,
1512 Add2Ptr(attr, SIZEOF_NONRESIDENT), t16,
1513 &vcn);
1514 if (err < 0)
1515 goto out5;
1516
1517 if (vcn != clst) {
1518 err = -EINVAL;
1519 goto out5;
1520 }
1521
Kari Argillanderfa3cacf2021-08-26 11:56:29 +03001522 asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001523 } else {
1524 attr->res.data_off = SIZEOF_RESIDENT_LE;
1525 attr->res.data_size = cpu_to_le32(nsize);
1526 memcpy(Add2Ptr(attr, SIZEOF_RESIDENT), rp, nsize);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001527 nsize = 0;
1528 }
Konstantin Komarov14a98112021-10-04 18:52:24 +03001529 /* Size of symlink equals the length of input string. */
1530 inode->i_size = size;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001531
1532 attr->size = cpu_to_le32(asize);
1533
1534 err = ntfs_insert_reparse(sbi, IO_REPARSE_TAG_SYMLINK,
1535 &new_de->ref);
1536 if (err)
1537 goto out5;
1538
1539 rp_inserted = true;
1540 }
1541
1542 attr = Add2Ptr(attr, asize);
1543 attr->type = ATTR_END;
1544
1545 rec->used = cpu_to_le32(PtrOffset(rec, attr) + 8);
1546 rec->next_attr_id = cpu_to_le16(aid);
1547
Kari Argillandere8b8e972021-08-03 14:57:09 +03001548 /* Step 2: Add new name in index. */
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001549 err = indx_insert_entry(&dir_ni->dir, dir_ni, new_de, sbi, fnd, 0);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001550 if (err)
1551 goto out6;
1552
Konstantin Komarovd562e902021-09-23 18:04:26 +03001553 /* Unlock parent directory before ntfs_init_acl. */
1554 ni_unlock(dir_ni);
1555
Konstantin Komarov82cae262021-08-13 17:21:29 +03001556 inode->i_generation = le16_to_cpu(rec->seq);
1557
1558 dir->i_mtime = dir->i_ctime = inode->i_atime;
1559
1560 if (S_ISDIR(mode)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001561 inode->i_op = &ntfs_dir_inode_operations;
1562 inode->i_fop = &ntfs_dir_operations;
1563 } else if (S_ISLNK(mode)) {
1564 inode->i_op = &ntfs_link_inode_operations;
1565 inode->i_fop = NULL;
1566 inode->i_mapping->a_ops = &ntfs_aops;
Konstantin Komarov14a98112021-10-04 18:52:24 +03001567 inode->i_size = size;
1568 inode_nohighmem(inode);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001569 } else if (S_ISREG(mode)) {
1570 inode->i_op = &ntfs_file_inode_operations;
1571 inode->i_fop = &ntfs_file_operations;
1572 inode->i_mapping->a_ops =
1573 is_compressed(ni) ? &ntfs_aops_cmpr : &ntfs_aops;
1574 init_rwsem(&ni->file.run_lock);
1575 } else {
1576 inode->i_op = &ntfs_special_inode_operations;
1577 init_special_inode(inode, mode, dev);
1578 }
1579
1580#ifdef CONFIG_NTFS3_FS_POSIX_ACL
1581 if (!S_ISLNK(mode) && (sb->s_flags & SB_POSIXACL)) {
1582 err = ntfs_init_acl(mnt_userns, inode, dir);
1583 if (err)
Konstantin Komarov6c1ee4d2021-09-23 18:05:36 +03001584 goto out7;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001585 } else
1586#endif
1587 {
1588 inode->i_flags |= S_NOSEC;
1589 }
1590
Kari Argillandere8b8e972021-08-03 14:57:09 +03001591 /* Write non resident data. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001592 if (nsize) {
Konstantin Komarov63544672021-09-09 13:15:20 +03001593 err = ntfs_sb_write_run(sbi, &ni->file.run, 0, rp, nsize, 0);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001594 if (err)
1595 goto out7;
1596 }
1597
Kari Argillandere8b8e972021-08-03 14:57:09 +03001598 /*
1599 * Call 'd_instantiate' after inode->i_op is set
1600 * but before finish_open.
1601 */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001602 d_instantiate(dentry, inode);
1603
1604 ntfs_save_wsl_perm(inode);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001605 mark_inode_dirty(dir);
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001606 mark_inode_dirty(inode);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001607
Kari Argillandere8b8e972021-08-03 14:57:09 +03001608 /* Normal exit. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001609 goto out2;
1610
1611out7:
1612
Kari Argillandere8b8e972021-08-03 14:57:09 +03001613 /* Undo 'indx_insert_entry'. */
Konstantin Komarovd562e902021-09-23 18:04:26 +03001614 ni_lock_dir(dir_ni);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001615 indx_delete_entry(&dir_ni->dir, dir_ni, new_de + 1,
1616 le16_to_cpu(new_de->key_size), sbi);
Konstantin Komarovd562e902021-09-23 18:04:26 +03001617 /* ni_unlock(dir_ni); will be called later. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001618out6:
1619 if (rp_inserted)
1620 ntfs_remove_reparse(sbi, IO_REPARSE_TAG_SYMLINK, &new_de->ref);
1621
1622out5:
1623 if (S_ISDIR(mode) || run_is_empty(&ni->file.run))
1624 goto out4;
1625
1626 run_deallocate(sbi, &ni->file.run, false);
1627
1628out4:
1629 clear_rec_inuse(rec);
1630 clear_nlink(inode);
1631 ni->mi.dirty = false;
1632 discard_new_inode(inode);
1633out3:
1634 ntfs_mark_rec_free(sbi, ino);
1635
1636out2:
1637 __putname(new_de);
Kari Argillander195c52b2021-08-24 21:37:07 +03001638 kfree(rp);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001639
1640out1:
Konstantin Komarovd562e902021-09-23 18:04:26 +03001641 if (err) {
1642 ni_unlock(dir_ni);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001643 return ERR_PTR(err);
Konstantin Komarovd562e902021-09-23 18:04:26 +03001644 }
Konstantin Komarov82cae262021-08-13 17:21:29 +03001645
1646 unlock_new_inode(inode);
1647
1648 return inode;
1649}
1650
1651int ntfs_link_inode(struct inode *inode, struct dentry *dentry)
1652{
1653 int err;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001654 struct ntfs_inode *ni = ntfs_i(inode);
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001655 struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
1656 struct NTFS_DE *de;
1657 struct ATTR_FILE_NAME *de_name;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001658
Kari Argillandere8b8e972021-08-03 14:57:09 +03001659 /* Allocate PATH_MAX bytes. */
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001660 de = __getname();
1661 if (!de)
Konstantin Komarov82cae262021-08-13 17:21:29 +03001662 return -ENOMEM;
1663
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001664 /* Mark rw ntfs as dirty. It will be cleared at umount. */
1665 ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001666
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001667 /* Construct 'de'. */
1668 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001669 if (err)
1670 goto out;
1671
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001672 de_name = (struct ATTR_FILE_NAME *)(de + 1);
1673 /* Fill duplicate info. */
1674 de_name->dup.cr_time = de_name->dup.m_time = de_name->dup.c_time =
1675 de_name->dup.a_time = kernel2nt(&inode->i_ctime);
1676 de_name->dup.alloc_size = de_name->dup.data_size =
1677 cpu_to_le64(inode->i_size);
1678 de_name->dup.fa = ni->std_fa;
1679 de_name->dup.ea_size = de_name->dup.reparse = 0;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001680
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001681 err = ni_add_name(ntfs_i(d_inode(dentry->d_parent)), ni, de);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001682out:
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001683 __putname(de);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001684 return err;
1685}
1686
1687/*
1688 * ntfs_unlink_inode
1689 *
1690 * inode_operations::unlink
1691 * inode_operations::rmdir
1692 */
1693int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry)
1694{
1695 int err;
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001696 struct ntfs_sb_info *sbi = dir->i_sb->s_fs_info;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001697 struct inode *inode = d_inode(dentry);
1698 struct ntfs_inode *ni = ntfs_i(inode);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001699 struct ntfs_inode *dir_ni = ntfs_i(dir);
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001700 struct NTFS_DE *de, *de2 = NULL;
1701 int undo_remove;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001702
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001703 if (ntfs_is_meta_file(sbi, ni->mi.rno))
Konstantin Komarov82cae262021-08-13 17:21:29 +03001704 return -EINVAL;
1705
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001706 /* Allocate PATH_MAX bytes. */
1707 de = __getname();
1708 if (!de)
1709 return -ENOMEM;
1710
Konstantin Komarov82cae262021-08-13 17:21:29 +03001711 ni_lock(ni);
1712
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001713 if (S_ISDIR(inode->i_mode) && !dir_is_empty(inode)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001714 err = -ENOTEMPTY;
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001715 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001716 }
1717
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001718 err = fill_name_de(sbi, de, &dentry->d_name, NULL);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001719 if (err < 0)
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001720 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001721
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001722 undo_remove = 0;
1723 err = ni_remove_name(dir_ni, ni, de, &de2, &undo_remove);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001724
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001725 if (!err) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001726 drop_nlink(inode);
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001727 dir->i_mtime = dir->i_ctime = current_time(dir);
1728 mark_inode_dirty(dir);
1729 inode->i_ctime = dir->i_ctime;
1730 if (inode->i_nlink)
1731 mark_inode_dirty(inode);
1732 } else if (!ni_remove_name_undo(dir_ni, ni, de, de2, undo_remove)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001733 make_bad_inode(inode);
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001734 ntfs_inode_err(inode, "failed to undo unlink");
1735 ntfs_set_state(sbi, NTFS_DIRTY_ERROR);
1736 } else {
1737 if (ni_is_dirty(dir))
1738 mark_inode_dirty(dir);
1739 if (ni_is_dirty(inode))
1740 mark_inode_dirty(inode);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001741 }
1742
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001743out:
Konstantin Komarov82cae262021-08-13 17:21:29 +03001744 ni_unlock(ni);
Konstantin Komarov78ab59f2021-08-31 18:52:39 +03001745 __putname(de);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001746 return err;
1747}
1748
1749void ntfs_evict_inode(struct inode *inode)
1750{
1751 truncate_inode_pages_final(&inode->i_data);
1752
1753 if (inode->i_nlink)
1754 _ni_write_inode(inode, inode_needs_sync(inode));
1755
1756 invalidate_inode_buffers(inode);
1757 clear_inode(inode);
1758
1759 ni_clear(ntfs_i(inode));
1760}
1761
1762static noinline int ntfs_readlink_hlp(struct inode *inode, char *buffer,
1763 int buflen)
1764{
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001765 int i, err = -EINVAL;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001766 struct ntfs_inode *ni = ntfs_i(inode);
1767 struct super_block *sb = inode->i_sb;
1768 struct ntfs_sb_info *sbi = sb->s_fs_info;
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001769 u64 size;
1770 u16 ulen = 0;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001771 void *to_free = NULL;
1772 struct REPARSE_DATA_BUFFER *rp;
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001773 const __le16 *uname;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001774 struct ATTRIB *attr;
1775
Kari Argillandere8b8e972021-08-03 14:57:09 +03001776 /* Reparse data present. Try to parse it. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001777 static_assert(!offsetof(struct REPARSE_DATA_BUFFER, ReparseTag));
1778 static_assert(sizeof(u32) == sizeof(rp->ReparseTag));
1779
1780 *buffer = 0;
1781
Konstantin Komarov82cae262021-08-13 17:21:29 +03001782 attr = ni_find_attr(ni, NULL, NULL, ATTR_REPARSE, NULL, 0, NULL, NULL);
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001783 if (!attr)
Konstantin Komarov82cae262021-08-13 17:21:29 +03001784 goto out;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001785
1786 if (!attr->non_res) {
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001787 rp = resident_data_ex(attr, sizeof(struct REPARSE_DATA_BUFFER));
1788 if (!rp)
Konstantin Komarov82cae262021-08-13 17:21:29 +03001789 goto out;
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001790 size = le32_to_cpu(attr->res.data_size);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001791 } else {
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001792 size = le64_to_cpu(attr->nres.data_size);
1793 rp = NULL;
1794 }
1795
1796 if (size > sbi->reparse.max_size || size <= sizeof(u32))
1797 goto out;
1798
1799 if (!rp) {
1800 rp = kmalloc(size, GFP_NOFS);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001801 if (!rp) {
1802 err = -ENOMEM;
1803 goto out;
1804 }
1805 to_free = rp;
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001806 /* Read into temporal buffer. */
1807 err = ntfs_read_run_nb(sbi, &ni->file.run, 0, rp, size, NULL);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001808 if (err)
1809 goto out;
1810 }
1811
Kari Argillandere8b8e972021-08-03 14:57:09 +03001812 /* Microsoft Tag. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001813 switch (rp->ReparseTag) {
1814 case IO_REPARSE_TAG_MOUNT_POINT:
Kari Argillandere8b8e972021-08-03 14:57:09 +03001815 /* Mount points and junctions. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001816 /* Can we use 'Rp->MountPointReparseBuffer.PrintNameLength'? */
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001817 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1818 MountPointReparseBuffer.PathBuffer))
Konstantin Komarov82cae262021-08-13 17:21:29 +03001819 goto out;
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001820 uname = Add2Ptr(rp,
1821 offsetof(struct REPARSE_DATA_BUFFER,
1822 MountPointReparseBuffer.PathBuffer) +
1823 le16_to_cpu(rp->MountPointReparseBuffer
1824 .PrintNameOffset));
1825 ulen = le16_to_cpu(rp->MountPointReparseBuffer.PrintNameLength);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001826 break;
1827
1828 case IO_REPARSE_TAG_SYMLINK:
1829 /* FolderSymbolicLink */
1830 /* Can we use 'Rp->SymbolicLinkReparseBuffer.PrintNameLength'? */
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001831 if (size <= offsetof(struct REPARSE_DATA_BUFFER,
1832 SymbolicLinkReparseBuffer.PathBuffer))
Konstantin Komarov82cae262021-08-13 17:21:29 +03001833 goto out;
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001834 uname = Add2Ptr(
1835 rp, offsetof(struct REPARSE_DATA_BUFFER,
1836 SymbolicLinkReparseBuffer.PathBuffer) +
1837 le16_to_cpu(rp->SymbolicLinkReparseBuffer
1838 .PrintNameOffset));
1839 ulen = le16_to_cpu(
Konstantin Komarov82cae262021-08-13 17:21:29 +03001840 rp->SymbolicLinkReparseBuffer.PrintNameLength);
1841 break;
1842
1843 case IO_REPARSE_TAG_CLOUD:
1844 case IO_REPARSE_TAG_CLOUD_1:
1845 case IO_REPARSE_TAG_CLOUD_2:
1846 case IO_REPARSE_TAG_CLOUD_3:
1847 case IO_REPARSE_TAG_CLOUD_4:
1848 case IO_REPARSE_TAG_CLOUD_5:
1849 case IO_REPARSE_TAG_CLOUD_6:
1850 case IO_REPARSE_TAG_CLOUD_7:
1851 case IO_REPARSE_TAG_CLOUD_8:
1852 case IO_REPARSE_TAG_CLOUD_9:
1853 case IO_REPARSE_TAG_CLOUD_A:
1854 case IO_REPARSE_TAG_CLOUD_B:
1855 case IO_REPARSE_TAG_CLOUD_C:
1856 case IO_REPARSE_TAG_CLOUD_D:
1857 case IO_REPARSE_TAG_CLOUD_E:
1858 case IO_REPARSE_TAG_CLOUD_F:
1859 err = sizeof("OneDrive") - 1;
1860 if (err > buflen)
1861 err = buflen;
1862 memcpy(buffer, "OneDrive", err);
1863 goto out;
1864
1865 default:
1866 if (IsReparseTagMicrosoft(rp->ReparseTag)) {
Konstantin Komarovd3624462021-08-31 16:57:40 +03001867 /* Unknown Microsoft Tag. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001868 goto out;
1869 }
1870 if (!IsReparseTagNameSurrogate(rp->ReparseTag) ||
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001871 size <= sizeof(struct REPARSE_POINT)) {
Konstantin Komarov82cae262021-08-13 17:21:29 +03001872 goto out;
1873 }
1874
Kari Argillandere8b8e972021-08-03 14:57:09 +03001875 /* Users tag. */
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001876 uname = Add2Ptr(rp, sizeof(struct REPARSE_POINT));
1877 ulen = le16_to_cpu(rp->ReparseDataLength) -
Konstantin Komarov82cae262021-08-13 17:21:29 +03001878 sizeof(struct REPARSE_POINT);
1879 }
1880
Kari Argillandere8b8e972021-08-03 14:57:09 +03001881 /* Convert nlen from bytes to UNICODE chars. */
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001882 ulen >>= 1;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001883
Kari Argillandere8b8e972021-08-03 14:57:09 +03001884 /* Check that name is available. */
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001885 if (!ulen || uname + ulen > (__le16 *)Add2Ptr(rp, size))
Konstantin Komarov82cae262021-08-13 17:21:29 +03001886 goto out;
1887
Kari Argillandere8b8e972021-08-03 14:57:09 +03001888 /* If name is already zero terminated then truncate it now. */
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001889 if (!uname[ulen - 1])
1890 ulen -= 1;
Konstantin Komarov82cae262021-08-13 17:21:29 +03001891
Konstantin Komarov4dbe8e42021-10-04 18:59:55 +03001892 err = ntfs_utf16_to_nls(sbi, uname, ulen, buffer, buflen);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001893
1894 if (err < 0)
1895 goto out;
1896
Kari Argillandere8b8e972021-08-03 14:57:09 +03001897 /* Translate Windows '\' into Linux '/'. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001898 for (i = 0; i < err; i++) {
1899 if (buffer[i] == '\\')
1900 buffer[i] = '/';
1901 }
1902
Kari Argillandere8b8e972021-08-03 14:57:09 +03001903 /* Always set last zero. */
Konstantin Komarov82cae262021-08-13 17:21:29 +03001904 buffer[err] = 0;
1905out:
Kari Argillander195c52b2021-08-24 21:37:07 +03001906 kfree(to_free);
Konstantin Komarov82cae262021-08-13 17:21:29 +03001907 return err;
1908}
1909
1910static const char *ntfs_get_link(struct dentry *de, struct inode *inode,
1911 struct delayed_call *done)
1912{
1913 int err;
1914 char *ret;
1915
1916 if (!de)
1917 return ERR_PTR(-ECHILD);
1918
1919 ret = kmalloc(PAGE_SIZE, GFP_NOFS);
1920 if (!ret)
1921 return ERR_PTR(-ENOMEM);
1922
1923 err = ntfs_readlink_hlp(inode, ret, PAGE_SIZE);
1924 if (err < 0) {
1925 kfree(ret);
1926 return ERR_PTR(err);
1927 }
1928
1929 set_delayed_call(done, kfree_link, ret);
1930
1931 return ret;
1932}
1933
1934// clang-format off
1935const struct inode_operations ntfs_link_inode_operations = {
1936 .get_link = ntfs_get_link,
1937 .setattr = ntfs3_setattr,
1938 .listxattr = ntfs_listxattr,
1939 .permission = ntfs_permission,
1940 .get_acl = ntfs_get_acl,
1941 .set_acl = ntfs_set_acl,
1942};
1943
1944const struct address_space_operations ntfs_aops = {
1945 .readpage = ntfs_readpage,
1946 .readahead = ntfs_readahead,
1947 .writepage = ntfs_writepage,
1948 .writepages = ntfs_writepages,
1949 .write_begin = ntfs_write_begin,
1950 .write_end = ntfs_write_end,
1951 .direct_IO = ntfs_direct_IO,
1952 .bmap = ntfs_bmap,
1953 .set_page_dirty = __set_page_dirty_buffers,
1954};
1955
1956const struct address_space_operations ntfs_aops_cmpr = {
1957 .readpage = ntfs_readpage,
1958 .readahead = ntfs_readahead,
1959};
1960// clang-format on