xref: /linux/fs/ntfs/ea.c (revision cdd4dc3aebeab43a72ce0bc2b5bab6f0a80b97a5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Pocessing of EA's
4  *
5  * Part of this file is based on code from the NTFS-3G.
6  *
7  * Copyright (c) 2014-2021 Jean-Pierre Andre
8  * Copyright (c) 2025 LG Electronics Co., Ltd.
9  */
10 
11 #include <linux/fs.h>
12 #include <linux/posix_acl.h>
13 #include <linux/posix_acl_xattr.h>
14 #include <linux/xattr.h>
15 
16 #include "layout.h"
17 #include "attrib.h"
18 #include "index.h"
19 #include "dir.h"
20 #include "ea.h"
21 
22 static int ntfs_write_ea(struct ntfs_inode *ni, __le32 type, char *value, s64 ea_off,
23 		s64 ea_size, bool need_truncate)
24 {
25 	struct inode *ea_vi;
26 	int err = 0;
27 	s64 written;
28 
29 	ea_vi = ntfs_attr_iget(VFS_I(ni), type, AT_UNNAMED, 0);
30 	if (IS_ERR(ea_vi))
31 		return PTR_ERR(ea_vi);
32 
33 	written = ntfs_inode_attr_pwrite(ea_vi, ea_off, ea_size, value, false);
34 	if (written != ea_size)
35 		err = -EIO;
36 	else {
37 		struct ntfs_inode *ea_ni = NTFS_I(ea_vi);
38 
39 		if (need_truncate && ea_ni->data_size > ea_off + ea_size)
40 			ntfs_attr_truncate(ea_ni, ea_off + ea_size);
41 		mark_mft_record_dirty(ni);
42 	}
43 
44 	iput(ea_vi);
45 	return err;
46 }
47 
48 static int ntfs_ea_lookup(char *ea_buf, s64 ea_buf_size, const char *name,
49 			  int name_len, s64 *ea_offset, s64 *ea_size)
50 {
51 	const struct ea_attr *p_ea;
52 	size_t actual_size;
53 	loff_t offset, p_ea_size;
54 	unsigned int next;
55 
56 	if (ea_buf_size < sizeof(struct ea_attr))
57 		goto out;
58 
59 	offset = 0;
60 	do {
61 		p_ea = (const struct ea_attr *)&ea_buf[offset];
62 		next = le32_to_cpu(p_ea->next_entry_offset);
63 		p_ea_size = next ? next : (ea_buf_size - offset);
64 
65 		if (p_ea_size < sizeof(struct ea_attr) ||
66 		    offset + p_ea_size > ea_buf_size)
67 			break;
68 
69 		if ((s64)p_ea->ea_name_length + 1 >
70 		    p_ea_size - offsetof(struct ea_attr, ea_name))
71 			break;
72 
73 		actual_size = ALIGN(struct_size(p_ea, ea_name, 1 + p_ea->ea_name_length +
74 					le16_to_cpu(p_ea->ea_value_length)), 4);
75 		if (actual_size > p_ea_size)
76 			break;
77 
78 		if (p_ea->ea_name_length == name_len &&
79 		    !memcmp(p_ea->ea_name, name, name_len)) {
80 			*ea_offset = offset;
81 			*ea_size = next ? next : actual_size;
82 
83 			if (ea_buf_size < *ea_offset + *ea_size)
84 				goto out;
85 
86 			return 0;
87 		}
88 		offset += next;
89 	} while (next > 0 && offset < ea_buf_size);
90 
91 out:
92 	return -ENOENT;
93 }
94 
95 /*
96  * Return the existing EA
97  *
98  * The EA_INFORMATION is not examined and the consistency of the
99  * existing EA is not checked.
100  *
101  * If successful, the full attribute is returned unchanged
102  * and its size is returned.
103  * If the designated buffer is too small, the needed size is
104  * returned, and the buffer is left unchanged.
105  * If there is an error, a negative value is returned and errno
106  * is set according to the error.
107  */
108 static int ntfs_get_ea(struct inode *inode, const char *name, size_t name_len,
109 		void *buffer, size_t size)
110 {
111 	struct ntfs_inode *ni = NTFS_I(inode);
112 	const struct ea_attr *p_ea;
113 	char *ea_buf;
114 	s64 ea_off, ea_size, all_ea_size, ea_info_size;
115 	int err;
116 	u32 ea_info_qlen;
117 	u16 ea_value_len;
118 	struct ea_information *p_ea_info;
119 
120 	if (!NInoHasEA(ni))
121 		return -ENODATA;
122 
123 	p_ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0,
124 			&ea_info_size);
125 	if (!p_ea_info || ea_info_size != sizeof(struct ea_information)) {
126 		kvfree(p_ea_info);
127 		return -ENODATA;
128 	}
129 
130 	ea_info_qlen = le32_to_cpu(p_ea_info->ea_query_length);
131 	kvfree(p_ea_info);
132 
133 	ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size);
134 	if (!ea_buf)
135 		return -ENODATA;
136 
137 	if (ea_info_qlen > all_ea_size) {
138 		err = -EIO;
139 		goto free_ea_buf;
140 	}
141 
142 	err = ntfs_ea_lookup(ea_buf, ea_info_qlen, name, name_len, &ea_off,
143 			&ea_size);
144 	if (!err) {
145 		p_ea = (struct ea_attr *)&ea_buf[ea_off];
146 		ea_value_len = le16_to_cpu(p_ea->ea_value_length);
147 		if (!buffer) {
148 			kvfree(ea_buf);
149 			return ea_value_len;
150 		}
151 
152 		if (ea_value_len > size) {
153 			err = -ERANGE;
154 			goto free_ea_buf;
155 		}
156 
157 		memcpy(buffer, &p_ea->ea_name[p_ea->ea_name_length + 1],
158 				ea_value_len);
159 		kvfree(ea_buf);
160 		return ea_value_len;
161 	}
162 
163 	err = -ENODATA;
164 free_ea_buf:
165 	kvfree(ea_buf);
166 	return err;
167 }
168 
169 static inline int ea_packed_size(const struct ea_attr *p_ea)
170 {
171 	/*
172 	 * 4 bytes for header (flags and lengths) + name length + 1 +
173 	 * value length.
174 	 */
175 	return 5 + p_ea->ea_name_length + le16_to_cpu(p_ea->ea_value_length);
176 }
177 
178 /*
179  * Set a new EA, and set EA_INFORMATION accordingly
180  *
181  * This is roughly the same as ZwSetEaFile() on Windows, however
182  * the "offset to next" of the last EA should not be cleared.
183  *
184  * Consistency of the new EA is first checked.
185  *
186  * EA_INFORMATION is set first, and it is restored to its former
187  * state if setting EA fails.
188  */
189 static int ntfs_set_ea(struct inode *inode, const char *name, size_t name_len,
190 		const void *value, size_t val_size, int flags,
191 		__le16 *packed_ea_size)
192 {
193 	struct ntfs_inode *ni = NTFS_I(inode);
194 	struct ea_information *p_ea_info = NULL;
195 	int ea_packed, err = 0;
196 	struct ea_attr *p_ea;
197 	u32 ea_info_qsize = 0;
198 	char *ea_buf = NULL;
199 	size_t new_ea_size = ALIGN(struct_size(p_ea, ea_name, 1 + name_len + val_size), 4);
200 	s64 ea_off, ea_info_size, all_ea_size, ea_size;
201 
202 	if (name_len > 255)
203 		return -ENAMETOOLONG;
204 
205 	if (ntfs_attr_exist(ni, AT_EA_INFORMATION, AT_UNNAMED, 0)) {
206 		p_ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0,
207 						&ea_info_size);
208 		if (!p_ea_info || ea_info_size != sizeof(struct ea_information))
209 			goto out;
210 
211 		ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &all_ea_size);
212 		if (!ea_buf) {
213 			ea_info_qsize = 0;
214 			kvfree(p_ea_info);
215 			goto create_ea_info;
216 		}
217 
218 		ea_info_qsize = le32_to_cpu(p_ea_info->ea_query_length);
219 	} else {
220 create_ea_info:
221 		p_ea_info = kzalloc(sizeof(struct ea_information), GFP_NOFS);
222 		if (!p_ea_info)
223 			return -ENOMEM;
224 
225 		ea_info_qsize = 0;
226 		err = ntfs_attr_add(ni, AT_EA_INFORMATION, AT_UNNAMED, 0,
227 				(char *)p_ea_info, sizeof(struct ea_information));
228 		if (err)
229 			goto out;
230 
231 		if (ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) {
232 			err = ntfs_attr_remove(ni, AT_EA, AT_UNNAMED, 0);
233 			if (err)
234 				goto out;
235 		}
236 
237 		goto alloc_new_ea;
238 	}
239 
240 	if (ea_info_qsize > all_ea_size) {
241 		err = -EIO;
242 		goto out;
243 	}
244 
245 	err = ntfs_ea_lookup(ea_buf, ea_info_qsize, name, name_len, &ea_off,
246 			&ea_size);
247 	if (ea_info_qsize && !err) {
248 		if (flags & XATTR_CREATE) {
249 			err = -EEXIST;
250 			goto out;
251 		}
252 
253 		p_ea = (struct ea_attr *)(ea_buf + ea_off);
254 
255 		if (val_size &&
256 		    le16_to_cpu(p_ea->ea_value_length) == val_size &&
257 		    !memcmp(p_ea->ea_name + p_ea->ea_name_length + 1, value,
258 			    val_size))
259 			goto out;
260 
261 		le16_add_cpu(&p_ea_info->ea_length, 0 - ea_packed_size(p_ea));
262 
263 		if (p_ea->flags & NEED_EA)
264 			le16_add_cpu(&p_ea_info->need_ea_count, -1);
265 
266 		memmove((char *)p_ea, (char *)p_ea + ea_size, ea_info_qsize - (ea_off + ea_size));
267 		ea_info_qsize -= ea_size;
268 		p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize);
269 
270 		err = ntfs_write_ea(ni, AT_EA_INFORMATION, (char *)p_ea_info, 0,
271 				sizeof(struct ea_information), false);
272 		if (err)
273 			goto out;
274 
275 		err = ntfs_write_ea(ni, AT_EA, ea_buf, 0, ea_info_qsize, true);
276 		if (err)
277 			goto out;
278 
279 		if ((flags & XATTR_REPLACE) && !val_size) {
280 			/* Remove xattr. */
281 			goto out;
282 		}
283 	} else {
284 		if (flags & XATTR_REPLACE) {
285 			err = -ENODATA;
286 			goto out;
287 		}
288 	}
289 	kvfree(ea_buf);
290 
291 alloc_new_ea:
292 	ea_buf = kzalloc(new_ea_size, GFP_NOFS);
293 	if (!ea_buf) {
294 		err = -ENOMEM;
295 		goto out;
296 	}
297 
298 	/*
299 	 * EA and REPARSE_POINT compatibility not checked any more,
300 	 * required by Windows 10, but having both may lead to
301 	 * problems with earlier versions.
302 	 */
303 	p_ea = (struct ea_attr *)ea_buf;
304 	memcpy(p_ea->ea_name, name, name_len);
305 	p_ea->ea_name_length = name_len;
306 	p_ea->ea_name[name_len] = 0;
307 	memcpy(p_ea->ea_name + name_len + 1, value, val_size);
308 	p_ea->ea_value_length = cpu_to_le16(val_size);
309 	p_ea->next_entry_offset = cpu_to_le32(new_ea_size);
310 
311 	ea_packed = le16_to_cpu(p_ea_info->ea_length) + ea_packed_size(p_ea);
312 	p_ea_info->ea_length = cpu_to_le16(ea_packed);
313 	p_ea_info->ea_query_length = cpu_to_le32(ea_info_qsize + new_ea_size);
314 
315 	if (ea_packed > 0xffff ||
316 	    ntfs_attr_size_bounds_check(ni->vol, AT_EA, new_ea_size)) {
317 		err = -EFBIG;
318 		goto out;
319 	}
320 
321 	/*
322 	 * no EA or EA_INFORMATION : add them
323 	 */
324 	if (!ntfs_attr_exist(ni, AT_EA, AT_UNNAMED, 0)) {
325 		err = ntfs_attr_add(ni, AT_EA, AT_UNNAMED, 0, (char *)p_ea,
326 				new_ea_size);
327 		if (err)
328 			goto out;
329 	} else {
330 		err = ntfs_write_ea(ni, AT_EA, (char *)p_ea, ea_info_qsize,
331 				new_ea_size, false);
332 		if (err)
333 			goto out;
334 	}
335 
336 	err = ntfs_write_ea(ni, AT_EA_INFORMATION, (char *)p_ea_info, 0,
337 			sizeof(struct ea_information), false);
338 	if (err)
339 		goto out;
340 
341 	if (packed_ea_size)
342 		*packed_ea_size = p_ea_info->ea_length;
343 	mark_mft_record_dirty(ni);
344 out:
345 	if (ea_info_qsize > 0)
346 		NInoSetHasEA(ni);
347 	else
348 		NInoClearHasEA(ni);
349 
350 	kvfree(ea_buf);
351 	kvfree(p_ea_info);
352 
353 	return err;
354 }
355 
356 /*
357  * Check for the presence of an EA "$LXDEV" (used by WSL)
358  * and return its value as a device address
359  */
360 int ntfs_ea_get_wsl_inode(struct inode *inode, dev_t *rdevp, unsigned int flags)
361 {
362 	int err;
363 	__le32 v;
364 
365 	if (!(flags & NTFS_VOL_UID)) {
366 		/* Load uid to lxuid EA */
367 		err = ntfs_get_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v,
368 				sizeof(v));
369 		if (err < 0)
370 			return err;
371 		if (err != sizeof(v))
372 			return -EIO;
373 		i_uid_write(inode, le32_to_cpu(v));
374 	}
375 
376 	if (!(flags & NTFS_VOL_GID)) {
377 		/* Load gid to lxgid EA */
378 		err = ntfs_get_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v,
379 				sizeof(v));
380 		if (err < 0)
381 			return err;
382 		if (err != sizeof(v))
383 			return -EIO;
384 		i_gid_write(inode, le32_to_cpu(v));
385 	}
386 
387 	/* Load mode to lxmod EA */
388 	err = ntfs_get_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v, sizeof(v));
389 	if (err == sizeof(v)) {
390 		inode->i_mode = le32_to_cpu(v);
391 	} else {
392 		/* Everyone gets all permissions. */
393 		inode->i_mode |= 0777;
394 	}
395 
396 	/* Load mode to lxdev EA */
397 	err = ntfs_get_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v));
398 	if (err == sizeof(v))
399 		*rdevp = le32_to_cpu(v);
400 	err = 0;
401 
402 	return err;
403 }
404 
405 int ntfs_ea_set_wsl_inode(struct inode *inode, dev_t rdev, __le16 *ea_size,
406 		unsigned int flags)
407 {
408 	__le32 v;
409 	int err;
410 
411 	if (flags & NTFS_EA_UID) {
412 		/* Store uid to lxuid EA */
413 		v = cpu_to_le32(i_uid_read(inode));
414 		err = ntfs_set_ea(inode, "$LXUID", sizeof("$LXUID") - 1, &v,
415 				sizeof(v), 0, ea_size);
416 		if (err)
417 			return err;
418 	}
419 
420 	if (flags & NTFS_EA_GID) {
421 		/* Store gid to lxgid EA */
422 		v = cpu_to_le32(i_gid_read(inode));
423 		err = ntfs_set_ea(inode, "$LXGID", sizeof("$LXGID") - 1, &v,
424 				sizeof(v), 0, ea_size);
425 		if (err)
426 			return err;
427 	}
428 
429 	if (flags & NTFS_EA_MODE) {
430 		/* Store mode to lxmod EA */
431 		v = cpu_to_le32(inode->i_mode);
432 		err = ntfs_set_ea(inode, "$LXMOD", sizeof("$LXMOD") - 1, &v,
433 				sizeof(v), 0, ea_size);
434 		if (err)
435 			return err;
436 	}
437 
438 	if (rdev) {
439 		v = cpu_to_le32(rdev);
440 		err = ntfs_set_ea(inode, "$LXDEV", sizeof("$LXDEV") - 1, &v, sizeof(v),
441 				0, ea_size);
442 	}
443 
444 	return err;
445 }
446 
447 ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
448 {
449 	struct inode *inode = d_inode(dentry);
450 	struct ntfs_inode *ni = NTFS_I(inode);
451 	const struct ea_attr *p_ea;
452 	s64 offset, ea_buf_size, ea_info_size;
453 	s64 ea_size;
454 	u32 next;
455 	int err = 0;
456 	u32 ea_info_qsize;
457 	char *ea_buf = NULL;
458 	ssize_t ret = 0;
459 	struct ea_information *ea_info;
460 
461 	if (!NInoHasEA(ni))
462 		return 0;
463 
464 	mutex_lock(&NTFS_I(inode)->mrec_lock);
465 	ea_info = ntfs_attr_readall(ni, AT_EA_INFORMATION, NULL, 0,
466 			&ea_info_size);
467 	if (!ea_info || ea_info_size != sizeof(struct ea_information))
468 		goto out;
469 
470 	ea_info_qsize = le32_to_cpu(ea_info->ea_query_length);
471 
472 	ea_buf = ntfs_attr_readall(ni, AT_EA, NULL, 0, &ea_buf_size);
473 	if (!ea_buf)
474 		goto out;
475 
476 	if (ea_info_qsize > ea_buf_size || ea_info_qsize == 0)
477 		goto out;
478 
479 	if (ea_info_qsize < sizeof(struct ea_attr)) {
480 		err = -EIO;
481 		goto out;
482 	}
483 
484 	offset = 0;
485 	do {
486 		p_ea = (const struct ea_attr *)&ea_buf[offset];
487 		next = le32_to_cpu(p_ea->next_entry_offset);
488 		ea_size = next ? next : (ea_info_qsize - offset);
489 
490 		if (ea_size < sizeof(struct ea_attr) ||
491 		    offset + ea_size > ea_info_qsize) {
492 			err = -EIO;
493 			goto out;
494 		}
495 
496 		if ((int)p_ea->ea_name_length + 1 >
497 			ea_size - offsetof(struct ea_attr, ea_name)) {
498 			err = -EIO;
499 			goto out;
500 		}
501 
502 		if (buffer) {
503 			if (ret + p_ea->ea_name_length + 1 > size) {
504 				err = -ERANGE;
505 				goto out;
506 			}
507 
508 			memcpy(buffer + ret, p_ea->ea_name, p_ea->ea_name_length);
509 			buffer[ret + p_ea->ea_name_length] = 0;
510 		}
511 
512 		ret += p_ea->ea_name_length + 1;
513 		offset += ea_size;
514 	} while (next > 0 && offset < ea_info_qsize);
515 
516 out:
517 	mutex_unlock(&NTFS_I(inode)->mrec_lock);
518 	kvfree(ea_info);
519 	kvfree(ea_buf);
520 
521 	return err ? err : ret;
522 }
523 
524 // clang-format off
525 #define SYSTEM_DOS_ATTRIB     "system.dos_attrib"
526 #define SYSTEM_NTFS_ATTRIB    "system.ntfs_attrib"
527 #define SYSTEM_NTFS_ATTRIB_BE "system.ntfs_attrib_be"
528 // clang-format on
529 
530 static int ntfs_getxattr(const struct xattr_handler *handler,
531 		struct dentry *unused, struct inode *inode, const char *name,
532 		void *buffer, size_t size)
533 {
534 	struct ntfs_inode *ni = NTFS_I(inode);
535 	int err;
536 
537 	if (NVolShutdown(ni->vol))
538 		return -EIO;
539 
540 	if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
541 		if (!buffer) {
542 			err = sizeof(u8);
543 		} else if (size < sizeof(u8)) {
544 			err = -ENODATA;
545 		} else {
546 			err = sizeof(u8);
547 			*(u8 *)buffer = (u8)(le32_to_cpu(ni->flags) & 0x3F);
548 		}
549 		goto out;
550 	}
551 
552 	if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
553 	    !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
554 		if (!buffer) {
555 			err = sizeof(u32);
556 		} else if (size < sizeof(u32)) {
557 			err = -ENODATA;
558 		} else {
559 			err = sizeof(u32);
560 			*(u32 *)buffer = le32_to_cpu(ni->flags);
561 			if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
562 				*(__be32 *)buffer = cpu_to_be32(*(u32 *)buffer);
563 		}
564 		goto out;
565 	}
566 
567 	mutex_lock(&ni->mrec_lock);
568 	err = ntfs_get_ea(inode, name, strlen(name), buffer, size);
569 	mutex_unlock(&ni->mrec_lock);
570 
571 out:
572 	return err;
573 }
574 
575 static int ntfs_new_attr_flags(struct ntfs_inode *ni, __le32 fattr)
576 {
577 	struct ntfs_attr_search_ctx *ctx;
578 	struct mft_record *m;
579 	struct attr_record *a;
580 	__le16 new_aflags;
581 	int mp_size, mp_ofs, name_ofs, arec_size, err;
582 
583 	m = map_mft_record(ni);
584 	if (IS_ERR(m))
585 		return PTR_ERR(m);
586 
587 	ctx = ntfs_attr_get_search_ctx(ni, m);
588 	if (!ctx) {
589 		err = -ENOMEM;
590 		goto err_out;
591 	}
592 
593 	err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len,
594 			CASE_SENSITIVE, 0, NULL, 0, ctx);
595 	if (err) {
596 		err = -EINVAL;
597 		goto err_out;
598 	}
599 
600 	a = ctx->attr;
601 	new_aflags = ctx->attr->flags;
602 
603 	if (fattr & FILE_ATTR_SPARSE_FILE)
604 		new_aflags |= ATTR_IS_SPARSE;
605 	else
606 		new_aflags &= ~ATTR_IS_SPARSE;
607 
608 	if (fattr & FILE_ATTR_COMPRESSED)
609 		new_aflags |= ATTR_IS_COMPRESSED;
610 	else
611 		new_aflags &= ~ATTR_IS_COMPRESSED;
612 
613 	if (new_aflags == a->flags)
614 		return 0;
615 
616 	if ((new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) ==
617 			  (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) {
618 		pr_err("file can't be sparsed and compressed\n");
619 		err = -EOPNOTSUPP;
620 		goto err_out;
621 	}
622 
623 	if (!a->non_resident)
624 		goto out;
625 
626 	if (a->data.non_resident.data_size) {
627 		pr_err("Can't change sparsed/compressed for non-empty file\n");
628 		err = -EOPNOTSUPP;
629 		goto err_out;
630 	}
631 
632 	if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED))
633 		name_ofs = (offsetof(struct attr_record,
634 				     data.non_resident.compressed_size) +
635 					sizeof(a->data.non_resident.compressed_size) + 7) & ~7;
636 	else
637 		name_ofs = (offsetof(struct attr_record,
638 				     data.non_resident.compressed_size) + 7) & ~7;
639 
640 	mp_size = ntfs_get_size_for_mapping_pairs(ni->vol, ni->runlist.rl, 0, -1, -1);
641 	if (unlikely(mp_size < 0)) {
642 		err = mp_size;
643 		ntfs_debug("Failed to get size for mapping pairs array, error code %i.\n", err);
644 		goto err_out;
645 	}
646 
647 	mp_ofs = (name_ofs + a->name_length * sizeof(__le16) + 7) & ~7;
648 	arec_size = (mp_ofs + mp_size + 7) & ~7;
649 
650 	err = ntfs_attr_record_resize(m, a, arec_size);
651 	if (unlikely(err))
652 		goto err_out;
653 
654 	if (new_aflags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) {
655 		a->data.non_resident.compression_unit = 0;
656 		if (new_aflags & ATTR_IS_COMPRESSED || ni->vol->major_ver < 3)
657 			a->data.non_resident.compression_unit = 4;
658 		a->data.non_resident.compressed_size = 0;
659 		ni->itype.compressed.size = 0;
660 		if (a->data.non_resident.compression_unit) {
661 			ni->itype.compressed.block_size = 1U <<
662 				(a->data.non_resident.compression_unit +
663 				 ni->vol->cluster_size_bits);
664 			ni->itype.compressed.block_size_bits =
665 					ffs(ni->itype.compressed.block_size) -
666 					1;
667 			ni->itype.compressed.block_clusters = 1U <<
668 					a->data.non_resident.compression_unit;
669 		} else {
670 			ni->itype.compressed.block_size = 0;
671 			ni->itype.compressed.block_size_bits = 0;
672 			ni->itype.compressed.block_clusters = 0;
673 		}
674 
675 		if (new_aflags & ATTR_IS_SPARSE) {
676 			NInoSetSparse(ni);
677 			ni->flags |= FILE_ATTR_SPARSE_FILE;
678 		}
679 
680 		if (new_aflags & ATTR_IS_COMPRESSED) {
681 			NInoSetCompressed(ni);
682 			ni->flags |= FILE_ATTR_COMPRESSED;
683 		}
684 	} else {
685 		ni->flags &= ~(FILE_ATTR_SPARSE_FILE | FILE_ATTR_COMPRESSED);
686 		a->data.non_resident.compression_unit = 0;
687 		NInoClearSparse(ni);
688 		NInoClearCompressed(ni);
689 	}
690 
691 	a->name_offset = cpu_to_le16(name_ofs);
692 	a->data.non_resident.mapping_pairs_offset = cpu_to_le16(mp_ofs);
693 
694 out:
695 	a->flags = new_aflags;
696 	mark_mft_record_dirty(ctx->ntfs_ino);
697 err_out:
698 	if (ctx)
699 		ntfs_attr_put_search_ctx(ctx);
700 	unmap_mft_record(ni);
701 	return err;
702 }
703 
704 static int ntfs_setxattr(const struct xattr_handler *handler,
705 		struct mnt_idmap *idmap, struct dentry *unused,
706 		struct inode *inode, const char *name, const void *value,
707 		size_t size, int flags)
708 {
709 	struct ntfs_inode *ni = NTFS_I(inode);
710 	int err;
711 	__le32 fattr;
712 
713 	if (NVolShutdown(ni->vol))
714 		return -EIO;
715 
716 	if (!strcmp(name, SYSTEM_DOS_ATTRIB)) {
717 		if (sizeof(u8) != size) {
718 			err = -EINVAL;
719 			goto out;
720 		}
721 		fattr = cpu_to_le32(*(u8 *)value);
722 		goto set_fattr;
723 	}
724 
725 	if (!strcmp(name, SYSTEM_NTFS_ATTRIB) ||
726 	    !strcmp(name, SYSTEM_NTFS_ATTRIB_BE)) {
727 		if (size != sizeof(u32)) {
728 			err = -EINVAL;
729 			goto out;
730 		}
731 		if (!strcmp(name, SYSTEM_NTFS_ATTRIB_BE))
732 			fattr = cpu_to_le32(be32_to_cpu(*(__be32 *)value));
733 		else
734 			fattr = cpu_to_le32(*(u32 *)value);
735 
736 		if (S_ISREG(inode->i_mode)) {
737 			mutex_lock(&ni->mrec_lock);
738 			err = ntfs_new_attr_flags(ni, fattr);
739 			mutex_unlock(&ni->mrec_lock);
740 			if (err)
741 				goto out;
742 		}
743 
744 set_fattr:
745 		if (S_ISDIR(inode->i_mode))
746 			fattr |= FILE_ATTR_DIRECTORY;
747 		else
748 			fattr &= ~FILE_ATTR_DIRECTORY;
749 
750 		if (ni->flags != fattr) {
751 			ni->flags = fattr;
752 			if (fattr & FILE_ATTR_READONLY)
753 				inode->i_mode &= ~0222;
754 			else
755 				inode->i_mode |= 0222;
756 			NInoSetFileNameDirty(ni);
757 			mark_inode_dirty(inode);
758 		}
759 		err = 0;
760 		goto out;
761 	}
762 
763 	mutex_lock(&ni->mrec_lock);
764 	err = ntfs_set_ea(inode, name, strlen(name), value, size, flags, NULL);
765 	mutex_unlock(&ni->mrec_lock);
766 
767 out:
768 	inode_set_ctime_current(inode);
769 	mark_inode_dirty(inode);
770 	return err;
771 }
772 
773 static bool ntfs_xattr_user_list(struct dentry *dentry)
774 {
775 	return true;
776 }
777 
778 // clang-format off
779 static const struct xattr_handler ntfs_other_xattr_handler = {
780 	.prefix	= "",
781 	.get	= ntfs_getxattr,
782 	.set	= ntfs_setxattr,
783 	.list	= ntfs_xattr_user_list,
784 };
785 
786 const struct xattr_handler * const ntfs_xattr_handlers[] = {
787 	&ntfs_other_xattr_handler,
788 	NULL,
789 };
790 // clang-format on
791 
792 #ifdef CONFIG_NTFS_FS_POSIX_ACL
793 struct posix_acl *ntfs_get_acl(struct mnt_idmap *idmap, struct dentry *dentry,
794 			       int type)
795 {
796 	struct inode *inode = d_inode(dentry);
797 	struct ntfs_inode *ni = NTFS_I(inode);
798 	const char *name;
799 	size_t name_len;
800 	struct posix_acl *acl;
801 	int err;
802 	void *buf;
803 
804 	buf = kmalloc(PATH_MAX, GFP_KERNEL);
805 	if (!buf)
806 		return ERR_PTR(-ENOMEM);
807 
808 	/* Possible values of 'type' was already checked above. */
809 	if (type == ACL_TYPE_ACCESS) {
810 		name = XATTR_NAME_POSIX_ACL_ACCESS;
811 		name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
812 	} else {
813 		name = XATTR_NAME_POSIX_ACL_DEFAULT;
814 		name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
815 	}
816 
817 	mutex_lock(&ni->mrec_lock);
818 	err = ntfs_get_ea(inode, name, name_len, buf, PATH_MAX);
819 	mutex_unlock(&ni->mrec_lock);
820 
821 	/* Translate extended attribute to acl. */
822 	if (err >= 0)
823 		acl = posix_acl_from_xattr(&init_user_ns, buf, err);
824 	else if (err == -ENODATA)
825 		acl = NULL;
826 	else
827 		acl = ERR_PTR(err);
828 
829 	if (!IS_ERR(acl))
830 		set_cached_acl(inode, type, acl);
831 
832 	kfree(buf);
833 
834 	return acl;
835 }
836 
837 static noinline int ntfs_set_acl_ex(struct mnt_idmap *idmap,
838 				    struct inode *inode, struct posix_acl *acl,
839 				    int type, bool init_acl)
840 {
841 	const char *name;
842 	size_t size, name_len;
843 	void *value;
844 	int err;
845 	int flags;
846 	umode_t mode;
847 
848 	if (S_ISLNK(inode->i_mode))
849 		return -EOPNOTSUPP;
850 
851 	mode = inode->i_mode;
852 	switch (type) {
853 	case ACL_TYPE_ACCESS:
854 		/* Do not change i_mode if we are in init_acl */
855 		if (acl && !init_acl) {
856 			err = posix_acl_update_mode(idmap, inode, &mode, &acl);
857 			if (err)
858 				return err;
859 		}
860 		name = XATTR_NAME_POSIX_ACL_ACCESS;
861 		name_len = sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1;
862 		break;
863 
864 	case ACL_TYPE_DEFAULT:
865 		if (!S_ISDIR(inode->i_mode))
866 			return acl ? -EACCES : 0;
867 		name = XATTR_NAME_POSIX_ACL_DEFAULT;
868 		name_len = sizeof(XATTR_NAME_POSIX_ACL_DEFAULT) - 1;
869 		break;
870 
871 	default:
872 		return -EINVAL;
873 	}
874 
875 	if (!acl) {
876 		/* Remove xattr if it can be presented via mode. */
877 		size = 0;
878 		value = NULL;
879 		flags = XATTR_REPLACE;
880 	} else {
881 		value = posix_acl_to_xattr(&init_user_ns, acl, &size, GFP_NOFS);
882 		if (!value)
883 			return -ENOMEM;
884 		flags = 0;
885 	}
886 
887 	mutex_lock(&NTFS_I(inode)->mrec_lock);
888 	err = ntfs_set_ea(inode, name, name_len, value, size, flags, NULL);
889 	mutex_unlock(&NTFS_I(inode)->mrec_lock);
890 	if (err == -ENODATA && !size)
891 		err = 0; /* Removing non existed xattr. */
892 	if (!err) {
893 		__le16 ea_size = 0;
894 		umode_t old_mode = inode->i_mode;
895 
896 		inode->i_mode = mode;
897 		mutex_lock(&NTFS_I(inode)->mrec_lock);
898 		err = ntfs_ea_set_wsl_inode(inode, 0, &ea_size, NTFS_EA_MODE);
899 		if (err) {
900 			ntfs_set_ea(inode, name, name_len, NULL, 0,
901 				    XATTR_REPLACE, NULL);
902 			mutex_unlock(&NTFS_I(inode)->mrec_lock);
903 			inode->i_mode = old_mode;
904 			goto out;
905 		}
906 		mutex_unlock(&NTFS_I(inode)->mrec_lock);
907 
908 		set_cached_acl(inode, type, acl);
909 		inode_set_ctime_current(inode);
910 		mark_inode_dirty(inode);
911 	}
912 
913 out:
914 	kfree(value);
915 
916 	return err;
917 }
918 
919 int ntfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
920 		 struct posix_acl *acl, int type)
921 {
922 	return ntfs_set_acl_ex(idmap, d_inode(dentry), acl, type, false);
923 }
924 
925 int ntfs_init_acl(struct mnt_idmap *idmap, struct inode *inode,
926 		  struct inode *dir)
927 {
928 	struct posix_acl *default_acl, *acl;
929 	int err;
930 
931 	err = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
932 	if (err)
933 		return err;
934 
935 	if (default_acl) {
936 		err = ntfs_set_acl_ex(idmap, inode, default_acl,
937 				      ACL_TYPE_DEFAULT, true);
938 		posix_acl_release(default_acl);
939 	} else {
940 		inode->i_default_acl = NULL;
941 	}
942 
943 	if (acl) {
944 		if (!err)
945 			err = ntfs_set_acl_ex(idmap, inode, acl,
946 					      ACL_TYPE_ACCESS, true);
947 		posix_acl_release(acl);
948 	} else {
949 		inode->i_acl = NULL;
950 	}
951 
952 	return err;
953 }
954 #endif
955