inline.c 49 KB

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