inline.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /*
  3. * Copyright (c) 2012 Taobao.
  4. * Written by Tao Ma <boyu.mt@taobao.com>
  5. */
  6. #include <linux/iomap.h>
  7. #include <linux/fiemap.h>
  8. #include <linux/namei.h>
  9. #include <linux/iversion.h>
  10. #include <linux/sched/mm.h>
  11. #include "ext4_jbd2.h"
  12. #include "ext4.h"
  13. #include "xattr.h"
  14. #include "truncate.h"
  15. #define EXT4_XATTR_SYSTEM_DATA "data"
  16. #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS))
  17. #define EXT4_INLINE_DOTDOT_OFFSET 2
  18. #define EXT4_INLINE_DOTDOT_SIZE 4
  19. static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
  20. struct inode *inode,
  21. void **fsdata);
  22. static int ext4_get_inline_size(struct inode *inode)
  23. {
  24. if (EXT4_I(inode)->i_inline_off)
  25. return EXT4_I(inode)->i_inline_size;
  26. return 0;
  27. }
  28. static int get_max_inline_xattr_value_size(struct inode *inode,
  29. struct ext4_iloc *iloc)
  30. {
  31. struct ext4_xattr_ibody_header *header;
  32. struct ext4_xattr_entry *entry;
  33. struct ext4_inode *raw_inode;
  34. void *end;
  35. int free, min_offs;
  36. if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
  37. return 0;
  38. min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
  39. EXT4_GOOD_OLD_INODE_SIZE -
  40. EXT4_I(inode)->i_extra_isize -
  41. sizeof(struct ext4_xattr_ibody_header);
  42. /*
  43. * We need to subtract another sizeof(__u32) since an in-inode xattr
  44. * needs an empty 4 bytes to indicate the gap between the xattr entry
  45. * and the name/value pair.
  46. */
  47. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  48. return EXT4_XATTR_SIZE(min_offs -
  49. EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
  50. EXT4_XATTR_ROUND - sizeof(__u32));
  51. raw_inode = ext4_raw_inode(iloc);
  52. header = IHDR(inode, raw_inode);
  53. entry = IFIRST(header);
  54. end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
  55. /* Compute min_offs. */
  56. while (!IS_LAST_ENTRY(entry)) {
  57. void *next = EXT4_XATTR_NEXT(entry);
  58. if (next >= end) {
  59. EXT4_ERROR_INODE(inode,
  60. "corrupt xattr in inline inode");
  61. return 0;
  62. }
  63. if (!entry->e_value_inum && entry->e_value_size) {
  64. size_t offs = le16_to_cpu(entry->e_value_offs);
  65. if (offs < min_offs)
  66. min_offs = offs;
  67. }
  68. entry = next;
  69. }
  70. free = min_offs -
  71. ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
  72. if (EXT4_I(inode)->i_inline_off) {
  73. entry = (struct ext4_xattr_entry *)
  74. ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
  75. free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
  76. goto out;
  77. }
  78. free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
  79. if (free > EXT4_XATTR_ROUND)
  80. free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
  81. else
  82. free = 0;
  83. out:
  84. return free;
  85. }
  86. /*
  87. * Get the maximum size we now can store in an inode.
  88. * If we can't find the space for a xattr entry, don't use the space
  89. * of the extents since we have no space to indicate the inline data.
  90. */
  91. int ext4_get_max_inline_size(struct inode *inode)
  92. {
  93. int error, max_inline_size;
  94. struct ext4_iloc iloc;
  95. if (EXT4_I(inode)->i_extra_isize == 0)
  96. return 0;
  97. error = ext4_get_inode_loc(inode, &iloc);
  98. if (error) {
  99. ext4_error_inode_err(inode, __func__, __LINE__, 0, -error,
  100. "can't get inode location %lu",
  101. inode->i_ino);
  102. return 0;
  103. }
  104. down_read(&EXT4_I(inode)->xattr_sem);
  105. max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
  106. up_read(&EXT4_I(inode)->xattr_sem);
  107. brelse(iloc.bh);
  108. if (!max_inline_size)
  109. return 0;
  110. return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
  111. }
  112. /*
  113. * this function does not take xattr_sem, which is OK because it is
  114. * currently only used in a code path coming form ext4_iget, before
  115. * the new inode has been unlocked
  116. */
  117. int ext4_find_inline_data_nolock(struct inode *inode)
  118. {
  119. struct ext4_xattr_ibody_find is = {
  120. .s = { .not_found = -ENODATA, },
  121. };
  122. struct ext4_xattr_info i = {
  123. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  124. .name = EXT4_XATTR_SYSTEM_DATA,
  125. };
  126. int error;
  127. if (EXT4_I(inode)->i_extra_isize == 0)
  128. return 0;
  129. error = ext4_get_inode_loc(inode, &is.iloc);
  130. if (error)
  131. return error;
  132. error = ext4_xattr_ibody_find(inode, &i, &is);
  133. if (error)
  134. goto out;
  135. if (!is.s.not_found) {
  136. if (is.s.here->e_value_inum) {
  137. EXT4_ERROR_INODE(inode, "inline data xattr refers "
  138. "to an external xattr inode");
  139. error = -EFSCORRUPTED;
  140. goto out;
  141. }
  142. EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
  143. (void *)ext4_raw_inode(&is.iloc));
  144. EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
  145. le32_to_cpu(is.s.here->e_value_size);
  146. }
  147. out:
  148. brelse(is.iloc.bh);
  149. return error;
  150. }
  151. static int ext4_read_inline_data(struct inode *inode, void *buffer,
  152. unsigned int len,
  153. struct ext4_iloc *iloc)
  154. {
  155. struct ext4_xattr_entry *entry;
  156. struct ext4_xattr_ibody_header *header;
  157. int cp_len = 0;
  158. struct ext4_inode *raw_inode;
  159. if (!len)
  160. return 0;
  161. BUG_ON(len > EXT4_I(inode)->i_inline_size);
  162. cp_len = min_t(unsigned int, len, EXT4_MIN_INLINE_DATA_SIZE);
  163. raw_inode = ext4_raw_inode(iloc);
  164. memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
  165. len -= cp_len;
  166. buffer += cp_len;
  167. if (!len)
  168. goto out;
  169. header = IHDR(inode, raw_inode);
  170. entry = (struct ext4_xattr_entry *)((void *)raw_inode +
  171. EXT4_I(inode)->i_inline_off);
  172. len = min_t(unsigned int, len,
  173. (unsigned int)le32_to_cpu(entry->e_value_size));
  174. memcpy(buffer,
  175. (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
  176. cp_len += len;
  177. out:
  178. return cp_len;
  179. }
  180. /*
  181. * write the buffer to the inline inode.
  182. * If 'create' is set, we don't need to do the extra copy in the xattr
  183. * value since it is already handled by ext4_xattr_ibody_set.
  184. * That saves us one memcpy.
  185. */
  186. static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
  187. void *buffer, loff_t pos, unsigned int len)
  188. {
  189. struct ext4_xattr_entry *entry;
  190. struct ext4_xattr_ibody_header *header;
  191. struct ext4_inode *raw_inode;
  192. int cp_len = 0;
  193. if (unlikely(ext4_emergency_state(inode->i_sb)))
  194. return;
  195. BUG_ON(!EXT4_I(inode)->i_inline_off);
  196. BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
  197. raw_inode = ext4_raw_inode(iloc);
  198. buffer += pos;
  199. if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
  200. cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
  201. EXT4_MIN_INLINE_DATA_SIZE - pos : len;
  202. memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
  203. len -= cp_len;
  204. buffer += cp_len;
  205. pos += cp_len;
  206. }
  207. if (!len)
  208. return;
  209. pos -= EXT4_MIN_INLINE_DATA_SIZE;
  210. header = IHDR(inode, raw_inode);
  211. entry = (struct ext4_xattr_entry *)((void *)raw_inode +
  212. EXT4_I(inode)->i_inline_off);
  213. memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
  214. buffer, len);
  215. }
  216. static int ext4_create_inline_data(handle_t *handle,
  217. struct inode *inode, unsigned len)
  218. {
  219. int error;
  220. void *value = NULL;
  221. struct ext4_xattr_ibody_find is = {
  222. .s = { .not_found = -ENODATA, },
  223. };
  224. struct ext4_xattr_info i = {
  225. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  226. .name = EXT4_XATTR_SYSTEM_DATA,
  227. };
  228. error = ext4_get_inode_loc(inode, &is.iloc);
  229. if (error)
  230. return error;
  231. BUFFER_TRACE(is.iloc.bh, "get_write_access");
  232. error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
  233. EXT4_JTR_NONE);
  234. if (error)
  235. goto out;
  236. if (len > EXT4_MIN_INLINE_DATA_SIZE) {
  237. value = EXT4_ZERO_XATTR_VALUE;
  238. len -= EXT4_MIN_INLINE_DATA_SIZE;
  239. } else {
  240. value = "";
  241. len = 0;
  242. }
  243. /* Insert the xttr entry. */
  244. i.value = value;
  245. i.value_len = len;
  246. error = ext4_xattr_ibody_find(inode, &i, &is);
  247. if (error)
  248. goto out;
  249. if (!is.s.not_found) {
  250. EXT4_ERROR_INODE(inode, "unexpected inline data xattr");
  251. error = -EFSCORRUPTED;
  252. goto out;
  253. }
  254. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  255. if (error) {
  256. if (error == -ENOSPC)
  257. ext4_clear_inode_state(inode,
  258. EXT4_STATE_MAY_INLINE_DATA);
  259. goto out;
  260. }
  261. memset((void *)ext4_raw_inode(&is.iloc)->i_block,
  262. 0, EXT4_MIN_INLINE_DATA_SIZE);
  263. EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
  264. (void *)ext4_raw_inode(&is.iloc));
  265. EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
  266. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  267. ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
  268. get_bh(is.iloc.bh);
  269. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  270. out:
  271. brelse(is.iloc.bh);
  272. return error;
  273. }
  274. static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
  275. unsigned int len)
  276. {
  277. int error;
  278. void *value = NULL;
  279. struct ext4_xattr_ibody_find is = {
  280. .s = { .not_found = -ENODATA, },
  281. };
  282. struct ext4_xattr_info i = {
  283. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  284. .name = EXT4_XATTR_SYSTEM_DATA,
  285. };
  286. /* If the old space is ok, write the data directly. */
  287. if (len <= EXT4_I(inode)->i_inline_size)
  288. return 0;
  289. error = ext4_get_inode_loc(inode, &is.iloc);
  290. if (error)
  291. return error;
  292. error = ext4_xattr_ibody_find(inode, &i, &is);
  293. if (error)
  294. goto out;
  295. if (is.s.not_found) {
  296. EXT4_ERROR_INODE(inode, "missing inline data xattr");
  297. error = -EFSCORRUPTED;
  298. goto out;
  299. }
  300. len -= EXT4_MIN_INLINE_DATA_SIZE;
  301. value = kzalloc(len, GFP_NOFS);
  302. if (!value) {
  303. error = -ENOMEM;
  304. goto out;
  305. }
  306. error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
  307. value, len);
  308. if (error < 0)
  309. goto out;
  310. BUFFER_TRACE(is.iloc.bh, "get_write_access");
  311. error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
  312. EXT4_JTR_NONE);
  313. if (error)
  314. goto out;
  315. /* Update the xattr entry. */
  316. i.value = value;
  317. i.value_len = len;
  318. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  319. if (error)
  320. goto out;
  321. EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
  322. (void *)ext4_raw_inode(&is.iloc));
  323. EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
  324. le32_to_cpu(is.s.here->e_value_size);
  325. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  326. get_bh(is.iloc.bh);
  327. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  328. out:
  329. kfree(value);
  330. brelse(is.iloc.bh);
  331. return error;
  332. }
  333. static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
  334. loff_t len)
  335. {
  336. int ret, size, no_expand;
  337. struct ext4_inode_info *ei = EXT4_I(inode);
  338. if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
  339. return -ENOSPC;
  340. size = ext4_get_max_inline_size(inode);
  341. if (size < len)
  342. return -ENOSPC;
  343. ext4_write_lock_xattr(inode, &no_expand);
  344. /*
  345. * ei->i_inline_size may have changed since the initial check
  346. * if other xattrs were added. Recalculate to ensure
  347. * ext4_update_inline_data() validates against current capacity.
  348. */
  349. (void) ext4_find_inline_data_nolock(inode);
  350. if (ei->i_inline_off)
  351. ret = ext4_update_inline_data(handle, inode, len);
  352. else
  353. ret = ext4_create_inline_data(handle, inode, len);
  354. ext4_write_unlock_xattr(inode, &no_expand);
  355. return ret;
  356. }
  357. static int ext4_destroy_inline_data_nolock(handle_t *handle,
  358. struct inode *inode)
  359. {
  360. struct ext4_inode_info *ei = EXT4_I(inode);
  361. struct ext4_xattr_ibody_find is = {
  362. .s = { .not_found = 0, },
  363. };
  364. struct ext4_xattr_info i = {
  365. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  366. .name = EXT4_XATTR_SYSTEM_DATA,
  367. .value = NULL,
  368. .value_len = 0,
  369. };
  370. int error;
  371. if (!ei->i_inline_off)
  372. return 0;
  373. down_write(&ei->i_data_sem);
  374. error = ext4_get_inode_loc(inode, &is.iloc);
  375. if (error) {
  376. up_write(&ei->i_data_sem);
  377. return error;
  378. }
  379. error = ext4_xattr_ibody_find(inode, &i, &is);
  380. if (error)
  381. goto out;
  382. BUFFER_TRACE(is.iloc.bh, "get_write_access");
  383. error = ext4_journal_get_write_access(handle, inode->i_sb, is.iloc.bh,
  384. EXT4_JTR_NONE);
  385. if (error)
  386. goto out;
  387. error = ext4_xattr_ibody_set(handle, inode, &i, &is);
  388. if (error)
  389. goto out;
  390. memset((void *)ext4_raw_inode(&is.iloc)->i_block,
  391. 0, EXT4_MIN_INLINE_DATA_SIZE);
  392. memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE);
  393. if (ext4_has_feature_extents(inode->i_sb)) {
  394. if (S_ISDIR(inode->i_mode) ||
  395. S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
  396. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  397. ext4_ext_tree_init(handle, inode);
  398. }
  399. }
  400. ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
  401. get_bh(is.iloc.bh);
  402. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  403. EXT4_I(inode)->i_inline_off = 0;
  404. EXT4_I(inode)->i_inline_size = 0;
  405. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  406. out:
  407. brelse(is.iloc.bh);
  408. if (error == -ENODATA)
  409. error = 0;
  410. up_write(&ei->i_data_sem);
  411. return error;
  412. }
  413. static int ext4_read_inline_folio(struct inode *inode, struct folio *folio)
  414. {
  415. void *kaddr;
  416. int ret = 0;
  417. size_t len;
  418. struct ext4_iloc iloc;
  419. BUG_ON(!folio_test_locked(folio));
  420. BUG_ON(!ext4_has_inline_data(inode));
  421. BUG_ON(folio->index);
  422. if (!EXT4_I(inode)->i_inline_off) {
  423. ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
  424. inode->i_ino);
  425. goto out;
  426. }
  427. ret = ext4_get_inode_loc(inode, &iloc);
  428. if (ret)
  429. goto out;
  430. len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
  431. if (len > PAGE_SIZE) {
  432. ext4_error_inode(inode, __func__, __LINE__, 0,
  433. "inline size %zu exceeds PAGE_SIZE", len);
  434. ret = -EFSCORRUPTED;
  435. brelse(iloc.bh);
  436. goto out;
  437. }
  438. kaddr = kmap_local_folio(folio, 0);
  439. ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
  440. kaddr = folio_zero_tail(folio, len, kaddr + len);
  441. kunmap_local(kaddr);
  442. folio_mark_uptodate(folio);
  443. brelse(iloc.bh);
  444. out:
  445. return ret;
  446. }
  447. int ext4_readpage_inline(struct inode *inode, struct folio *folio)
  448. {
  449. int ret = 0;
  450. down_read(&EXT4_I(inode)->xattr_sem);
  451. if (!ext4_has_inline_data(inode)) {
  452. up_read(&EXT4_I(inode)->xattr_sem);
  453. return -EAGAIN;
  454. }
  455. /*
  456. * Current inline data can only exist in the 1st page,
  457. * So for all the other pages, just set them uptodate.
  458. */
  459. if (!folio->index)
  460. ret = ext4_read_inline_folio(inode, folio);
  461. else if (!folio_test_uptodate(folio)) {
  462. folio_zero_segment(folio, 0, folio_size(folio));
  463. folio_mark_uptodate(folio);
  464. }
  465. up_read(&EXT4_I(inode)->xattr_sem);
  466. folio_unlock(folio);
  467. return ret >= 0 ? 0 : ret;
  468. }
  469. static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
  470. struct inode *inode)
  471. {
  472. int ret, needed_blocks, no_expand;
  473. handle_t *handle = NULL;
  474. int retries = 0, sem_held = 0;
  475. struct folio *folio = NULL;
  476. unsigned from, to;
  477. struct ext4_iloc iloc;
  478. if (!ext4_has_inline_data(inode)) {
  479. /*
  480. * clear the flag so that no new write
  481. * will trap here again.
  482. */
  483. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  484. return 0;
  485. }
  486. needed_blocks = ext4_chunk_trans_extent(inode, 1);
  487. ret = ext4_get_inode_loc(inode, &iloc);
  488. if (ret)
  489. return ret;
  490. retry:
  491. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
  492. if (IS_ERR(handle)) {
  493. ret = PTR_ERR(handle);
  494. handle = NULL;
  495. goto out;
  496. }
  497. /* We cannot recurse into the filesystem as the transaction is already
  498. * started */
  499. folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
  500. mapping_gfp_mask(mapping));
  501. if (IS_ERR(folio)) {
  502. ret = PTR_ERR(folio);
  503. goto out_nofolio;
  504. }
  505. ext4_write_lock_xattr(inode, &no_expand);
  506. sem_held = 1;
  507. /* If some one has already done this for us, just exit. */
  508. if (!ext4_has_inline_data(inode)) {
  509. ret = 0;
  510. goto out;
  511. }
  512. from = 0;
  513. to = ext4_get_inline_size(inode);
  514. if (!folio_test_uptodate(folio)) {
  515. ret = ext4_read_inline_folio(inode, folio);
  516. if (ret < 0)
  517. goto out;
  518. }
  519. ext4_fc_track_inode(handle, inode);
  520. ret = ext4_destroy_inline_data_nolock(handle, inode);
  521. if (ret)
  522. goto out;
  523. if (ext4_should_dioread_nolock(inode)) {
  524. ret = ext4_block_write_begin(handle, folio, from, to,
  525. ext4_get_block_unwritten);
  526. } else
  527. ret = ext4_block_write_begin(handle, folio, from, to,
  528. ext4_get_block);
  529. clear_buffer_new(folio_buffers(folio));
  530. if (!ret && ext4_should_journal_data(inode)) {
  531. ret = ext4_walk_page_buffers(handle, inode,
  532. folio_buffers(folio), from, to,
  533. NULL, do_journal_get_write_access);
  534. }
  535. if (ret) {
  536. folio_unlock(folio);
  537. folio_put(folio);
  538. folio = NULL;
  539. ext4_orphan_add(handle, inode);
  540. ext4_write_unlock_xattr(inode, &no_expand);
  541. sem_held = 0;
  542. ext4_journal_stop(handle);
  543. handle = NULL;
  544. ext4_truncate_failed_write(inode);
  545. /*
  546. * If truncate failed early the inode might
  547. * still be on the orphan list; we need to
  548. * make sure the inode is removed from the
  549. * orphan list in that case.
  550. */
  551. if (inode->i_nlink)
  552. ext4_orphan_del(NULL, inode);
  553. }
  554. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  555. goto retry;
  556. if (folio)
  557. block_commit_write(folio, from, to);
  558. out:
  559. if (folio) {
  560. folio_unlock(folio);
  561. folio_put(folio);
  562. }
  563. out_nofolio:
  564. if (sem_held)
  565. ext4_write_unlock_xattr(inode, &no_expand);
  566. if (handle)
  567. ext4_journal_stop(handle);
  568. brelse(iloc.bh);
  569. return ret;
  570. }
  571. /*
  572. * Prepare the write for the inline data.
  573. * If the data can be written into the inode, we just read
  574. * the page and make it uptodate, and start the journal.
  575. * Otherwise read the page, makes it dirty so that it can be
  576. * handle in writepages(the i_disksize update is left to the
  577. * normal ext4_da_write_end).
  578. */
  579. int ext4_generic_write_inline_data(struct address_space *mapping,
  580. struct inode *inode,
  581. loff_t pos, unsigned len,
  582. struct folio **foliop,
  583. void **fsdata, bool da)
  584. {
  585. int ret;
  586. handle_t *handle;
  587. struct folio *folio;
  588. struct ext4_iloc iloc;
  589. int retries = 0;
  590. ret = ext4_get_inode_loc(inode, &iloc);
  591. if (ret)
  592. return ret;
  593. retry_journal:
  594. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  595. if (IS_ERR(handle)) {
  596. ret = PTR_ERR(handle);
  597. goto out_release_bh;
  598. }
  599. ret = ext4_prepare_inline_data(handle, inode, pos + len);
  600. if (ret && ret != -ENOSPC)
  601. goto out_stop_journal;
  602. if (ret == -ENOSPC) {
  603. ext4_journal_stop(handle);
  604. if (!da) {
  605. brelse(iloc.bh);
  606. /* Retry inside */
  607. return ext4_convert_inline_data_to_extent(mapping, inode);
  608. }
  609. ret = ext4_da_convert_inline_data_to_extent(mapping, inode, fsdata);
  610. if (ret == -ENOSPC &&
  611. ext4_should_retry_alloc(inode->i_sb, &retries))
  612. goto retry_journal;
  613. goto out_release_bh;
  614. }
  615. folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
  616. mapping_gfp_mask(mapping));
  617. if (IS_ERR(folio)) {
  618. ret = PTR_ERR(folio);
  619. goto out_stop_journal;
  620. }
  621. down_read(&EXT4_I(inode)->xattr_sem);
  622. /* Someone else had converted it to extent */
  623. if (!ext4_has_inline_data(inode)) {
  624. ret = 0;
  625. goto out_release_folio;
  626. }
  627. if (!folio_test_uptodate(folio)) {
  628. ret = ext4_read_inline_folio(inode, folio);
  629. if (ret < 0)
  630. goto out_release_folio;
  631. }
  632. ret = ext4_journal_get_write_access(handle, inode->i_sb, iloc.bh, EXT4_JTR_NONE);
  633. if (ret)
  634. goto out_release_folio;
  635. *foliop = folio;
  636. up_read(&EXT4_I(inode)->xattr_sem);
  637. brelse(iloc.bh);
  638. return 1;
  639. out_release_folio:
  640. up_read(&EXT4_I(inode)->xattr_sem);
  641. folio_unlock(folio);
  642. folio_put(folio);
  643. out_stop_journal:
  644. ext4_journal_stop(handle);
  645. out_release_bh:
  646. brelse(iloc.bh);
  647. return ret;
  648. }
  649. /*
  650. * Try to write data in the inode.
  651. * If the inode has inline data, check whether the new write can be
  652. * in the inode also. If not, create the page the handle, move the data
  653. * to the page make it update and let the later codes create extent for it.
  654. */
  655. int ext4_try_to_write_inline_data(struct address_space *mapping,
  656. struct inode *inode,
  657. loff_t pos, unsigned len,
  658. struct folio **foliop)
  659. {
  660. if (pos + len > ext4_get_max_inline_size(inode))
  661. return ext4_convert_inline_data_to_extent(mapping, inode);
  662. return ext4_generic_write_inline_data(mapping, inode, pos, len,
  663. foliop, NULL, false);
  664. }
  665. int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
  666. unsigned copied, struct folio *folio)
  667. {
  668. handle_t *handle = ext4_journal_current_handle();
  669. int no_expand;
  670. void *kaddr;
  671. struct ext4_iloc iloc;
  672. int ret = 0, ret2;
  673. if (unlikely(copied < len) && !folio_test_uptodate(folio))
  674. copied = 0;
  675. if (likely(copied)) {
  676. ret = ext4_get_inode_loc(inode, &iloc);
  677. if (ret) {
  678. folio_unlock(folio);
  679. folio_put(folio);
  680. ext4_std_error(inode->i_sb, ret);
  681. goto out;
  682. }
  683. ext4_write_lock_xattr(inode, &no_expand);
  684. BUG_ON(!ext4_has_inline_data(inode));
  685. /*
  686. * ei->i_inline_off may have changed since
  687. * ext4_write_begin() called
  688. * ext4_try_to_write_inline_data()
  689. */
  690. (void) ext4_find_inline_data_nolock(inode);
  691. kaddr = kmap_local_folio(folio, 0);
  692. ext4_write_inline_data(inode, &iloc, kaddr, pos, copied);
  693. kunmap_local(kaddr);
  694. folio_mark_uptodate(folio);
  695. /* clear dirty flag so that writepages wouldn't work for us. */
  696. folio_clear_dirty(folio);
  697. ext4_write_unlock_xattr(inode, &no_expand);
  698. brelse(iloc.bh);
  699. /*
  700. * It's important to update i_size while still holding folio
  701. * lock: page writeout could otherwise come in and zero
  702. * beyond i_size.
  703. */
  704. ext4_update_inode_size(inode, pos + copied);
  705. }
  706. folio_unlock(folio);
  707. folio_put(folio);
  708. /*
  709. * Don't mark the inode dirty under folio lock. First, it unnecessarily
  710. * makes the holding time of folio lock longer. Second, it forces lock
  711. * ordering of folio lock and transaction start for journaling
  712. * filesystems.
  713. */
  714. if (likely(copied))
  715. mark_inode_dirty(inode);
  716. out:
  717. /*
  718. * If we didn't copy as much data as expected, we need to trim back
  719. * size of xattr containing inline data.
  720. */
  721. if (pos + len > inode->i_size && ext4_can_truncate(inode))
  722. ext4_orphan_add(handle, inode);
  723. ret2 = ext4_journal_stop(handle);
  724. if (!ret)
  725. ret = ret2;
  726. if (pos + len > inode->i_size) {
  727. ext4_truncate_failed_write(inode);
  728. /*
  729. * If truncate failed early the inode might still be
  730. * on the orphan list; we need to make sure the inode
  731. * is removed from the orphan list in that case.
  732. */
  733. if (inode->i_nlink)
  734. ext4_orphan_del(NULL, inode);
  735. }
  736. return ret ? ret : copied;
  737. }
  738. /*
  739. * Try to make the page cache and handle ready for the inline data case.
  740. * We can call this function in 2 cases:
  741. * 1. The inode is created and the first write exceeds inline size. We can
  742. * clear the inode state safely.
  743. * 2. The inode has inline data, then we need to read the data, make it
  744. * update and dirty so that ext4_da_writepages can handle it. We don't
  745. * need to start the journal since the file's metadata isn't changed now.
  746. */
  747. static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
  748. struct inode *inode,
  749. void **fsdata)
  750. {
  751. int ret = 0, inline_size;
  752. struct folio *folio;
  753. folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN,
  754. mapping_gfp_mask(mapping));
  755. if (IS_ERR(folio))
  756. return PTR_ERR(folio);
  757. down_read(&EXT4_I(inode)->xattr_sem);
  758. if (!ext4_has_inline_data(inode)) {
  759. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  760. goto out;
  761. }
  762. inline_size = ext4_get_inline_size(inode);
  763. if (!folio_test_uptodate(folio)) {
  764. ret = ext4_read_inline_folio(inode, folio);
  765. if (ret < 0)
  766. goto out;
  767. }
  768. ret = ext4_block_write_begin(NULL, folio, 0, inline_size,
  769. ext4_da_get_block_prep);
  770. if (ret) {
  771. up_read(&EXT4_I(inode)->xattr_sem);
  772. folio_unlock(folio);
  773. folio_put(folio);
  774. ext4_truncate_failed_write(inode);
  775. return ret;
  776. }
  777. clear_buffer_new(folio_buffers(folio));
  778. folio_mark_dirty(folio);
  779. folio_mark_uptodate(folio);
  780. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  781. *fsdata = (void *)CONVERT_INLINE_DATA;
  782. out:
  783. up_read(&EXT4_I(inode)->xattr_sem);
  784. if (folio) {
  785. folio_unlock(folio);
  786. folio_put(folio);
  787. }
  788. return ret;
  789. }
  790. #ifdef INLINE_DIR_DEBUG
  791. void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
  792. void *inline_start, int inline_size)
  793. {
  794. int offset;
  795. unsigned short de_len;
  796. struct ext4_dir_entry_2 *de = inline_start;
  797. void *dlimit = inline_start + inline_size;
  798. trace_printk("inode %lu\n", dir->i_ino);
  799. offset = 0;
  800. while ((void *)de < dlimit) {
  801. de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
  802. trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
  803. offset, de_len, de->name_len, de->name,
  804. de->name_len, le32_to_cpu(de->inode));
  805. if (ext4_check_dir_entry(dir, NULL, de, bh,
  806. inline_start, inline_size, offset))
  807. BUG();
  808. offset += de_len;
  809. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  810. }
  811. }
  812. #else
  813. #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
  814. #endif
  815. /*
  816. * Add a new entry into a inline dir.
  817. * It will return -ENOSPC if no space is available, and -EIO
  818. * and -EEXIST if directory entry already exists.
  819. */
  820. static int ext4_add_dirent_to_inline(handle_t *handle,
  821. struct ext4_filename *fname,
  822. struct inode *dir,
  823. struct inode *inode,
  824. struct ext4_iloc *iloc,
  825. void *inline_start, int inline_size)
  826. {
  827. int err;
  828. struct ext4_dir_entry_2 *de;
  829. err = ext4_find_dest_de(dir, iloc->bh, inline_start,
  830. inline_size, fname, &de);
  831. if (err)
  832. return err;
  833. BUFFER_TRACE(iloc->bh, "get_write_access");
  834. err = ext4_journal_get_write_access(handle, dir->i_sb, iloc->bh,
  835. EXT4_JTR_NONE);
  836. if (err)
  837. return err;
  838. ext4_insert_dentry(dir, inode, de, inline_size, fname);
  839. ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
  840. /*
  841. * XXX shouldn't update any times until successful
  842. * completion of syscall, but too many callers depend
  843. * on this.
  844. *
  845. * XXX similarly, too many callers depend on
  846. * ext4_new_inode() setting the times, but error
  847. * recovery deletes the inode, so the worst that can
  848. * happen is that the times are slightly out of date
  849. * and/or different from the directory change time.
  850. */
  851. inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
  852. ext4_update_dx_flag(dir);
  853. inode_inc_iversion(dir);
  854. return 1;
  855. }
  856. static void *ext4_get_inline_xattr_pos(struct inode *inode,
  857. struct ext4_iloc *iloc)
  858. {
  859. struct ext4_xattr_entry *entry;
  860. struct ext4_xattr_ibody_header *header;
  861. BUG_ON(!EXT4_I(inode)->i_inline_off);
  862. header = IHDR(inode, ext4_raw_inode(iloc));
  863. entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
  864. EXT4_I(inode)->i_inline_off);
  865. return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
  866. }
  867. /* Set the final de to cover the whole block. */
  868. void ext4_update_final_de(void *de_buf, int old_size, int new_size)
  869. {
  870. struct ext4_dir_entry_2 *de, *prev_de;
  871. void *limit;
  872. int de_len;
  873. de = de_buf;
  874. if (old_size) {
  875. limit = de_buf + old_size;
  876. do {
  877. prev_de = de;
  878. de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
  879. de_buf += de_len;
  880. de = de_buf;
  881. } while (de_buf < limit);
  882. prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
  883. old_size, new_size);
  884. } else {
  885. /* this is just created, so create an empty entry. */
  886. de->inode = 0;
  887. de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
  888. }
  889. }
  890. static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
  891. struct ext4_iloc *iloc)
  892. {
  893. int ret;
  894. int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
  895. int new_size = get_max_inline_xattr_value_size(dir, iloc);
  896. if (new_size - old_size <= ext4_dir_rec_len(1, NULL))
  897. return -ENOSPC;
  898. ret = ext4_update_inline_data(handle, dir,
  899. new_size + EXT4_MIN_INLINE_DATA_SIZE);
  900. if (ret)
  901. return ret;
  902. ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
  903. EXT4_I(dir)->i_inline_size -
  904. EXT4_MIN_INLINE_DATA_SIZE);
  905. dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
  906. return 0;
  907. }
  908. static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
  909. struct ext4_iloc *iloc,
  910. void *buf, int inline_size)
  911. {
  912. int ret;
  913. ret = ext4_create_inline_data(handle, inode, inline_size);
  914. if (ret) {
  915. ext4_msg(inode->i_sb, KERN_EMERG,
  916. "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)",
  917. inode->i_ino, ret);
  918. return;
  919. }
  920. ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
  921. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  922. }
  923. static int ext4_convert_inline_data_nolock(handle_t *handle,
  924. struct inode *inode,
  925. struct ext4_iloc *iloc)
  926. {
  927. int error;
  928. void *buf = NULL;
  929. struct buffer_head *data_bh = NULL;
  930. struct ext4_map_blocks map;
  931. int inline_size;
  932. inline_size = ext4_get_inline_size(inode);
  933. buf = kmalloc(inline_size, GFP_NOFS);
  934. if (!buf) {
  935. error = -ENOMEM;
  936. goto out;
  937. }
  938. error = ext4_read_inline_data(inode, buf, inline_size, iloc);
  939. if (error < 0)
  940. goto out;
  941. /*
  942. * Make sure the inline directory entries pass checks before we try to
  943. * convert them, so that we avoid touching stuff that needs fsck.
  944. */
  945. if (S_ISDIR(inode->i_mode)) {
  946. error = ext4_check_all_de(inode, iloc->bh,
  947. buf + EXT4_INLINE_DOTDOT_SIZE,
  948. inline_size - EXT4_INLINE_DOTDOT_SIZE);
  949. if (error)
  950. goto out;
  951. }
  952. error = ext4_destroy_inline_data_nolock(handle, inode);
  953. if (error)
  954. goto out;
  955. map.m_lblk = 0;
  956. map.m_len = 1;
  957. map.m_flags = 0;
  958. error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
  959. if (error < 0)
  960. goto out_restore;
  961. if (!(map.m_flags & EXT4_MAP_MAPPED)) {
  962. error = -EIO;
  963. goto out_restore;
  964. }
  965. data_bh = sb_getblk(inode->i_sb, map.m_pblk);
  966. if (!data_bh) {
  967. error = -ENOMEM;
  968. goto out_restore;
  969. }
  970. lock_buffer(data_bh);
  971. error = ext4_journal_get_create_access(handle, inode->i_sb, data_bh,
  972. EXT4_JTR_NONE);
  973. if (error) {
  974. unlock_buffer(data_bh);
  975. error = -EIO;
  976. goto out_restore;
  977. }
  978. memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
  979. if (!S_ISDIR(inode->i_mode)) {
  980. memcpy(data_bh->b_data, buf, inline_size);
  981. set_buffer_uptodate(data_bh);
  982. unlock_buffer(data_bh);
  983. error = ext4_handle_dirty_metadata(handle,
  984. inode, data_bh);
  985. } else {
  986. unlock_buffer(data_bh);
  987. inode->i_size = inode->i_sb->s_blocksize;
  988. i_size_write(inode, inode->i_sb->s_blocksize);
  989. EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  990. error = ext4_init_dirblock(handle, inode, data_bh,
  991. le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode),
  992. buf + EXT4_INLINE_DOTDOT_SIZE,
  993. inline_size - EXT4_INLINE_DOTDOT_SIZE);
  994. if (!error)
  995. error = ext4_mark_inode_dirty(handle, inode);
  996. }
  997. out_restore:
  998. if (error)
  999. ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
  1000. out:
  1001. brelse(data_bh);
  1002. kfree(buf);
  1003. return error;
  1004. }
  1005. /*
  1006. * Try to add the new entry to the inline data.
  1007. * If succeeds, return 0. If not, extended the inline dir and copied data to
  1008. * the new created block.
  1009. */
  1010. int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
  1011. struct inode *dir, struct inode *inode)
  1012. {
  1013. int ret, ret2, inline_size, no_expand;
  1014. void *inline_start;
  1015. struct ext4_iloc iloc;
  1016. ret = ext4_get_inode_loc(dir, &iloc);
  1017. if (ret)
  1018. return ret;
  1019. ext4_write_lock_xattr(dir, &no_expand);
  1020. if (!ext4_has_inline_data(dir))
  1021. goto out;
  1022. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1023. EXT4_INLINE_DOTDOT_SIZE;
  1024. inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
  1025. ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
  1026. inline_start, inline_size);
  1027. if (ret != -ENOSPC)
  1028. goto out;
  1029. /* check whether it can be inserted to inline xattr space. */
  1030. inline_size = EXT4_I(dir)->i_inline_size -
  1031. EXT4_MIN_INLINE_DATA_SIZE;
  1032. if (!inline_size) {
  1033. /* Try to use the xattr space.*/
  1034. ret = ext4_update_inline_dir(handle, dir, &iloc);
  1035. if (ret && ret != -ENOSPC)
  1036. goto out;
  1037. inline_size = EXT4_I(dir)->i_inline_size -
  1038. EXT4_MIN_INLINE_DATA_SIZE;
  1039. }
  1040. if (inline_size) {
  1041. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1042. ret = ext4_add_dirent_to_inline(handle, fname, dir,
  1043. inode, &iloc, inline_start,
  1044. inline_size);
  1045. if (ret != -ENOSPC)
  1046. goto out;
  1047. }
  1048. /*
  1049. * The inline space is filled up, so create a new block for it.
  1050. * As the extent tree will be created, we have to save the inline
  1051. * dir first.
  1052. */
  1053. ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
  1054. out:
  1055. ext4_write_unlock_xattr(dir, &no_expand);
  1056. ret2 = ext4_mark_inode_dirty(handle, dir);
  1057. if (unlikely(ret2 && !ret))
  1058. ret = ret2;
  1059. brelse(iloc.bh);
  1060. return ret;
  1061. }
  1062. /*
  1063. * This function fills a red-black tree with information from an
  1064. * inlined dir. It returns the number directory entries loaded
  1065. * into the tree. If there is an error it is returned in err.
  1066. */
  1067. int ext4_inlinedir_to_tree(struct file *dir_file,
  1068. struct inode *dir, ext4_lblk_t block,
  1069. struct dx_hash_info *hinfo,
  1070. __u32 start_hash, __u32 start_minor_hash,
  1071. int *has_inline_data)
  1072. {
  1073. int err = 0, count = 0;
  1074. unsigned int parent_ino;
  1075. int pos;
  1076. struct ext4_dir_entry_2 *de;
  1077. struct inode *inode = file_inode(dir_file);
  1078. int ret, inline_size = 0;
  1079. struct ext4_iloc iloc;
  1080. void *dir_buf = NULL;
  1081. struct ext4_dir_entry_2 fake;
  1082. struct fscrypt_str tmp_str;
  1083. ret = ext4_get_inode_loc(inode, &iloc);
  1084. if (ret)
  1085. return ret;
  1086. down_read(&EXT4_I(inode)->xattr_sem);
  1087. if (!ext4_has_inline_data(inode)) {
  1088. up_read(&EXT4_I(inode)->xattr_sem);
  1089. *has_inline_data = 0;
  1090. goto out;
  1091. }
  1092. inline_size = ext4_get_inline_size(inode);
  1093. dir_buf = kmalloc(inline_size, GFP_NOFS);
  1094. if (!dir_buf) {
  1095. ret = -ENOMEM;
  1096. up_read(&EXT4_I(inode)->xattr_sem);
  1097. goto out;
  1098. }
  1099. ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
  1100. up_read(&EXT4_I(inode)->xattr_sem);
  1101. if (ret < 0)
  1102. goto out;
  1103. pos = 0;
  1104. parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
  1105. while (pos < inline_size) {
  1106. /*
  1107. * As inlined dir doesn't store any information about '.' and
  1108. * only the inode number of '..' is stored, we have to handle
  1109. * them differently.
  1110. */
  1111. if (pos == 0) {
  1112. fake.inode = cpu_to_le32(inode->i_ino);
  1113. fake.name_len = 1;
  1114. memcpy(fake.name, ".", 2);
  1115. fake.rec_len = ext4_rec_len_to_disk(
  1116. ext4_dir_rec_len(fake.name_len, NULL),
  1117. inline_size);
  1118. ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
  1119. de = &fake;
  1120. pos = EXT4_INLINE_DOTDOT_OFFSET;
  1121. } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
  1122. fake.inode = cpu_to_le32(parent_ino);
  1123. fake.name_len = 2;
  1124. memcpy(fake.name, "..", 3);
  1125. fake.rec_len = ext4_rec_len_to_disk(
  1126. ext4_dir_rec_len(fake.name_len, NULL),
  1127. inline_size);
  1128. ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
  1129. de = &fake;
  1130. pos = EXT4_INLINE_DOTDOT_SIZE;
  1131. } else {
  1132. de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
  1133. pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
  1134. if (ext4_check_dir_entry(inode, dir_file, de,
  1135. iloc.bh, dir_buf,
  1136. inline_size, pos)) {
  1137. ret = count;
  1138. goto out;
  1139. }
  1140. }
  1141. if (ext4_hash_in_dirent(dir)) {
  1142. hinfo->hash = EXT4_DIRENT_HASH(de);
  1143. hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de);
  1144. } else {
  1145. err = ext4fs_dirhash(dir, de->name, de->name_len, hinfo);
  1146. if (err) {
  1147. ret = err;
  1148. goto out;
  1149. }
  1150. }
  1151. if ((hinfo->hash < start_hash) ||
  1152. ((hinfo->hash == start_hash) &&
  1153. (hinfo->minor_hash < start_minor_hash)))
  1154. continue;
  1155. if (de->inode == 0)
  1156. continue;
  1157. tmp_str.name = de->name;
  1158. tmp_str.len = de->name_len;
  1159. err = ext4_htree_store_dirent(dir_file, hinfo->hash,
  1160. hinfo->minor_hash, de, &tmp_str);
  1161. if (err) {
  1162. ret = err;
  1163. goto out;
  1164. }
  1165. count++;
  1166. }
  1167. ret = count;
  1168. out:
  1169. kfree(dir_buf);
  1170. brelse(iloc.bh);
  1171. return ret;
  1172. }
  1173. /*
  1174. * So this function is called when the volume is mkfsed with
  1175. * dir_index disabled. In order to keep f_pos persistent
  1176. * after we convert from an inlined dir to a blocked based,
  1177. * we just pretend that we are a normal dir and return the
  1178. * offset as if '.' and '..' really take place.
  1179. *
  1180. */
  1181. int ext4_read_inline_dir(struct file *file,
  1182. struct dir_context *ctx,
  1183. int *has_inline_data)
  1184. {
  1185. unsigned int offset, parent_ino;
  1186. int i;
  1187. struct ext4_dir_entry_2 *de;
  1188. struct super_block *sb;
  1189. struct inode *inode = file_inode(file);
  1190. int ret, inline_size = 0;
  1191. struct ext4_iloc iloc;
  1192. void *dir_buf = NULL;
  1193. int dotdot_offset, dotdot_size, extra_offset, extra_size;
  1194. struct dir_private_info *info = file->private_data;
  1195. ret = ext4_get_inode_loc(inode, &iloc);
  1196. if (ret)
  1197. return ret;
  1198. down_read(&EXT4_I(inode)->xattr_sem);
  1199. if (!ext4_has_inline_data(inode)) {
  1200. up_read(&EXT4_I(inode)->xattr_sem);
  1201. *has_inline_data = 0;
  1202. goto out;
  1203. }
  1204. inline_size = ext4_get_inline_size(inode);
  1205. dir_buf = kmalloc(inline_size, GFP_NOFS);
  1206. if (!dir_buf) {
  1207. ret = -ENOMEM;
  1208. up_read(&EXT4_I(inode)->xattr_sem);
  1209. goto out;
  1210. }
  1211. ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
  1212. up_read(&EXT4_I(inode)->xattr_sem);
  1213. if (ret < 0)
  1214. goto out;
  1215. ret = 0;
  1216. sb = inode->i_sb;
  1217. parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
  1218. offset = ctx->pos;
  1219. /*
  1220. * dotdot_offset and dotdot_size is the real offset and
  1221. * size for ".." and "." if the dir is block based while
  1222. * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
  1223. * So we will use extra_offset and extra_size to indicate them
  1224. * during the inline dir iteration.
  1225. */
  1226. dotdot_offset = ext4_dir_rec_len(1, NULL);
  1227. dotdot_size = dotdot_offset + ext4_dir_rec_len(2, NULL);
  1228. extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
  1229. extra_size = extra_offset + inline_size;
  1230. /*
  1231. * If the cookie has changed since the last call to
  1232. * readdir(2), then we might be pointing to an invalid
  1233. * dirent right now. Scan from the start of the inline
  1234. * dir to make sure.
  1235. */
  1236. if (!inode_eq_iversion(inode, info->cookie)) {
  1237. for (i = 0; i < extra_size && i < offset;) {
  1238. /*
  1239. * "." is with offset 0 and
  1240. * ".." is dotdot_offset.
  1241. */
  1242. if (!i) {
  1243. i = dotdot_offset;
  1244. continue;
  1245. } else if (i == dotdot_offset) {
  1246. i = dotdot_size;
  1247. continue;
  1248. }
  1249. /* for other entry, the real offset in
  1250. * the buf has to be tuned accordingly.
  1251. */
  1252. de = (struct ext4_dir_entry_2 *)
  1253. (dir_buf + i - extra_offset);
  1254. /* It's too expensive to do a full
  1255. * dirent test each time round this
  1256. * loop, but we do have to test at
  1257. * least that it is non-zero. A
  1258. * failure will be detected in the
  1259. * dirent test below. */
  1260. if (ext4_rec_len_from_disk(de->rec_len, extra_size)
  1261. < ext4_dir_rec_len(1, NULL))
  1262. break;
  1263. i += ext4_rec_len_from_disk(de->rec_len,
  1264. extra_size);
  1265. }
  1266. offset = i;
  1267. ctx->pos = offset;
  1268. info->cookie = inode_query_iversion(inode);
  1269. }
  1270. while (ctx->pos < extra_size) {
  1271. if (ctx->pos == 0) {
  1272. if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
  1273. goto out;
  1274. ctx->pos = dotdot_offset;
  1275. continue;
  1276. }
  1277. if (ctx->pos == dotdot_offset) {
  1278. if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
  1279. goto out;
  1280. ctx->pos = dotdot_size;
  1281. continue;
  1282. }
  1283. de = (struct ext4_dir_entry_2 *)
  1284. (dir_buf + ctx->pos - extra_offset);
  1285. if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
  1286. extra_size, ctx->pos))
  1287. goto out;
  1288. if (le32_to_cpu(de->inode)) {
  1289. if (!dir_emit(ctx, de->name, de->name_len,
  1290. le32_to_cpu(de->inode),
  1291. get_dtype(sb, de->file_type)))
  1292. goto out;
  1293. }
  1294. ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
  1295. }
  1296. out:
  1297. kfree(dir_buf);
  1298. brelse(iloc.bh);
  1299. return ret;
  1300. }
  1301. void *ext4_read_inline_link(struct inode *inode)
  1302. {
  1303. struct ext4_iloc iloc;
  1304. int ret, inline_size;
  1305. void *link;
  1306. ret = ext4_get_inode_loc(inode, &iloc);
  1307. if (ret)
  1308. return ERR_PTR(ret);
  1309. ret = -ENOMEM;
  1310. inline_size = ext4_get_inline_size(inode);
  1311. link = kmalloc(inline_size + 1, GFP_NOFS);
  1312. if (!link)
  1313. goto out;
  1314. ret = ext4_read_inline_data(inode, link, inline_size, &iloc);
  1315. if (ret < 0) {
  1316. kfree(link);
  1317. goto out;
  1318. }
  1319. nd_terminate_link(link, inode->i_size, ret);
  1320. out:
  1321. if (ret < 0)
  1322. link = ERR_PTR(ret);
  1323. brelse(iloc.bh);
  1324. return link;
  1325. }
  1326. struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
  1327. struct ext4_dir_entry_2 **parent_de,
  1328. int *retval)
  1329. {
  1330. struct ext4_iloc iloc;
  1331. *retval = ext4_get_inode_loc(inode, &iloc);
  1332. if (*retval)
  1333. return NULL;
  1334. *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1335. return iloc.bh;
  1336. }
  1337. /*
  1338. * Try to create the inline data for the new dir.
  1339. * If it succeeds, return 0, otherwise return the error.
  1340. * In case of ENOSPC, the caller should create the normal disk layout dir.
  1341. */
  1342. int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
  1343. struct inode *inode)
  1344. {
  1345. int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
  1346. struct ext4_iloc iloc;
  1347. struct ext4_dir_entry_2 *de;
  1348. ret = ext4_get_inode_loc(inode, &iloc);
  1349. if (ret)
  1350. return ret;
  1351. ret = ext4_prepare_inline_data(handle, inode, inline_size);
  1352. if (ret)
  1353. goto out;
  1354. /*
  1355. * For inline dir, we only save the inode information for the ".."
  1356. * and create a fake dentry to cover the left space.
  1357. */
  1358. de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1359. de->inode = cpu_to_le32(parent->i_ino);
  1360. de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
  1361. de->inode = 0;
  1362. de->rec_len = ext4_rec_len_to_disk(
  1363. inline_size - EXT4_INLINE_DOTDOT_SIZE,
  1364. inline_size);
  1365. set_nlink(inode, 2);
  1366. inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
  1367. out:
  1368. brelse(iloc.bh);
  1369. return ret;
  1370. }
  1371. struct buffer_head *ext4_find_inline_entry(struct inode *dir,
  1372. struct ext4_filename *fname,
  1373. struct ext4_dir_entry_2 **res_dir,
  1374. int *has_inline_data)
  1375. {
  1376. struct ext4_xattr_ibody_find is = {
  1377. .s = { .not_found = -ENODATA, },
  1378. };
  1379. struct ext4_xattr_info i = {
  1380. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  1381. .name = EXT4_XATTR_SYSTEM_DATA,
  1382. };
  1383. int ret;
  1384. void *inline_start;
  1385. int inline_size;
  1386. ret = ext4_get_inode_loc(dir, &is.iloc);
  1387. if (ret)
  1388. return ERR_PTR(ret);
  1389. down_read(&EXT4_I(dir)->xattr_sem);
  1390. ret = ext4_xattr_ibody_find(dir, &i, &is);
  1391. if (ret)
  1392. goto out;
  1393. if (!ext4_has_inline_data(dir)) {
  1394. *has_inline_data = 0;
  1395. goto out;
  1396. }
  1397. inline_start = (void *)ext4_raw_inode(&is.iloc)->i_block +
  1398. EXT4_INLINE_DOTDOT_SIZE;
  1399. inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
  1400. ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size,
  1401. dir, fname, 0, res_dir);
  1402. if (ret == 1)
  1403. goto out_find;
  1404. if (ret < 0)
  1405. goto out;
  1406. if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
  1407. goto out;
  1408. inline_start = ext4_get_inline_xattr_pos(dir, &is.iloc);
  1409. inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
  1410. ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size,
  1411. dir, fname, 0, res_dir);
  1412. if (ret == 1)
  1413. goto out_find;
  1414. out:
  1415. brelse(is.iloc.bh);
  1416. if (ret < 0)
  1417. is.iloc.bh = ERR_PTR(ret);
  1418. else
  1419. is.iloc.bh = NULL;
  1420. out_find:
  1421. up_read(&EXT4_I(dir)->xattr_sem);
  1422. return is.iloc.bh;
  1423. }
  1424. int ext4_delete_inline_entry(handle_t *handle,
  1425. struct inode *dir,
  1426. struct ext4_dir_entry_2 *de_del,
  1427. struct buffer_head *bh,
  1428. int *has_inline_data)
  1429. {
  1430. int err, inline_size, no_expand;
  1431. struct ext4_iloc iloc;
  1432. void *inline_start;
  1433. err = ext4_get_inode_loc(dir, &iloc);
  1434. if (err)
  1435. return err;
  1436. ext4_write_lock_xattr(dir, &no_expand);
  1437. if (!ext4_has_inline_data(dir)) {
  1438. *has_inline_data = 0;
  1439. goto out;
  1440. }
  1441. if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
  1442. EXT4_MIN_INLINE_DATA_SIZE) {
  1443. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1444. EXT4_INLINE_DOTDOT_SIZE;
  1445. inline_size = EXT4_MIN_INLINE_DATA_SIZE -
  1446. EXT4_INLINE_DOTDOT_SIZE;
  1447. } else {
  1448. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1449. inline_size = ext4_get_inline_size(dir) -
  1450. EXT4_MIN_INLINE_DATA_SIZE;
  1451. }
  1452. BUFFER_TRACE(bh, "get_write_access");
  1453. err = ext4_journal_get_write_access(handle, dir->i_sb, bh,
  1454. EXT4_JTR_NONE);
  1455. if (err)
  1456. goto out;
  1457. err = ext4_generic_delete_entry(dir, de_del, bh,
  1458. inline_start, inline_size, 0);
  1459. if (err)
  1460. goto out;
  1461. ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
  1462. out:
  1463. ext4_write_unlock_xattr(dir, &no_expand);
  1464. if (likely(err == 0))
  1465. err = ext4_mark_inode_dirty(handle, dir);
  1466. brelse(iloc.bh);
  1467. if (err != -ENOENT)
  1468. ext4_std_error(dir->i_sb, err);
  1469. return err;
  1470. }
  1471. /*
  1472. * Get the inline dentry at offset.
  1473. */
  1474. static inline struct ext4_dir_entry_2 *
  1475. ext4_get_inline_entry(struct inode *inode,
  1476. struct ext4_iloc *iloc,
  1477. unsigned int offset,
  1478. void **inline_start,
  1479. int *inline_size)
  1480. {
  1481. void *inline_pos;
  1482. BUG_ON(offset > ext4_get_inline_size(inode));
  1483. if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
  1484. inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
  1485. *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
  1486. } else {
  1487. inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
  1488. offset -= EXT4_MIN_INLINE_DATA_SIZE;
  1489. *inline_size = ext4_get_inline_size(inode) -
  1490. EXT4_MIN_INLINE_DATA_SIZE;
  1491. }
  1492. if (inline_start)
  1493. *inline_start = inline_pos;
  1494. return (struct ext4_dir_entry_2 *)(inline_pos + offset);
  1495. }
  1496. bool empty_inline_dir(struct inode *dir, int *has_inline_data)
  1497. {
  1498. int err, inline_size;
  1499. struct ext4_iloc iloc;
  1500. size_t inline_len;
  1501. void *inline_pos;
  1502. unsigned int offset;
  1503. struct ext4_dir_entry_2 *de;
  1504. bool ret = false;
  1505. err = ext4_get_inode_loc(dir, &iloc);
  1506. if (err) {
  1507. EXT4_ERROR_INODE_ERR(dir, -err,
  1508. "error %d getting inode %lu block",
  1509. err, dir->i_ino);
  1510. return false;
  1511. }
  1512. down_read(&EXT4_I(dir)->xattr_sem);
  1513. if (!ext4_has_inline_data(dir)) {
  1514. *has_inline_data = 0;
  1515. ret = true;
  1516. goto out;
  1517. }
  1518. de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1519. if (!le32_to_cpu(de->inode)) {
  1520. ext4_warning(dir->i_sb,
  1521. "bad inline directory (dir #%lu) - no `..'",
  1522. dir->i_ino);
  1523. goto out;
  1524. }
  1525. inline_len = ext4_get_inline_size(dir);
  1526. offset = EXT4_INLINE_DOTDOT_SIZE;
  1527. while (offset < inline_len) {
  1528. de = ext4_get_inline_entry(dir, &iloc, offset,
  1529. &inline_pos, &inline_size);
  1530. if (ext4_check_dir_entry(dir, NULL, de,
  1531. iloc.bh, inline_pos,
  1532. inline_size, offset)) {
  1533. ext4_warning(dir->i_sb,
  1534. "bad inline directory (dir #%lu) - "
  1535. "inode %u, rec_len %u, name_len %d"
  1536. "inline size %d",
  1537. dir->i_ino, le32_to_cpu(de->inode),
  1538. le16_to_cpu(de->rec_len), de->name_len,
  1539. inline_size);
  1540. goto out;
  1541. }
  1542. if (le32_to_cpu(de->inode)) {
  1543. goto out;
  1544. }
  1545. offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
  1546. }
  1547. ret = true;
  1548. out:
  1549. up_read(&EXT4_I(dir)->xattr_sem);
  1550. brelse(iloc.bh);
  1551. return ret;
  1552. }
  1553. int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
  1554. {
  1555. int ret, no_expand;
  1556. ext4_write_lock_xattr(inode, &no_expand);
  1557. ret = ext4_destroy_inline_data_nolock(handle, inode);
  1558. ext4_write_unlock_xattr(inode, &no_expand);
  1559. return ret;
  1560. }
  1561. int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
  1562. {
  1563. __u64 addr;
  1564. int error = -EAGAIN;
  1565. struct ext4_iloc iloc;
  1566. down_read(&EXT4_I(inode)->xattr_sem);
  1567. if (!ext4_has_inline_data(inode))
  1568. goto out;
  1569. error = ext4_get_inode_loc(inode, &iloc);
  1570. if (error)
  1571. goto out;
  1572. addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
  1573. addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
  1574. addr += offsetof(struct ext4_inode, i_block);
  1575. brelse(iloc.bh);
  1576. iomap->addr = addr;
  1577. iomap->offset = 0;
  1578. iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
  1579. i_size_read(inode));
  1580. iomap->type = IOMAP_INLINE;
  1581. iomap->flags = 0;
  1582. out:
  1583. up_read(&EXT4_I(inode)->xattr_sem);
  1584. return error;
  1585. }
  1586. int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
  1587. {
  1588. handle_t *handle;
  1589. int inline_size, value_len, needed_blocks, no_expand, err = 0;
  1590. size_t i_size;
  1591. void *value = NULL;
  1592. struct ext4_xattr_ibody_find is = {
  1593. .s = { .not_found = -ENODATA, },
  1594. };
  1595. struct ext4_xattr_info i = {
  1596. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  1597. .name = EXT4_XATTR_SYSTEM_DATA,
  1598. };
  1599. needed_blocks = ext4_chunk_trans_extent(inode, 1);
  1600. handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
  1601. if (IS_ERR(handle))
  1602. return PTR_ERR(handle);
  1603. ext4_write_lock_xattr(inode, &no_expand);
  1604. if (!ext4_has_inline_data(inode)) {
  1605. ext4_write_unlock_xattr(inode, &no_expand);
  1606. *has_inline = 0;
  1607. ext4_journal_stop(handle);
  1608. return 0;
  1609. }
  1610. if ((err = ext4_orphan_add(handle, inode)) != 0)
  1611. goto out;
  1612. if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
  1613. goto out;
  1614. down_write(&EXT4_I(inode)->i_data_sem);
  1615. i_size = inode->i_size;
  1616. inline_size = ext4_get_inline_size(inode);
  1617. EXT4_I(inode)->i_disksize = i_size;
  1618. if (i_size < inline_size) {
  1619. /*
  1620. * if there's inline data to truncate and this file was
  1621. * converted to extents after that inline data was written,
  1622. * the extent status cache must be cleared to avoid leaving
  1623. * behind stale delayed allocated extent entries
  1624. */
  1625. if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
  1626. ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
  1627. /* Clear the content in the xattr space. */
  1628. if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
  1629. if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
  1630. goto out_error;
  1631. if (is.s.not_found) {
  1632. EXT4_ERROR_INODE(inode,
  1633. "missing inline data xattr");
  1634. err = -EFSCORRUPTED;
  1635. goto out_error;
  1636. }
  1637. value_len = le32_to_cpu(is.s.here->e_value_size);
  1638. value = kmalloc(value_len, GFP_NOFS);
  1639. if (!value) {
  1640. err = -ENOMEM;
  1641. goto out_error;
  1642. }
  1643. err = ext4_xattr_ibody_get(inode, i.name_index,
  1644. i.name, value, value_len);
  1645. if (err <= 0)
  1646. goto out_error;
  1647. i.value = value;
  1648. i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
  1649. i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
  1650. err = ext4_xattr_ibody_set(handle, inode, &i, &is);
  1651. if (err)
  1652. goto out_error;
  1653. }
  1654. /* Clear the content within i_blocks. */
  1655. if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
  1656. void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
  1657. memset(p + i_size, 0,
  1658. EXT4_MIN_INLINE_DATA_SIZE - i_size);
  1659. }
  1660. EXT4_I(inode)->i_inline_size = i_size <
  1661. EXT4_MIN_INLINE_DATA_SIZE ?
  1662. EXT4_MIN_INLINE_DATA_SIZE : i_size;
  1663. }
  1664. out_error:
  1665. up_write(&EXT4_I(inode)->i_data_sem);
  1666. out:
  1667. brelse(is.iloc.bh);
  1668. ext4_write_unlock_xattr(inode, &no_expand);
  1669. kfree(value);
  1670. if (inode->i_nlink)
  1671. ext4_orphan_del(handle, inode);
  1672. if (err == 0) {
  1673. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  1674. err = ext4_mark_inode_dirty(handle, inode);
  1675. if (IS_SYNC(inode))
  1676. ext4_handle_sync(handle);
  1677. }
  1678. ext4_journal_stop(handle);
  1679. return err;
  1680. }
  1681. int ext4_convert_inline_data(struct inode *inode)
  1682. {
  1683. int error, needed_blocks, no_expand;
  1684. handle_t *handle;
  1685. struct ext4_iloc iloc;
  1686. if (!ext4_has_inline_data(inode)) {
  1687. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  1688. return 0;
  1689. } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
  1690. /*
  1691. * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is
  1692. * cleared. This means we are in the middle of moving of
  1693. * inline data to delay allocated block. Just force writeout
  1694. * here to finish conversion.
  1695. */
  1696. error = filemap_flush(inode->i_mapping);
  1697. if (error)
  1698. return error;
  1699. if (!ext4_has_inline_data(inode))
  1700. return 0;
  1701. }
  1702. needed_blocks = ext4_chunk_trans_extent(inode, 1);
  1703. iloc.bh = NULL;
  1704. error = ext4_get_inode_loc(inode, &iloc);
  1705. if (error)
  1706. return error;
  1707. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
  1708. if (IS_ERR(handle)) {
  1709. error = PTR_ERR(handle);
  1710. goto out_free;
  1711. }
  1712. ext4_write_lock_xattr(inode, &no_expand);
  1713. if (ext4_has_inline_data(inode))
  1714. error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
  1715. ext4_write_unlock_xattr(inode, &no_expand);
  1716. ext4_journal_stop(handle);
  1717. out_free:
  1718. brelse(iloc.bh);
  1719. return error;
  1720. }