inline.c 49 KB

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