inline.c 49 KB

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