attrlist.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. */
  7. #include <linux/fs.h>
  8. #include "debug.h"
  9. #include "ntfs.h"
  10. #include "ntfs_fs.h"
  11. /*
  12. * al_is_valid_le
  13. *
  14. * Return: True if @le is valid.
  15. */
  16. static inline bool al_is_valid_le(const struct ntfs_inode *ni,
  17. struct ATTR_LIST_ENTRY *le)
  18. {
  19. if (!le || !ni->attr_list.le || !ni->attr_list.size)
  20. return false;
  21. return PtrOffset(ni->attr_list.le, le) + le16_to_cpu(le->size) <=
  22. ni->attr_list.size;
  23. }
  24. void al_destroy(struct ntfs_inode *ni)
  25. {
  26. run_close(&ni->attr_list.run);
  27. kvfree(ni->attr_list.le);
  28. ni->attr_list.le = NULL;
  29. ni->attr_list.size = 0;
  30. ni->attr_list.dirty = false;
  31. }
  32. /*
  33. * ntfs_load_attr_list
  34. *
  35. * This method makes sure that the ATTRIB list, if present,
  36. * has been properly set up.
  37. */
  38. int ntfs_load_attr_list(struct ntfs_inode *ni, struct ATTRIB *attr)
  39. {
  40. int err;
  41. size_t lsize;
  42. void *le = NULL;
  43. if (ni->attr_list.size)
  44. return 0;
  45. if (!attr->non_res) {
  46. lsize = le32_to_cpu(attr->res.data_size);
  47. /* attr is resident: lsize < record_size (1K or 4K) */
  48. le = kvmalloc(al_aligned(lsize), GFP_KERNEL);
  49. if (!le) {
  50. err = -ENOMEM;
  51. goto out;
  52. }
  53. memcpy(le, resident_data(attr), lsize);
  54. } else if (attr->nres.svcn) {
  55. err = -EINVAL;
  56. goto out;
  57. } else {
  58. u16 run_off = le16_to_cpu(attr->nres.run_off);
  59. lsize = le64_to_cpu(attr->nres.data_size);
  60. run_init(&ni->attr_list.run);
  61. if (run_off > le32_to_cpu(attr->size)) {
  62. err = -EINVAL;
  63. goto out;
  64. }
  65. err = run_unpack_ex(&ni->attr_list.run, ni->mi.sbi, ni->mi.rno,
  66. 0, le64_to_cpu(attr->nres.evcn), 0,
  67. Add2Ptr(attr, run_off),
  68. le32_to_cpu(attr->size) - run_off);
  69. if (err < 0)
  70. goto out;
  71. /* attr is nonresident.
  72. * The worst case:
  73. * 1T (2^40) extremely fragmented file.
  74. * cluster = 4K (2^12) => 2^28 fragments
  75. * 2^9 fragments per one record => 2^19 records
  76. * 2^5 bytes of ATTR_LIST_ENTRY per one record => 2^24 bytes.
  77. *
  78. * the result is 16M bytes per attribute list.
  79. * Use kvmalloc to allocate in range [several Kbytes - dozen Mbytes]
  80. */
  81. le = kvmalloc(al_aligned(lsize), GFP_KERNEL);
  82. if (!le) {
  83. err = -ENOMEM;
  84. goto out;
  85. }
  86. err = ntfs_read_run_nb(ni->mi.sbi, &ni->attr_list.run, 0, le,
  87. lsize, NULL);
  88. if (err)
  89. goto out;
  90. }
  91. ni->attr_list.size = lsize;
  92. ni->attr_list.le = le;
  93. return 0;
  94. out:
  95. ni->attr_list.le = le;
  96. al_destroy(ni);
  97. return err;
  98. }
  99. /*
  100. * al_enumerate
  101. *
  102. * Return:
  103. * * The next list le.
  104. * * If @le is NULL then return the first le.
  105. */
  106. struct ATTR_LIST_ENTRY *al_enumerate(struct ntfs_inode *ni,
  107. struct ATTR_LIST_ENTRY *le)
  108. {
  109. size_t off;
  110. u16 sz;
  111. const unsigned le_min_size = le_size(0);
  112. if (!le) {
  113. le = ni->attr_list.le;
  114. } else {
  115. sz = le16_to_cpu(le->size);
  116. if (sz < le_min_size) {
  117. /* Impossible 'cause we should not return such le. */
  118. return NULL;
  119. }
  120. le = Add2Ptr(le, sz);
  121. }
  122. /* Check boundary. */
  123. off = PtrOffset(ni->attr_list.le, le);
  124. if (off + le_min_size > ni->attr_list.size) {
  125. /* The regular end of list. */
  126. return NULL;
  127. }
  128. sz = le16_to_cpu(le->size);
  129. /* Check le for errors. */
  130. if (sz < le_min_size || off + sz > ni->attr_list.size ||
  131. sz < le->name_off + le->name_len * sizeof(short)) {
  132. return NULL;
  133. }
  134. return le;
  135. }
  136. /*
  137. * al_find_le
  138. *
  139. * Find the first le in the list which matches type, name and VCN.
  140. *
  141. * Return: NULL if not found.
  142. */
  143. struct ATTR_LIST_ENTRY *al_find_le(struct ntfs_inode *ni,
  144. struct ATTR_LIST_ENTRY *le,
  145. const struct ATTRIB *attr)
  146. {
  147. CLST svcn = attr_svcn(attr);
  148. return al_find_ex(ni, le, attr->type, attr_name(attr), attr->name_len,
  149. &svcn);
  150. }
  151. /*
  152. * al_find_ex
  153. *
  154. * Find the first le in the list which matches type, name and VCN.
  155. *
  156. * Return: NULL if not found.
  157. */
  158. struct ATTR_LIST_ENTRY *al_find_ex(struct ntfs_inode *ni,
  159. struct ATTR_LIST_ENTRY *le,
  160. enum ATTR_TYPE type, const __le16 *name,
  161. u8 name_len, const CLST *vcn)
  162. {
  163. struct ATTR_LIST_ENTRY *ret = NULL;
  164. u32 type_in = le32_to_cpu(type);
  165. while ((le = al_enumerate(ni, le))) {
  166. u64 le_vcn;
  167. int diff = le32_to_cpu(le->type) - type_in;
  168. /* List entries are sorted by type, name and VCN. */
  169. if (diff < 0)
  170. continue;
  171. if (diff > 0)
  172. return ret;
  173. if (le->name_len != name_len)
  174. continue;
  175. le_vcn = le64_to_cpu(le->vcn);
  176. if (!le_vcn) {
  177. /*
  178. * Compare entry names only for entry with vcn == 0.
  179. */
  180. diff = ntfs_cmp_names(le_name(le), name_len, name,
  181. name_len, ni->mi.sbi->upcase,
  182. true);
  183. if (diff < 0)
  184. continue;
  185. if (diff > 0)
  186. return ret;
  187. }
  188. if (!vcn)
  189. return le;
  190. if (*vcn == le_vcn)
  191. return le;
  192. if (*vcn < le_vcn)
  193. return ret;
  194. ret = le;
  195. }
  196. return ret;
  197. }
  198. /*
  199. * al_find_le_to_insert
  200. *
  201. * Find the first list entry which matches type, name and VCN.
  202. */
  203. static struct ATTR_LIST_ENTRY *al_find_le_to_insert(struct ntfs_inode *ni,
  204. enum ATTR_TYPE type,
  205. const __le16 *name,
  206. u8 name_len, CLST vcn)
  207. {
  208. struct ATTR_LIST_ENTRY *le = NULL, *prev;
  209. u32 type_in = le32_to_cpu(type);
  210. /* List entries are sorted by type, name and VCN. */
  211. while ((le = al_enumerate(ni, prev = le))) {
  212. int diff = le32_to_cpu(le->type) - type_in;
  213. if (diff < 0)
  214. continue;
  215. if (diff > 0)
  216. return le;
  217. if (!le->vcn) {
  218. /*
  219. * Compare entry names only for entry with vcn == 0.
  220. */
  221. diff = ntfs_cmp_names(le_name(le), le->name_len, name,
  222. name_len, ni->mi.sbi->upcase,
  223. true);
  224. if (diff < 0)
  225. continue;
  226. if (diff > 0)
  227. return le;
  228. }
  229. if (le64_to_cpu(le->vcn) >= vcn)
  230. return le;
  231. }
  232. return prev ? Add2Ptr(prev, le16_to_cpu(prev->size)) : ni->attr_list.le;
  233. }
  234. /*
  235. * al_add_le
  236. *
  237. * Add an "attribute list entry" to the list.
  238. */
  239. int al_add_le(struct ntfs_inode *ni, enum ATTR_TYPE type, const __le16 *name,
  240. u8 name_len, CLST svcn, __le16 id, const struct MFT_REF *ref,
  241. struct ATTR_LIST_ENTRY **new_le)
  242. {
  243. int err;
  244. struct ATTRIB *attr;
  245. struct ATTR_LIST_ENTRY *le;
  246. size_t off;
  247. u16 sz;
  248. size_t asize, new_asize, old_size;
  249. u64 new_size;
  250. typeof(ni->attr_list) *al = &ni->attr_list;
  251. /*
  252. * Compute the size of the new 'le'
  253. */
  254. sz = le_size(name_len);
  255. old_size = al->size;
  256. new_size = old_size + sz;
  257. asize = al_aligned(old_size);
  258. new_asize = al_aligned(new_size);
  259. /* Scan forward to the point at which the new 'le' should be inserted. */
  260. le = al_find_le_to_insert(ni, type, name, name_len, svcn);
  261. off = PtrOffset(al->le, le);
  262. if (new_size > asize) {
  263. void *ptr = kmalloc(new_asize, GFP_NOFS);
  264. if (!ptr)
  265. return -ENOMEM;
  266. memcpy(ptr, al->le, off);
  267. memcpy(Add2Ptr(ptr, off + sz), le, old_size - off);
  268. le = Add2Ptr(ptr, off);
  269. kvfree(al->le);
  270. al->le = ptr;
  271. } else {
  272. memmove(Add2Ptr(le, sz), le, old_size - off);
  273. }
  274. *new_le = le;
  275. al->size = new_size;
  276. le->type = type;
  277. le->size = cpu_to_le16(sz);
  278. le->name_len = name_len;
  279. le->name_off = offsetof(struct ATTR_LIST_ENTRY, name);
  280. le->vcn = cpu_to_le64(svcn);
  281. le->ref = *ref;
  282. le->id = id;
  283. memcpy(le->name, name, sizeof(short) * name_len);
  284. err = attr_set_size(ni, ATTR_LIST, NULL, 0, &al->run, new_size,
  285. &new_size, true, &attr);
  286. if (err) {
  287. /* Undo memmove above. */
  288. memmove(le, Add2Ptr(le, sz), old_size - off);
  289. al->size = old_size;
  290. return err;
  291. }
  292. al->dirty = true;
  293. if (attr && attr->non_res) {
  294. err = ntfs_sb_write_run(ni->mi.sbi, &al->run, 0, al->le,
  295. al->size, 0);
  296. if (err)
  297. return err;
  298. al->dirty = false;
  299. }
  300. return 0;
  301. }
  302. /*
  303. * al_remove_le - Remove @le from attribute list.
  304. */
  305. bool al_remove_le(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le)
  306. {
  307. u16 size;
  308. size_t off;
  309. typeof(ni->attr_list) *al = &ni->attr_list;
  310. if (!al_is_valid_le(ni, le))
  311. return false;
  312. /* Save on stack the size of 'le' */
  313. size = le16_to_cpu(le->size);
  314. off = PtrOffset(al->le, le);
  315. memmove(le, Add2Ptr(le, size), al->size - (off + size));
  316. al->size -= size;
  317. al->dirty = true;
  318. return true;
  319. }
  320. int al_update(struct ntfs_inode *ni, int sync)
  321. {
  322. int err;
  323. struct ATTRIB *attr;
  324. typeof(ni->attr_list) *al = &ni->attr_list;
  325. if (!al->dirty || !al->size)
  326. return 0;
  327. /*
  328. * Attribute list increased on demand in al_add_le.
  329. * Attribute list decreased here.
  330. */
  331. err = attr_set_size(ni, ATTR_LIST, NULL, 0, &al->run, al->size, NULL,
  332. false, &attr);
  333. if (err)
  334. goto out;
  335. if (!attr->non_res) {
  336. memcpy(resident_data(attr), al->le, al->size);
  337. } else {
  338. err = ntfs_sb_write_run(ni->mi.sbi, &al->run, 0, al->le,
  339. al->size, sync);
  340. if (err)
  341. goto out;
  342. attr->nres.valid_size = attr->nres.data_size;
  343. }
  344. ni->mi.dirty = true;
  345. al->dirty = false;
  346. out:
  347. return err;
  348. }