inline.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  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. flags |= AOP_FLAG_NOFS;
  568. page = grab_cache_page_write_begin(mapping, 0, flags);
  569. if (!page) {
  570. ret = -ENOMEM;
  571. goto out;
  572. }
  573. *pagep = page;
  574. down_read(&EXT4_I(inode)->xattr_sem);
  575. if (!ext4_has_inline_data(inode)) {
  576. ret = 0;
  577. unlock_page(page);
  578. put_page(page);
  579. goto out_up_read;
  580. }
  581. if (!PageUptodate(page)) {
  582. ret = ext4_read_inline_page(inode, page);
  583. if (ret < 0)
  584. goto out_up_read;
  585. }
  586. ret = 1;
  587. handle = NULL;
  588. out_up_read:
  589. up_read(&EXT4_I(inode)->xattr_sem);
  590. out:
  591. if (handle)
  592. ext4_journal_stop(handle);
  593. brelse(iloc.bh);
  594. return ret;
  595. convert:
  596. return ext4_convert_inline_data_to_extent(mapping,
  597. inode, flags);
  598. }
  599. int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
  600. unsigned copied, struct page *page)
  601. {
  602. int ret, no_expand;
  603. void *kaddr;
  604. struct ext4_iloc iloc;
  605. if (unlikely(copied < len)) {
  606. if (!PageUptodate(page)) {
  607. copied = 0;
  608. goto out;
  609. }
  610. }
  611. ret = ext4_get_inode_loc(inode, &iloc);
  612. if (ret) {
  613. ext4_std_error(inode->i_sb, ret);
  614. copied = 0;
  615. goto out;
  616. }
  617. ext4_write_lock_xattr(inode, &no_expand);
  618. BUG_ON(!ext4_has_inline_data(inode));
  619. kaddr = kmap_atomic(page);
  620. ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
  621. kunmap_atomic(kaddr);
  622. SetPageUptodate(page);
  623. /* clear page dirty so that writepages wouldn't work for us. */
  624. ClearPageDirty(page);
  625. ext4_write_unlock_xattr(inode, &no_expand);
  626. brelse(iloc.bh);
  627. out:
  628. return copied;
  629. }
  630. struct buffer_head *
  631. ext4_journalled_write_inline_data(struct inode *inode,
  632. unsigned len,
  633. struct page *page)
  634. {
  635. int ret, no_expand;
  636. void *kaddr;
  637. struct ext4_iloc iloc;
  638. ret = ext4_get_inode_loc(inode, &iloc);
  639. if (ret) {
  640. ext4_std_error(inode->i_sb, ret);
  641. return NULL;
  642. }
  643. ext4_write_lock_xattr(inode, &no_expand);
  644. kaddr = kmap_atomic(page);
  645. ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
  646. kunmap_atomic(kaddr);
  647. ext4_write_unlock_xattr(inode, &no_expand);
  648. return iloc.bh;
  649. }
  650. /*
  651. * Try to make the page cache and handle ready for the inline data case.
  652. * We can call this function in 2 cases:
  653. * 1. The inode is created and the first write exceeds inline size. We can
  654. * clear the inode state safely.
  655. * 2. The inode has inline data, then we need to read the data, make it
  656. * update and dirty so that ext4_da_writepages can handle it. We don't
  657. * need to start the journal since the file's metatdata isn't changed now.
  658. */
  659. static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
  660. struct inode *inode,
  661. unsigned flags,
  662. void **fsdata)
  663. {
  664. int ret = 0, inline_size;
  665. struct page *page;
  666. page = grab_cache_page_write_begin(mapping, 0, flags);
  667. if (!page)
  668. return -ENOMEM;
  669. down_read(&EXT4_I(inode)->xattr_sem);
  670. if (!ext4_has_inline_data(inode)) {
  671. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  672. goto out;
  673. }
  674. inline_size = ext4_get_inline_size(inode);
  675. if (!PageUptodate(page)) {
  676. ret = ext4_read_inline_page(inode, page);
  677. if (ret < 0)
  678. goto out;
  679. }
  680. ret = __block_write_begin(page, 0, inline_size,
  681. ext4_da_get_block_prep);
  682. if (ret) {
  683. up_read(&EXT4_I(inode)->xattr_sem);
  684. unlock_page(page);
  685. put_page(page);
  686. ext4_truncate_failed_write(inode);
  687. return ret;
  688. }
  689. SetPageDirty(page);
  690. SetPageUptodate(page);
  691. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  692. *fsdata = (void *)CONVERT_INLINE_DATA;
  693. out:
  694. up_read(&EXT4_I(inode)->xattr_sem);
  695. if (page) {
  696. unlock_page(page);
  697. put_page(page);
  698. }
  699. return ret;
  700. }
  701. /*
  702. * Prepare the write for the inline data.
  703. * If the the data can be written into the inode, we just read
  704. * the page and make it uptodate, and start the journal.
  705. * Otherwise read the page, makes it dirty so that it can be
  706. * handle in writepages(the i_disksize update is left to the
  707. * normal ext4_da_write_end).
  708. */
  709. int ext4_da_write_inline_data_begin(struct address_space *mapping,
  710. struct inode *inode,
  711. loff_t pos, unsigned len,
  712. unsigned flags,
  713. struct page **pagep,
  714. void **fsdata)
  715. {
  716. int ret, inline_size;
  717. handle_t *handle;
  718. struct page *page;
  719. struct ext4_iloc iloc;
  720. int retries;
  721. ret = ext4_get_inode_loc(inode, &iloc);
  722. if (ret)
  723. return ret;
  724. retry_journal:
  725. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  726. if (IS_ERR(handle)) {
  727. ret = PTR_ERR(handle);
  728. goto out;
  729. }
  730. inline_size = ext4_get_max_inline_size(inode);
  731. ret = -ENOSPC;
  732. if (inline_size >= pos + len) {
  733. ret = ext4_prepare_inline_data(handle, inode, pos + len);
  734. if (ret && ret != -ENOSPC)
  735. goto out_journal;
  736. }
  737. /*
  738. * We cannot recurse into the filesystem as the transaction
  739. * is already started.
  740. */
  741. flags |= AOP_FLAG_NOFS;
  742. if (ret == -ENOSPC) {
  743. ext4_journal_stop(handle);
  744. ret = ext4_da_convert_inline_data_to_extent(mapping,
  745. inode,
  746. flags,
  747. fsdata);
  748. if (ret == -ENOSPC &&
  749. ext4_should_retry_alloc(inode->i_sb, &retries))
  750. goto retry_journal;
  751. goto out;
  752. }
  753. page = grab_cache_page_write_begin(mapping, 0, flags);
  754. if (!page) {
  755. ret = -ENOMEM;
  756. goto out_journal;
  757. }
  758. down_read(&EXT4_I(inode)->xattr_sem);
  759. if (!ext4_has_inline_data(inode)) {
  760. ret = 0;
  761. goto out_release_page;
  762. }
  763. if (!PageUptodate(page)) {
  764. ret = ext4_read_inline_page(inode, page);
  765. if (ret < 0)
  766. goto out_release_page;
  767. }
  768. up_read(&EXT4_I(inode)->xattr_sem);
  769. *pagep = page;
  770. brelse(iloc.bh);
  771. return 1;
  772. out_release_page:
  773. up_read(&EXT4_I(inode)->xattr_sem);
  774. unlock_page(page);
  775. put_page(page);
  776. out_journal:
  777. ext4_journal_stop(handle);
  778. out:
  779. brelse(iloc.bh);
  780. return ret;
  781. }
  782. int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
  783. unsigned len, unsigned copied,
  784. struct page *page)
  785. {
  786. int i_size_changed = 0;
  787. int ret;
  788. ret = ext4_write_inline_data_end(inode, pos, len, copied, page);
  789. if (ret < 0) {
  790. unlock_page(page);
  791. put_page(page);
  792. return ret;
  793. }
  794. copied = ret;
  795. /*
  796. * No need to use i_size_read() here, the i_size
  797. * cannot change under us because we hold i_mutex.
  798. *
  799. * But it's important to update i_size while still holding page lock:
  800. * page writeout could otherwise come in and zero beyond i_size.
  801. */
  802. if (pos+copied > inode->i_size) {
  803. i_size_write(inode, pos+copied);
  804. i_size_changed = 1;
  805. }
  806. unlock_page(page);
  807. put_page(page);
  808. /*
  809. * Don't mark the inode dirty under page lock. First, it unnecessarily
  810. * makes the holding time of page lock longer. Second, it forces lock
  811. * ordering of page lock and transaction start for journaling
  812. * filesystems.
  813. */
  814. if (i_size_changed)
  815. mark_inode_dirty(inode);
  816. return copied;
  817. }
  818. #ifdef INLINE_DIR_DEBUG
  819. void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
  820. void *inline_start, int inline_size)
  821. {
  822. int offset;
  823. unsigned short de_len;
  824. struct ext4_dir_entry_2 *de = inline_start;
  825. void *dlimit = inline_start + inline_size;
  826. trace_printk("inode %lu\n", dir->i_ino);
  827. offset = 0;
  828. while ((void *)de < dlimit) {
  829. de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
  830. trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n",
  831. offset, de_len, de->name_len, de->name,
  832. de->name_len, le32_to_cpu(de->inode));
  833. if (ext4_check_dir_entry(dir, NULL, de, bh,
  834. inline_start, inline_size, offset))
  835. BUG();
  836. offset += de_len;
  837. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  838. }
  839. }
  840. #else
  841. #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
  842. #endif
  843. /*
  844. * Add a new entry into a inline dir.
  845. * It will return -ENOSPC if no space is available, and -EIO
  846. * and -EEXIST if directory entry already exists.
  847. */
  848. static int ext4_add_dirent_to_inline(handle_t *handle,
  849. struct ext4_filename *fname,
  850. struct inode *dir,
  851. struct inode *inode,
  852. struct ext4_iloc *iloc,
  853. void *inline_start, int inline_size)
  854. {
  855. int err;
  856. struct ext4_dir_entry_2 *de;
  857. err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start,
  858. inline_size, fname, &de);
  859. if (err)
  860. return err;
  861. BUFFER_TRACE(iloc->bh, "get_write_access");
  862. err = ext4_journal_get_write_access(handle, iloc->bh);
  863. if (err)
  864. return err;
  865. ext4_insert_dentry(inode, de, inline_size, fname);
  866. ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
  867. /*
  868. * XXX shouldn't update any times until successful
  869. * completion of syscall, but too many callers depend
  870. * on this.
  871. *
  872. * XXX similarly, too many callers depend on
  873. * ext4_new_inode() setting the times, but error
  874. * recovery deletes the inode, so the worst that can
  875. * happen is that the times are slightly out of date
  876. * and/or different from the directory change time.
  877. */
  878. dir->i_mtime = dir->i_ctime = current_time(dir);
  879. ext4_update_dx_flag(dir);
  880. inode_inc_iversion(dir);
  881. return 1;
  882. }
  883. static void *ext4_get_inline_xattr_pos(struct inode *inode,
  884. struct ext4_iloc *iloc)
  885. {
  886. struct ext4_xattr_entry *entry;
  887. struct ext4_xattr_ibody_header *header;
  888. BUG_ON(!EXT4_I(inode)->i_inline_off);
  889. header = IHDR(inode, ext4_raw_inode(iloc));
  890. entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
  891. EXT4_I(inode)->i_inline_off);
  892. return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
  893. }
  894. /* Set the final de to cover the whole block. */
  895. static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
  896. {
  897. struct ext4_dir_entry_2 *de, *prev_de;
  898. void *limit;
  899. int de_len;
  900. de = (struct ext4_dir_entry_2 *)de_buf;
  901. if (old_size) {
  902. limit = de_buf + old_size;
  903. do {
  904. prev_de = de;
  905. de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
  906. de_buf += de_len;
  907. de = (struct ext4_dir_entry_2 *)de_buf;
  908. } while (de_buf < limit);
  909. prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
  910. old_size, new_size);
  911. } else {
  912. /* this is just created, so create an empty entry. */
  913. de->inode = 0;
  914. de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
  915. }
  916. }
  917. static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
  918. struct ext4_iloc *iloc)
  919. {
  920. int ret;
  921. int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
  922. int new_size = get_max_inline_xattr_value_size(dir, iloc);
  923. if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
  924. return -ENOSPC;
  925. ret = ext4_update_inline_data(handle, dir,
  926. new_size + EXT4_MIN_INLINE_DATA_SIZE);
  927. if (ret)
  928. return ret;
  929. ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
  930. EXT4_I(dir)->i_inline_size -
  931. EXT4_MIN_INLINE_DATA_SIZE);
  932. dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
  933. return 0;
  934. }
  935. static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
  936. struct ext4_iloc *iloc,
  937. void *buf, int inline_size)
  938. {
  939. ext4_create_inline_data(handle, inode, inline_size);
  940. ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
  941. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  942. }
  943. static int ext4_finish_convert_inline_dir(handle_t *handle,
  944. struct inode *inode,
  945. struct buffer_head *dir_block,
  946. void *buf,
  947. int inline_size)
  948. {
  949. int err, csum_size = 0, header_size = 0;
  950. struct ext4_dir_entry_2 *de;
  951. struct ext4_dir_entry_tail *t;
  952. void *target = dir_block->b_data;
  953. /*
  954. * First create "." and ".." and then copy the dir information
  955. * back to the block.
  956. */
  957. de = (struct ext4_dir_entry_2 *)target;
  958. de = ext4_init_dot_dotdot(inode, de,
  959. inode->i_sb->s_blocksize, csum_size,
  960. le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
  961. header_size = (void *)de - target;
  962. memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
  963. inline_size - EXT4_INLINE_DOTDOT_SIZE);
  964. if (ext4_has_metadata_csum(inode->i_sb))
  965. csum_size = sizeof(struct ext4_dir_entry_tail);
  966. inode->i_size = inode->i_sb->s_blocksize;
  967. i_size_write(inode, inode->i_sb->s_blocksize);
  968. EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  969. ext4_update_final_de(dir_block->b_data,
  970. inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
  971. inode->i_sb->s_blocksize - csum_size);
  972. if (csum_size) {
  973. t = EXT4_DIRENT_TAIL(dir_block->b_data,
  974. inode->i_sb->s_blocksize);
  975. initialize_dirent_tail(t, inode->i_sb->s_blocksize);
  976. }
  977. set_buffer_uptodate(dir_block);
  978. err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
  979. if (err)
  980. return err;
  981. set_buffer_verified(dir_block);
  982. return ext4_mark_inode_dirty(handle, inode);
  983. }
  984. static int ext4_convert_inline_data_nolock(handle_t *handle,
  985. struct inode *inode,
  986. struct ext4_iloc *iloc)
  987. {
  988. int error;
  989. void *buf = NULL;
  990. struct buffer_head *data_bh = NULL;
  991. struct ext4_map_blocks map;
  992. int inline_size;
  993. inline_size = ext4_get_inline_size(inode);
  994. buf = kmalloc(inline_size, GFP_NOFS);
  995. if (!buf) {
  996. error = -ENOMEM;
  997. goto out;
  998. }
  999. error = ext4_read_inline_data(inode, buf, inline_size, iloc);
  1000. if (error < 0)
  1001. goto out;
  1002. /*
  1003. * Make sure the inline directory entries pass checks before we try to
  1004. * convert them, so that we avoid touching stuff that needs fsck.
  1005. */
  1006. if (S_ISDIR(inode->i_mode)) {
  1007. error = ext4_check_all_de(inode, iloc->bh,
  1008. buf + EXT4_INLINE_DOTDOT_SIZE,
  1009. inline_size - EXT4_INLINE_DOTDOT_SIZE);
  1010. if (error)
  1011. goto out;
  1012. }
  1013. error = ext4_destroy_inline_data_nolock(handle, inode);
  1014. if (error)
  1015. goto out;
  1016. map.m_lblk = 0;
  1017. map.m_len = 1;
  1018. map.m_flags = 0;
  1019. error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
  1020. if (error < 0)
  1021. goto out_restore;
  1022. if (!(map.m_flags & EXT4_MAP_MAPPED)) {
  1023. error = -EIO;
  1024. goto out_restore;
  1025. }
  1026. data_bh = sb_getblk(inode->i_sb, map.m_pblk);
  1027. if (!data_bh) {
  1028. error = -ENOMEM;
  1029. goto out_restore;
  1030. }
  1031. lock_buffer(data_bh);
  1032. error = ext4_journal_get_create_access(handle, data_bh);
  1033. if (error) {
  1034. unlock_buffer(data_bh);
  1035. error = -EIO;
  1036. goto out_restore;
  1037. }
  1038. memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
  1039. if (!S_ISDIR(inode->i_mode)) {
  1040. memcpy(data_bh->b_data, buf, inline_size);
  1041. set_buffer_uptodate(data_bh);
  1042. error = ext4_handle_dirty_metadata(handle,
  1043. inode, data_bh);
  1044. } else {
  1045. error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
  1046. buf, inline_size);
  1047. }
  1048. unlock_buffer(data_bh);
  1049. out_restore:
  1050. if (error)
  1051. ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
  1052. out:
  1053. brelse(data_bh);
  1054. kfree(buf);
  1055. return error;
  1056. }
  1057. /*
  1058. * Try to add the new entry to the inline data.
  1059. * If succeeds, return 0. If not, extended the inline dir and copied data to
  1060. * the new created block.
  1061. */
  1062. int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname,
  1063. struct inode *dir, struct inode *inode)
  1064. {
  1065. int ret, inline_size, no_expand;
  1066. void *inline_start;
  1067. struct ext4_iloc iloc;
  1068. ret = ext4_get_inode_loc(dir, &iloc);
  1069. if (ret)
  1070. return ret;
  1071. ext4_write_lock_xattr(dir, &no_expand);
  1072. if (!ext4_has_inline_data(dir))
  1073. goto out;
  1074. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1075. EXT4_INLINE_DOTDOT_SIZE;
  1076. inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
  1077. ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc,
  1078. inline_start, inline_size);
  1079. if (ret != -ENOSPC)
  1080. goto out;
  1081. /* check whether it can be inserted to inline xattr space. */
  1082. inline_size = EXT4_I(dir)->i_inline_size -
  1083. EXT4_MIN_INLINE_DATA_SIZE;
  1084. if (!inline_size) {
  1085. /* Try to use the xattr space.*/
  1086. ret = ext4_update_inline_dir(handle, dir, &iloc);
  1087. if (ret && ret != -ENOSPC)
  1088. goto out;
  1089. inline_size = EXT4_I(dir)->i_inline_size -
  1090. EXT4_MIN_INLINE_DATA_SIZE;
  1091. }
  1092. if (inline_size) {
  1093. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1094. ret = ext4_add_dirent_to_inline(handle, fname, dir,
  1095. inode, &iloc, inline_start,
  1096. inline_size);
  1097. if (ret != -ENOSPC)
  1098. goto out;
  1099. }
  1100. /*
  1101. * The inline space is filled up, so create a new block for it.
  1102. * As the extent tree will be created, we have to save the inline
  1103. * dir first.
  1104. */
  1105. ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
  1106. out:
  1107. ext4_write_unlock_xattr(dir, &no_expand);
  1108. ext4_mark_inode_dirty(handle, dir);
  1109. brelse(iloc.bh);
  1110. return ret;
  1111. }
  1112. /*
  1113. * This function fills a red-black tree with information from an
  1114. * inlined dir. It returns the number directory entries loaded
  1115. * into the tree. If there is an error it is returned in err.
  1116. */
  1117. int htree_inlinedir_to_tree(struct file *dir_file,
  1118. struct inode *dir, ext4_lblk_t block,
  1119. struct dx_hash_info *hinfo,
  1120. __u32 start_hash, __u32 start_minor_hash,
  1121. int *has_inline_data)
  1122. {
  1123. int err = 0, count = 0;
  1124. unsigned int parent_ino;
  1125. int pos;
  1126. struct ext4_dir_entry_2 *de;
  1127. struct inode *inode = file_inode(dir_file);
  1128. int ret, inline_size = 0;
  1129. struct ext4_iloc iloc;
  1130. void *dir_buf = NULL;
  1131. struct ext4_dir_entry_2 fake;
  1132. struct fscrypt_str tmp_str;
  1133. ret = ext4_get_inode_loc(inode, &iloc);
  1134. if (ret)
  1135. return ret;
  1136. down_read(&EXT4_I(inode)->xattr_sem);
  1137. if (!ext4_has_inline_data(inode)) {
  1138. up_read(&EXT4_I(inode)->xattr_sem);
  1139. *has_inline_data = 0;
  1140. goto out;
  1141. }
  1142. inline_size = ext4_get_inline_size(inode);
  1143. dir_buf = kmalloc(inline_size, GFP_NOFS);
  1144. if (!dir_buf) {
  1145. ret = -ENOMEM;
  1146. up_read(&EXT4_I(inode)->xattr_sem);
  1147. goto out;
  1148. }
  1149. ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
  1150. up_read(&EXT4_I(inode)->xattr_sem);
  1151. if (ret < 0)
  1152. goto out;
  1153. pos = 0;
  1154. parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
  1155. while (pos < inline_size) {
  1156. /*
  1157. * As inlined dir doesn't store any information about '.' and
  1158. * only the inode number of '..' is stored, we have to handle
  1159. * them differently.
  1160. */
  1161. if (pos == 0) {
  1162. fake.inode = cpu_to_le32(inode->i_ino);
  1163. fake.name_len = 1;
  1164. strcpy(fake.name, ".");
  1165. fake.rec_len = ext4_rec_len_to_disk(
  1166. EXT4_DIR_REC_LEN(fake.name_len),
  1167. inline_size);
  1168. ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
  1169. de = &fake;
  1170. pos = EXT4_INLINE_DOTDOT_OFFSET;
  1171. } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
  1172. fake.inode = cpu_to_le32(parent_ino);
  1173. fake.name_len = 2;
  1174. strcpy(fake.name, "..");
  1175. fake.rec_len = ext4_rec_len_to_disk(
  1176. EXT4_DIR_REC_LEN(fake.name_len),
  1177. inline_size);
  1178. ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
  1179. de = &fake;
  1180. pos = EXT4_INLINE_DOTDOT_SIZE;
  1181. } else {
  1182. de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
  1183. pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
  1184. if (ext4_check_dir_entry(inode, dir_file, de,
  1185. iloc.bh, dir_buf,
  1186. inline_size, pos)) {
  1187. ret = count;
  1188. goto out;
  1189. }
  1190. }
  1191. ext4fs_dirhash(de->name, de->name_len, hinfo);
  1192. if ((hinfo->hash < start_hash) ||
  1193. ((hinfo->hash == start_hash) &&
  1194. (hinfo->minor_hash < start_minor_hash)))
  1195. continue;
  1196. if (de->inode == 0)
  1197. continue;
  1198. tmp_str.name = de->name;
  1199. tmp_str.len = de->name_len;
  1200. err = ext4_htree_store_dirent(dir_file, hinfo->hash,
  1201. hinfo->minor_hash, de, &tmp_str);
  1202. if (err) {
  1203. count = err;
  1204. goto out;
  1205. }
  1206. count++;
  1207. }
  1208. ret = count;
  1209. out:
  1210. kfree(dir_buf);
  1211. brelse(iloc.bh);
  1212. return ret;
  1213. }
  1214. /*
  1215. * So this function is called when the volume is mkfsed with
  1216. * dir_index disabled. In order to keep f_pos persistent
  1217. * after we convert from an inlined dir to a blocked based,
  1218. * we just pretend that we are a normal dir and return the
  1219. * offset as if '.' and '..' really take place.
  1220. *
  1221. */
  1222. int ext4_read_inline_dir(struct file *file,
  1223. struct dir_context *ctx,
  1224. int *has_inline_data)
  1225. {
  1226. unsigned int offset, parent_ino;
  1227. int i;
  1228. struct ext4_dir_entry_2 *de;
  1229. struct super_block *sb;
  1230. struct inode *inode = file_inode(file);
  1231. int ret, inline_size = 0;
  1232. struct ext4_iloc iloc;
  1233. void *dir_buf = NULL;
  1234. int dotdot_offset, dotdot_size, extra_offset, extra_size;
  1235. ret = ext4_get_inode_loc(inode, &iloc);
  1236. if (ret)
  1237. return ret;
  1238. down_read(&EXT4_I(inode)->xattr_sem);
  1239. if (!ext4_has_inline_data(inode)) {
  1240. up_read(&EXT4_I(inode)->xattr_sem);
  1241. *has_inline_data = 0;
  1242. goto out;
  1243. }
  1244. inline_size = ext4_get_inline_size(inode);
  1245. dir_buf = kmalloc(inline_size, GFP_NOFS);
  1246. if (!dir_buf) {
  1247. ret = -ENOMEM;
  1248. up_read(&EXT4_I(inode)->xattr_sem);
  1249. goto out;
  1250. }
  1251. ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
  1252. up_read(&EXT4_I(inode)->xattr_sem);
  1253. if (ret < 0)
  1254. goto out;
  1255. ret = 0;
  1256. sb = inode->i_sb;
  1257. parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
  1258. offset = ctx->pos;
  1259. /*
  1260. * dotdot_offset and dotdot_size is the real offset and
  1261. * size for ".." and "." if the dir is block based while
  1262. * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
  1263. * So we will use extra_offset and extra_size to indicate them
  1264. * during the inline dir iteration.
  1265. */
  1266. dotdot_offset = EXT4_DIR_REC_LEN(1);
  1267. dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
  1268. extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
  1269. extra_size = extra_offset + inline_size;
  1270. /*
  1271. * If the version has changed since the last call to
  1272. * readdir(2), then we might be pointing to an invalid
  1273. * dirent right now. Scan from the start of the inline
  1274. * dir to make sure.
  1275. */
  1276. if (!inode_eq_iversion(inode, file->f_version)) {
  1277. for (i = 0; i < extra_size && i < offset;) {
  1278. /*
  1279. * "." is with offset 0 and
  1280. * ".." is dotdot_offset.
  1281. */
  1282. if (!i) {
  1283. i = dotdot_offset;
  1284. continue;
  1285. } else if (i == dotdot_offset) {
  1286. i = dotdot_size;
  1287. continue;
  1288. }
  1289. /* for other entry, the real offset in
  1290. * the buf has to be tuned accordingly.
  1291. */
  1292. de = (struct ext4_dir_entry_2 *)
  1293. (dir_buf + i - extra_offset);
  1294. /* It's too expensive to do a full
  1295. * dirent test each time round this
  1296. * loop, but we do have to test at
  1297. * least that it is non-zero. A
  1298. * failure will be detected in the
  1299. * dirent test below. */
  1300. if (ext4_rec_len_from_disk(de->rec_len, extra_size)
  1301. < EXT4_DIR_REC_LEN(1))
  1302. break;
  1303. i += ext4_rec_len_from_disk(de->rec_len,
  1304. extra_size);
  1305. }
  1306. offset = i;
  1307. ctx->pos = offset;
  1308. file->f_version = inode_query_iversion(inode);
  1309. }
  1310. while (ctx->pos < extra_size) {
  1311. if (ctx->pos == 0) {
  1312. if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR))
  1313. goto out;
  1314. ctx->pos = dotdot_offset;
  1315. continue;
  1316. }
  1317. if (ctx->pos == dotdot_offset) {
  1318. if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR))
  1319. goto out;
  1320. ctx->pos = dotdot_size;
  1321. continue;
  1322. }
  1323. de = (struct ext4_dir_entry_2 *)
  1324. (dir_buf + ctx->pos - extra_offset);
  1325. if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf,
  1326. extra_size, ctx->pos))
  1327. goto out;
  1328. if (le32_to_cpu(de->inode)) {
  1329. if (!dir_emit(ctx, de->name, de->name_len,
  1330. le32_to_cpu(de->inode),
  1331. get_dtype(sb, de->file_type)))
  1332. goto out;
  1333. }
  1334. ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size);
  1335. }
  1336. out:
  1337. kfree(dir_buf);
  1338. brelse(iloc.bh);
  1339. return ret;
  1340. }
  1341. struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
  1342. struct ext4_dir_entry_2 **parent_de,
  1343. int *retval)
  1344. {
  1345. struct ext4_iloc iloc;
  1346. *retval = ext4_get_inode_loc(inode, &iloc);
  1347. if (*retval)
  1348. return NULL;
  1349. *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1350. return iloc.bh;
  1351. }
  1352. /*
  1353. * Try to create the inline data for the new dir.
  1354. * If it succeeds, return 0, otherwise return the error.
  1355. * In case of ENOSPC, the caller should create the normal disk layout dir.
  1356. */
  1357. int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
  1358. struct inode *inode)
  1359. {
  1360. int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
  1361. struct ext4_iloc iloc;
  1362. struct ext4_dir_entry_2 *de;
  1363. ret = ext4_get_inode_loc(inode, &iloc);
  1364. if (ret)
  1365. return ret;
  1366. ret = ext4_prepare_inline_data(handle, inode, inline_size);
  1367. if (ret)
  1368. goto out;
  1369. /*
  1370. * For inline dir, we only save the inode information for the ".."
  1371. * and create a fake dentry to cover the left space.
  1372. */
  1373. de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1374. de->inode = cpu_to_le32(parent->i_ino);
  1375. de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
  1376. de->inode = 0;
  1377. de->rec_len = ext4_rec_len_to_disk(
  1378. inline_size - EXT4_INLINE_DOTDOT_SIZE,
  1379. inline_size);
  1380. set_nlink(inode, 2);
  1381. inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
  1382. out:
  1383. brelse(iloc.bh);
  1384. return ret;
  1385. }
  1386. struct buffer_head *ext4_find_inline_entry(struct inode *dir,
  1387. struct ext4_filename *fname,
  1388. struct ext4_dir_entry_2 **res_dir,
  1389. int *has_inline_data)
  1390. {
  1391. int ret;
  1392. struct ext4_iloc iloc;
  1393. void *inline_start;
  1394. int inline_size;
  1395. if (ext4_get_inode_loc(dir, &iloc))
  1396. return NULL;
  1397. down_read(&EXT4_I(dir)->xattr_sem);
  1398. if (!ext4_has_inline_data(dir)) {
  1399. *has_inline_data = 0;
  1400. goto out;
  1401. }
  1402. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1403. EXT4_INLINE_DOTDOT_SIZE;
  1404. inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
  1405. ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
  1406. dir, fname, 0, res_dir);
  1407. if (ret == 1)
  1408. goto out_find;
  1409. if (ret < 0)
  1410. goto out;
  1411. if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
  1412. goto out;
  1413. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1414. inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
  1415. ret = ext4_search_dir(iloc.bh, inline_start, inline_size,
  1416. dir, fname, 0, res_dir);
  1417. if (ret == 1)
  1418. goto out_find;
  1419. out:
  1420. brelse(iloc.bh);
  1421. iloc.bh = NULL;
  1422. out_find:
  1423. up_read(&EXT4_I(dir)->xattr_sem);
  1424. return iloc.bh;
  1425. }
  1426. int ext4_delete_inline_entry(handle_t *handle,
  1427. struct inode *dir,
  1428. struct ext4_dir_entry_2 *de_del,
  1429. struct buffer_head *bh,
  1430. int *has_inline_data)
  1431. {
  1432. int err, inline_size, no_expand;
  1433. struct ext4_iloc iloc;
  1434. void *inline_start;
  1435. err = ext4_get_inode_loc(dir, &iloc);
  1436. if (err)
  1437. return err;
  1438. ext4_write_lock_xattr(dir, &no_expand);
  1439. if (!ext4_has_inline_data(dir)) {
  1440. *has_inline_data = 0;
  1441. goto out;
  1442. }
  1443. if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
  1444. EXT4_MIN_INLINE_DATA_SIZE) {
  1445. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1446. EXT4_INLINE_DOTDOT_SIZE;
  1447. inline_size = EXT4_MIN_INLINE_DATA_SIZE -
  1448. EXT4_INLINE_DOTDOT_SIZE;
  1449. } else {
  1450. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1451. inline_size = ext4_get_inline_size(dir) -
  1452. EXT4_MIN_INLINE_DATA_SIZE;
  1453. }
  1454. BUFFER_TRACE(bh, "get_write_access");
  1455. err = ext4_journal_get_write_access(handle, bh);
  1456. if (err)
  1457. goto out;
  1458. err = ext4_generic_delete_entry(handle, dir, de_del, bh,
  1459. inline_start, inline_size, 0);
  1460. if (err)
  1461. goto out;
  1462. ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
  1463. out:
  1464. ext4_write_unlock_xattr(dir, &no_expand);
  1465. if (likely(err == 0))
  1466. err = ext4_mark_inode_dirty(handle, dir);
  1467. brelse(iloc.bh);
  1468. if (err != -ENOENT)
  1469. ext4_std_error(dir->i_sb, err);
  1470. return err;
  1471. }
  1472. /*
  1473. * Get the inline dentry at offset.
  1474. */
  1475. static inline struct ext4_dir_entry_2 *
  1476. ext4_get_inline_entry(struct inode *inode,
  1477. struct ext4_iloc *iloc,
  1478. unsigned int offset,
  1479. void **inline_start,
  1480. int *inline_size)
  1481. {
  1482. void *inline_pos;
  1483. BUG_ON(offset > ext4_get_inline_size(inode));
  1484. if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
  1485. inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
  1486. *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
  1487. } else {
  1488. inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
  1489. offset -= EXT4_MIN_INLINE_DATA_SIZE;
  1490. *inline_size = ext4_get_inline_size(inode) -
  1491. EXT4_MIN_INLINE_DATA_SIZE;
  1492. }
  1493. if (inline_start)
  1494. *inline_start = inline_pos;
  1495. return (struct ext4_dir_entry_2 *)(inline_pos + offset);
  1496. }
  1497. bool empty_inline_dir(struct inode *dir, int *has_inline_data)
  1498. {
  1499. int err, inline_size;
  1500. struct ext4_iloc iloc;
  1501. void *inline_pos;
  1502. unsigned int offset;
  1503. struct ext4_dir_entry_2 *de;
  1504. bool ret = true;
  1505. err = ext4_get_inode_loc(dir, &iloc);
  1506. if (err) {
  1507. EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
  1508. err, dir->i_ino);
  1509. return true;
  1510. }
  1511. down_read(&EXT4_I(dir)->xattr_sem);
  1512. if (!ext4_has_inline_data(dir)) {
  1513. *has_inline_data = 0;
  1514. goto out;
  1515. }
  1516. de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1517. if (!le32_to_cpu(de->inode)) {
  1518. ext4_warning(dir->i_sb,
  1519. "bad inline directory (dir #%lu) - no `..'",
  1520. dir->i_ino);
  1521. ret = true;
  1522. goto out;
  1523. }
  1524. offset = EXT4_INLINE_DOTDOT_SIZE;
  1525. while (offset < dir->i_size) {
  1526. de = ext4_get_inline_entry(dir, &iloc, offset,
  1527. &inline_pos, &inline_size);
  1528. if (ext4_check_dir_entry(dir, NULL, de,
  1529. iloc.bh, inline_pos,
  1530. inline_size, offset)) {
  1531. ext4_warning(dir->i_sb,
  1532. "bad inline directory (dir #%lu) - "
  1533. "inode %u, rec_len %u, name_len %d"
  1534. "inline size %d",
  1535. dir->i_ino, le32_to_cpu(de->inode),
  1536. le16_to_cpu(de->rec_len), de->name_len,
  1537. inline_size);
  1538. ret = true;
  1539. goto out;
  1540. }
  1541. if (le32_to_cpu(de->inode)) {
  1542. ret = false;
  1543. goto out;
  1544. }
  1545. offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
  1546. }
  1547. out:
  1548. up_read(&EXT4_I(dir)->xattr_sem);
  1549. brelse(iloc.bh);
  1550. return ret;
  1551. }
  1552. int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
  1553. {
  1554. int ret, no_expand;
  1555. ext4_write_lock_xattr(inode, &no_expand);
  1556. ret = ext4_destroy_inline_data_nolock(handle, inode);
  1557. ext4_write_unlock_xattr(inode, &no_expand);
  1558. return ret;
  1559. }
  1560. int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap)
  1561. {
  1562. __u64 addr;
  1563. int error = -EAGAIN;
  1564. struct ext4_iloc iloc;
  1565. down_read(&EXT4_I(inode)->xattr_sem);
  1566. if (!ext4_has_inline_data(inode))
  1567. goto out;
  1568. error = ext4_get_inode_loc(inode, &iloc);
  1569. if (error)
  1570. goto out;
  1571. addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
  1572. addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
  1573. addr += offsetof(struct ext4_inode, i_block);
  1574. brelse(iloc.bh);
  1575. iomap->addr = addr;
  1576. iomap->offset = 0;
  1577. iomap->length = min_t(loff_t, ext4_get_inline_size(inode),
  1578. i_size_read(inode));
  1579. iomap->type = IOMAP_INLINE;
  1580. iomap->flags = 0;
  1581. out:
  1582. up_read(&EXT4_I(inode)->xattr_sem);
  1583. return error;
  1584. }
  1585. int ext4_inline_data_fiemap(struct inode *inode,
  1586. struct fiemap_extent_info *fieinfo,
  1587. int *has_inline, __u64 start, __u64 len)
  1588. {
  1589. __u64 physical = 0;
  1590. __u64 inline_len;
  1591. __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
  1592. FIEMAP_EXTENT_LAST;
  1593. int error = 0;
  1594. struct ext4_iloc iloc;
  1595. down_read(&EXT4_I(inode)->xattr_sem);
  1596. if (!ext4_has_inline_data(inode)) {
  1597. *has_inline = 0;
  1598. goto out;
  1599. }
  1600. inline_len = min_t(size_t, ext4_get_inline_size(inode),
  1601. i_size_read(inode));
  1602. if (start >= inline_len)
  1603. goto out;
  1604. if (start + len < inline_len)
  1605. inline_len = start + len;
  1606. inline_len -= start;
  1607. error = ext4_get_inode_loc(inode, &iloc);
  1608. if (error)
  1609. goto out;
  1610. physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
  1611. physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
  1612. physical += offsetof(struct ext4_inode, i_block);
  1613. if (physical)
  1614. error = fiemap_fill_next_extent(fieinfo, start, physical,
  1615. inline_len, flags);
  1616. brelse(iloc.bh);
  1617. out:
  1618. up_read(&EXT4_I(inode)->xattr_sem);
  1619. return (error < 0 ? error : 0);
  1620. }
  1621. int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
  1622. {
  1623. handle_t *handle;
  1624. int inline_size, value_len, needed_blocks, no_expand, err = 0;
  1625. size_t i_size;
  1626. void *value = NULL;
  1627. struct ext4_xattr_ibody_find is = {
  1628. .s = { .not_found = -ENODATA, },
  1629. };
  1630. struct ext4_xattr_info i = {
  1631. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  1632. .name = EXT4_XATTR_SYSTEM_DATA,
  1633. };
  1634. needed_blocks = ext4_writepage_trans_blocks(inode);
  1635. handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
  1636. if (IS_ERR(handle))
  1637. return PTR_ERR(handle);
  1638. ext4_write_lock_xattr(inode, &no_expand);
  1639. if (!ext4_has_inline_data(inode)) {
  1640. *has_inline = 0;
  1641. ext4_journal_stop(handle);
  1642. return 0;
  1643. }
  1644. if ((err = ext4_orphan_add(handle, inode)) != 0)
  1645. goto out;
  1646. if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0)
  1647. goto out;
  1648. down_write(&EXT4_I(inode)->i_data_sem);
  1649. i_size = inode->i_size;
  1650. inline_size = ext4_get_inline_size(inode);
  1651. EXT4_I(inode)->i_disksize = i_size;
  1652. if (i_size < inline_size) {
  1653. /* Clear the content in the xattr space. */
  1654. if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
  1655. if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0)
  1656. goto out_error;
  1657. BUG_ON(is.s.not_found);
  1658. value_len = le32_to_cpu(is.s.here->e_value_size);
  1659. value = kmalloc(value_len, GFP_NOFS);
  1660. if (!value) {
  1661. err = -ENOMEM;
  1662. goto out_error;
  1663. }
  1664. err = ext4_xattr_ibody_get(inode, i.name_index,
  1665. i.name, value, value_len);
  1666. if (err <= 0)
  1667. goto out_error;
  1668. i.value = value;
  1669. i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
  1670. i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
  1671. err = ext4_xattr_ibody_inline_set(handle, inode,
  1672. &i, &is);
  1673. if (err)
  1674. goto out_error;
  1675. }
  1676. /* Clear the content within i_blocks. */
  1677. if (i_size < EXT4_MIN_INLINE_DATA_SIZE) {
  1678. void *p = (void *) ext4_raw_inode(&is.iloc)->i_block;
  1679. memset(p + i_size, 0,
  1680. EXT4_MIN_INLINE_DATA_SIZE - i_size);
  1681. }
  1682. EXT4_I(inode)->i_inline_size = i_size <
  1683. EXT4_MIN_INLINE_DATA_SIZE ?
  1684. EXT4_MIN_INLINE_DATA_SIZE : i_size;
  1685. }
  1686. out_error:
  1687. up_write(&EXT4_I(inode)->i_data_sem);
  1688. out:
  1689. brelse(is.iloc.bh);
  1690. ext4_write_unlock_xattr(inode, &no_expand);
  1691. kfree(value);
  1692. if (inode->i_nlink)
  1693. ext4_orphan_del(handle, inode);
  1694. if (err == 0) {
  1695. inode->i_mtime = inode->i_ctime = current_time(inode);
  1696. err = ext4_mark_inode_dirty(handle, inode);
  1697. if (IS_SYNC(inode))
  1698. ext4_handle_sync(handle);
  1699. }
  1700. ext4_journal_stop(handle);
  1701. return err;
  1702. }
  1703. int ext4_convert_inline_data(struct inode *inode)
  1704. {
  1705. int error, needed_blocks, no_expand;
  1706. handle_t *handle;
  1707. struct ext4_iloc iloc;
  1708. if (!ext4_has_inline_data(inode)) {
  1709. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  1710. return 0;
  1711. }
  1712. needed_blocks = ext4_writepage_trans_blocks(inode);
  1713. iloc.bh = NULL;
  1714. error = ext4_get_inode_loc(inode, &iloc);
  1715. if (error)
  1716. return error;
  1717. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
  1718. if (IS_ERR(handle)) {
  1719. error = PTR_ERR(handle);
  1720. goto out_free;
  1721. }
  1722. ext4_write_lock_xattr(inode, &no_expand);
  1723. if (ext4_has_inline_data(inode))
  1724. error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
  1725. ext4_write_unlock_xattr(inode, &no_expand);
  1726. ext4_journal_stop(handle);
  1727. out_free:
  1728. brelse(iloc.bh);
  1729. return error;
  1730. }