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 "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. 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 += 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. 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. error = ext4_journal_get_write_access(handle, is.iloc.bh);
  221. if (error)
  222. goto out;
  223. if (len > EXT4_MIN_INLINE_DATA_SIZE) {
  224. value = EXT4_ZERO_XATTR_VALUE;
  225. len -= EXT4_MIN_INLINE_DATA_SIZE;
  226. } else {
  227. value = "";
  228. len = 0;
  229. }
  230. /* Insert the the xttr entry. */
  231. i.value = value;
  232. i.value_len = len;
  233. error = ext4_xattr_ibody_find(inode, &i, &is);
  234. if (error)
  235. goto out;
  236. BUG_ON(!is.s.not_found);
  237. error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
  238. if (error) {
  239. if (error == -ENOSPC)
  240. ext4_clear_inode_state(inode,
  241. EXT4_STATE_MAY_INLINE_DATA);
  242. goto out;
  243. }
  244. memset((void *)ext4_raw_inode(&is.iloc)->i_block,
  245. 0, EXT4_MIN_INLINE_DATA_SIZE);
  246. EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
  247. (void *)ext4_raw_inode(&is.iloc));
  248. EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
  249. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  250. ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
  251. get_bh(is.iloc.bh);
  252. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  253. out:
  254. brelse(is.iloc.bh);
  255. return error;
  256. }
  257. static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
  258. unsigned int len)
  259. {
  260. int error;
  261. void *value = NULL;
  262. struct ext4_xattr_ibody_find is = {
  263. .s = { .not_found = -ENODATA, },
  264. };
  265. struct ext4_xattr_info i = {
  266. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  267. .name = EXT4_XATTR_SYSTEM_DATA,
  268. };
  269. /* If the old space is ok, write the data directly. */
  270. if (len <= EXT4_I(inode)->i_inline_size)
  271. return 0;
  272. error = ext4_get_inode_loc(inode, &is.iloc);
  273. if (error)
  274. return error;
  275. error = ext4_xattr_ibody_find(inode, &i, &is);
  276. if (error)
  277. goto out;
  278. BUG_ON(is.s.not_found);
  279. len -= EXT4_MIN_INLINE_DATA_SIZE;
  280. value = kzalloc(len, GFP_NOFS);
  281. if (!value)
  282. goto out;
  283. error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
  284. value, len);
  285. if (error == -ENODATA)
  286. goto out;
  287. error = ext4_journal_get_write_access(handle, is.iloc.bh);
  288. if (error)
  289. goto out;
  290. /* Update the xttr entry. */
  291. i.value = value;
  292. i.value_len = len;
  293. error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
  294. if (error)
  295. goto out;
  296. EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
  297. (void *)ext4_raw_inode(&is.iloc));
  298. EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
  299. le32_to_cpu(is.s.here->e_value_size);
  300. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  301. get_bh(is.iloc.bh);
  302. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  303. out:
  304. kfree(value);
  305. brelse(is.iloc.bh);
  306. return error;
  307. }
  308. int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
  309. unsigned int len)
  310. {
  311. int ret, size;
  312. struct ext4_inode_info *ei = EXT4_I(inode);
  313. if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA))
  314. return -ENOSPC;
  315. size = ext4_get_max_inline_size(inode);
  316. if (size < len)
  317. return -ENOSPC;
  318. down_write(&EXT4_I(inode)->xattr_sem);
  319. if (ei->i_inline_off)
  320. ret = ext4_update_inline_data(handle, inode, len);
  321. else
  322. ret = ext4_create_inline_data(handle, inode, len);
  323. up_write(&EXT4_I(inode)->xattr_sem);
  324. return ret;
  325. }
  326. static int ext4_destroy_inline_data_nolock(handle_t *handle,
  327. struct inode *inode)
  328. {
  329. struct ext4_inode_info *ei = EXT4_I(inode);
  330. struct ext4_xattr_ibody_find is = {
  331. .s = { .not_found = 0, },
  332. };
  333. struct ext4_xattr_info i = {
  334. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  335. .name = EXT4_XATTR_SYSTEM_DATA,
  336. .value = NULL,
  337. .value_len = 0,
  338. };
  339. int error;
  340. if (!ei->i_inline_off)
  341. return 0;
  342. error = ext4_get_inode_loc(inode, &is.iloc);
  343. if (error)
  344. return error;
  345. error = ext4_xattr_ibody_find(inode, &i, &is);
  346. if (error)
  347. goto out;
  348. error = ext4_journal_get_write_access(handle, is.iloc.bh);
  349. if (error)
  350. goto out;
  351. error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
  352. if (error)
  353. goto out;
  354. memset((void *)ext4_raw_inode(&is.iloc)->i_block,
  355. 0, EXT4_MIN_INLINE_DATA_SIZE);
  356. if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
  357. EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  358. if (S_ISDIR(inode->i_mode) ||
  359. S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) {
  360. ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS);
  361. ext4_ext_tree_init(handle, inode);
  362. }
  363. }
  364. ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA);
  365. get_bh(is.iloc.bh);
  366. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  367. EXT4_I(inode)->i_inline_off = 0;
  368. EXT4_I(inode)->i_inline_size = 0;
  369. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  370. out:
  371. brelse(is.iloc.bh);
  372. if (error == -ENODATA)
  373. error = 0;
  374. return error;
  375. }
  376. static int ext4_read_inline_page(struct inode *inode, struct page *page)
  377. {
  378. void *kaddr;
  379. int ret = 0;
  380. size_t len;
  381. struct ext4_iloc iloc;
  382. BUG_ON(!PageLocked(page));
  383. BUG_ON(!ext4_has_inline_data(inode));
  384. BUG_ON(page->index);
  385. if (!EXT4_I(inode)->i_inline_off) {
  386. ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.",
  387. inode->i_ino);
  388. goto out;
  389. }
  390. ret = ext4_get_inode_loc(inode, &iloc);
  391. if (ret)
  392. goto out;
  393. len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode));
  394. kaddr = kmap_atomic(page);
  395. ret = ext4_read_inline_data(inode, kaddr, len, &iloc);
  396. flush_dcache_page(page);
  397. kunmap_atomic(kaddr);
  398. zero_user_segment(page, len, PAGE_CACHE_SIZE);
  399. SetPageUptodate(page);
  400. brelse(iloc.bh);
  401. out:
  402. return ret;
  403. }
  404. int ext4_readpage_inline(struct inode *inode, struct page *page)
  405. {
  406. int ret = 0;
  407. down_read(&EXT4_I(inode)->xattr_sem);
  408. if (!ext4_has_inline_data(inode)) {
  409. up_read(&EXT4_I(inode)->xattr_sem);
  410. return -EAGAIN;
  411. }
  412. /*
  413. * Current inline data can only exist in the 1st page,
  414. * So for all the other pages, just set them uptodate.
  415. */
  416. if (!page->index)
  417. ret = ext4_read_inline_page(inode, page);
  418. else if (!PageUptodate(page)) {
  419. zero_user_segment(page, 0, PAGE_CACHE_SIZE);
  420. SetPageUptodate(page);
  421. }
  422. up_read(&EXT4_I(inode)->xattr_sem);
  423. unlock_page(page);
  424. return ret >= 0 ? 0 : ret;
  425. }
  426. static int ext4_convert_inline_data_to_extent(struct address_space *mapping,
  427. struct inode *inode,
  428. unsigned flags)
  429. {
  430. int ret, needed_blocks;
  431. handle_t *handle = NULL;
  432. int retries = 0, sem_held = 0;
  433. struct page *page = NULL;
  434. unsigned from, to;
  435. struct ext4_iloc iloc;
  436. if (!ext4_has_inline_data(inode)) {
  437. /*
  438. * clear the flag so that no new write
  439. * will trap here again.
  440. */
  441. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  442. return 0;
  443. }
  444. needed_blocks = ext4_writepage_trans_blocks(inode);
  445. ret = ext4_get_inode_loc(inode, &iloc);
  446. if (ret)
  447. return ret;
  448. retry:
  449. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
  450. if (IS_ERR(handle)) {
  451. ret = PTR_ERR(handle);
  452. handle = NULL;
  453. goto out;
  454. }
  455. /* We cannot recurse into the filesystem as the transaction is already
  456. * started */
  457. flags |= AOP_FLAG_NOFS;
  458. page = grab_cache_page_write_begin(mapping, 0, flags);
  459. if (!page) {
  460. ret = -ENOMEM;
  461. goto out;
  462. }
  463. down_write(&EXT4_I(inode)->xattr_sem);
  464. sem_held = 1;
  465. /* If some one has already done this for us, just exit. */
  466. if (!ext4_has_inline_data(inode)) {
  467. ret = 0;
  468. goto out;
  469. }
  470. from = 0;
  471. to = ext4_get_inline_size(inode);
  472. if (!PageUptodate(page)) {
  473. ret = ext4_read_inline_page(inode, page);
  474. if (ret < 0)
  475. goto out;
  476. }
  477. ret = ext4_destroy_inline_data_nolock(handle, inode);
  478. if (ret)
  479. goto out;
  480. if (ext4_should_dioread_nolock(inode))
  481. ret = __block_write_begin(page, from, to, ext4_get_block_write);
  482. else
  483. ret = __block_write_begin(page, from, to, ext4_get_block);
  484. if (!ret && ext4_should_journal_data(inode)) {
  485. ret = ext4_walk_page_buffers(handle, page_buffers(page),
  486. from, to, NULL,
  487. do_journal_get_write_access);
  488. }
  489. if (ret) {
  490. unlock_page(page);
  491. page_cache_release(page);
  492. ext4_orphan_add(handle, inode);
  493. up_write(&EXT4_I(inode)->xattr_sem);
  494. sem_held = 0;
  495. ext4_journal_stop(handle);
  496. handle = NULL;
  497. ext4_truncate_failed_write(inode);
  498. /*
  499. * If truncate failed early the inode might
  500. * still be on the orphan list; we need to
  501. * make sure the inode is removed from the
  502. * orphan list in that case.
  503. */
  504. if (inode->i_nlink)
  505. ext4_orphan_del(NULL, inode);
  506. }
  507. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  508. goto retry;
  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. ext4_truncate_failed_write(inode);
  679. goto out;
  680. }
  681. SetPageDirty(page);
  682. SetPageUptodate(page);
  683. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  684. *fsdata = (void *)CONVERT_INLINE_DATA;
  685. out:
  686. up_read(&EXT4_I(inode)->xattr_sem);
  687. if (page) {
  688. unlock_page(page);
  689. page_cache_release(page);
  690. }
  691. return ret;
  692. }
  693. /*
  694. * Prepare the write for the inline data.
  695. * If the the data can be written into the inode, we just read
  696. * the page and make it uptodate, and start the journal.
  697. * Otherwise read the page, makes it dirty so that it can be
  698. * handle in writepages(the i_disksize update is left to the
  699. * normal ext4_da_write_end).
  700. */
  701. int ext4_da_write_inline_data_begin(struct address_space *mapping,
  702. struct inode *inode,
  703. loff_t pos, unsigned len,
  704. unsigned flags,
  705. struct page **pagep,
  706. void **fsdata)
  707. {
  708. int ret, inline_size;
  709. handle_t *handle;
  710. struct page *page;
  711. struct ext4_iloc iloc;
  712. ret = ext4_get_inode_loc(inode, &iloc);
  713. if (ret)
  714. return ret;
  715. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  716. if (IS_ERR(handle)) {
  717. ret = PTR_ERR(handle);
  718. handle = NULL;
  719. goto out;
  720. }
  721. inline_size = ext4_get_max_inline_size(inode);
  722. ret = -ENOSPC;
  723. if (inline_size >= pos + len) {
  724. ret = ext4_prepare_inline_data(handle, inode, pos + len);
  725. if (ret && ret != -ENOSPC)
  726. goto out;
  727. }
  728. if (ret == -ENOSPC) {
  729. ret = ext4_da_convert_inline_data_to_extent(mapping,
  730. inode,
  731. flags,
  732. fsdata);
  733. goto out;
  734. }
  735. /*
  736. * We cannot recurse into the filesystem as the transaction
  737. * is already started.
  738. */
  739. flags |= AOP_FLAG_NOFS;
  740. page = grab_cache_page_write_begin(mapping, 0, flags);
  741. if (!page) {
  742. ret = -ENOMEM;
  743. goto out;
  744. }
  745. down_read(&EXT4_I(inode)->xattr_sem);
  746. if (!ext4_has_inline_data(inode)) {
  747. ret = 0;
  748. goto out_release_page;
  749. }
  750. if (!PageUptodate(page)) {
  751. ret = ext4_read_inline_page(inode, page);
  752. if (ret < 0)
  753. goto out_release_page;
  754. }
  755. up_read(&EXT4_I(inode)->xattr_sem);
  756. *pagep = page;
  757. handle = NULL;
  758. brelse(iloc.bh);
  759. return 1;
  760. out_release_page:
  761. up_read(&EXT4_I(inode)->xattr_sem);
  762. unlock_page(page);
  763. page_cache_release(page);
  764. out:
  765. if (handle)
  766. ext4_journal_stop(handle);
  767. brelse(iloc.bh);
  768. return ret;
  769. }
  770. int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
  771. unsigned len, unsigned copied,
  772. struct page *page)
  773. {
  774. int i_size_changed = 0;
  775. copied = ext4_write_inline_data_end(inode, pos, len, copied, page);
  776. /*
  777. * No need to use i_size_read() here, the i_size
  778. * cannot change under us because we hold i_mutex.
  779. *
  780. * But it's important to update i_size while still holding page lock:
  781. * page writeout could otherwise come in and zero beyond i_size.
  782. */
  783. if (pos+copied > inode->i_size) {
  784. i_size_write(inode, pos+copied);
  785. i_size_changed = 1;
  786. }
  787. unlock_page(page);
  788. page_cache_release(page);
  789. /*
  790. * Don't mark the inode dirty under page lock. First, it unnecessarily
  791. * makes the holding time of page lock longer. Second, it forces lock
  792. * ordering of page lock and transaction start for journaling
  793. * filesystems.
  794. */
  795. if (i_size_changed)
  796. mark_inode_dirty(inode);
  797. return copied;
  798. }
  799. #ifdef INLINE_DIR_DEBUG
  800. void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
  801. void *inline_start, int inline_size)
  802. {
  803. int offset;
  804. unsigned short de_len;
  805. struct ext4_dir_entry_2 *de = inline_start;
  806. void *dlimit = inline_start + inline_size;
  807. trace_printk("inode %lu\n", dir->i_ino);
  808. offset = 0;
  809. while ((void *)de < dlimit) {
  810. de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
  811. trace_printk("de: off %u rlen %u name %*.s nlen %u ino %u\n",
  812. offset, de_len, de->name_len, de->name,
  813. de->name_len, le32_to_cpu(de->inode));
  814. if (ext4_check_dir_entry(dir, NULL, de, bh,
  815. inline_start, inline_size, offset))
  816. BUG();
  817. offset += de_len;
  818. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  819. }
  820. }
  821. #else
  822. #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
  823. #endif
  824. /*
  825. * Add a new entry into a inline dir.
  826. * It will return -ENOSPC if no space is available, and -EIO
  827. * and -EEXIST if directory entry already exists.
  828. */
  829. static int ext4_add_dirent_to_inline(handle_t *handle,
  830. struct dentry *dentry,
  831. struct inode *inode,
  832. struct ext4_iloc *iloc,
  833. void *inline_start, int inline_size)
  834. {
  835. struct inode *dir = dentry->d_parent->d_inode;
  836. const char *name = dentry->d_name.name;
  837. int namelen = dentry->d_name.len;
  838. unsigned short reclen;
  839. int err;
  840. struct ext4_dir_entry_2 *de;
  841. reclen = EXT4_DIR_REC_LEN(namelen);
  842. err = ext4_find_dest_de(dir, inode, iloc->bh,
  843. inline_start, inline_size,
  844. name, namelen, &de);
  845. if (err)
  846. return err;
  847. err = ext4_journal_get_write_access(handle, iloc->bh);
  848. if (err)
  849. return err;
  850. ext4_insert_dentry(inode, de, inline_size, name, namelen);
  851. ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
  852. /*
  853. * XXX shouldn't update any times until successful
  854. * completion of syscall, but too many callers depend
  855. * on this.
  856. *
  857. * XXX similarly, too many callers depend on
  858. * ext4_new_inode() setting the times, but error
  859. * recovery deletes the inode, so the worst that can
  860. * happen is that the times are slightly out of date
  861. * and/or different from the directory change time.
  862. */
  863. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  864. ext4_update_dx_flag(dir);
  865. dir->i_version++;
  866. ext4_mark_inode_dirty(handle, dir);
  867. return 1;
  868. }
  869. static void *ext4_get_inline_xattr_pos(struct inode *inode,
  870. struct ext4_iloc *iloc)
  871. {
  872. struct ext4_xattr_entry *entry;
  873. struct ext4_xattr_ibody_header *header;
  874. BUG_ON(!EXT4_I(inode)->i_inline_off);
  875. header = IHDR(inode, ext4_raw_inode(iloc));
  876. entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
  877. EXT4_I(inode)->i_inline_off);
  878. return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
  879. }
  880. /* Set the final de to cover the whole block. */
  881. static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
  882. {
  883. struct ext4_dir_entry_2 *de, *prev_de;
  884. void *limit;
  885. int de_len;
  886. de = (struct ext4_dir_entry_2 *)de_buf;
  887. if (old_size) {
  888. limit = de_buf + old_size;
  889. do {
  890. prev_de = de;
  891. de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
  892. de_buf += de_len;
  893. de = (struct ext4_dir_entry_2 *)de_buf;
  894. } while (de_buf < limit);
  895. prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
  896. old_size, new_size);
  897. } else {
  898. /* this is just created, so create an empty entry. */
  899. de->inode = 0;
  900. de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
  901. }
  902. }
  903. static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
  904. struct ext4_iloc *iloc)
  905. {
  906. int ret;
  907. int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
  908. int new_size = get_max_inline_xattr_value_size(dir, iloc);
  909. if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
  910. return -ENOSPC;
  911. ret = ext4_update_inline_data(handle, dir,
  912. new_size + EXT4_MIN_INLINE_DATA_SIZE);
  913. if (ret)
  914. return ret;
  915. ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
  916. EXT4_I(dir)->i_inline_size -
  917. EXT4_MIN_INLINE_DATA_SIZE);
  918. dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
  919. return 0;
  920. }
  921. static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
  922. struct ext4_iloc *iloc,
  923. void *buf, int inline_size)
  924. {
  925. ext4_create_inline_data(handle, inode, inline_size);
  926. ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
  927. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  928. }
  929. static int ext4_finish_convert_inline_dir(handle_t *handle,
  930. struct inode *inode,
  931. struct buffer_head *dir_block,
  932. void *buf,
  933. int inline_size)
  934. {
  935. int err, csum_size = 0, header_size = 0;
  936. struct ext4_dir_entry_2 *de;
  937. struct ext4_dir_entry_tail *t;
  938. void *target = dir_block->b_data;
  939. /*
  940. * First create "." and ".." and then copy the dir information
  941. * back to the block.
  942. */
  943. de = (struct ext4_dir_entry_2 *)target;
  944. de = ext4_init_dot_dotdot(inode, de,
  945. inode->i_sb->s_blocksize, csum_size,
  946. le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
  947. header_size = (void *)de - target;
  948. memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
  949. inline_size - EXT4_INLINE_DOTDOT_SIZE);
  950. if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  951. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  952. csum_size = sizeof(struct ext4_dir_entry_tail);
  953. inode->i_size = inode->i_sb->s_blocksize;
  954. i_size_write(inode, inode->i_sb->s_blocksize);
  955. EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  956. ext4_update_final_de(dir_block->b_data,
  957. inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
  958. inode->i_sb->s_blocksize - csum_size);
  959. if (csum_size) {
  960. t = EXT4_DIRENT_TAIL(dir_block->b_data,
  961. inode->i_sb->s_blocksize);
  962. initialize_dirent_tail(t, inode->i_sb->s_blocksize);
  963. }
  964. set_buffer_uptodate(dir_block);
  965. err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
  966. if (err)
  967. goto out;
  968. set_buffer_verified(dir_block);
  969. out:
  970. return err;
  971. }
  972. static int ext4_convert_inline_data_nolock(handle_t *handle,
  973. struct inode *inode,
  974. struct ext4_iloc *iloc)
  975. {
  976. int error;
  977. void *buf = NULL;
  978. struct buffer_head *data_bh = NULL;
  979. struct ext4_map_blocks map;
  980. int inline_size;
  981. inline_size = ext4_get_inline_size(inode);
  982. buf = kmalloc(inline_size, GFP_NOFS);
  983. if (!buf) {
  984. error = -ENOMEM;
  985. goto out;
  986. }
  987. error = ext4_read_inline_data(inode, buf, inline_size, iloc);
  988. if (error < 0)
  989. goto out;
  990. error = ext4_destroy_inline_data_nolock(handle, inode);
  991. if (error)
  992. goto out;
  993. map.m_lblk = 0;
  994. map.m_len = 1;
  995. map.m_flags = 0;
  996. error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
  997. if (error < 0)
  998. goto out_restore;
  999. if (!(map.m_flags & EXT4_MAP_MAPPED)) {
  1000. error = -EIO;
  1001. goto out_restore;
  1002. }
  1003. data_bh = sb_getblk(inode->i_sb, map.m_pblk);
  1004. if (!data_bh) {
  1005. error = -ENOMEM;
  1006. goto out_restore;
  1007. }
  1008. lock_buffer(data_bh);
  1009. error = ext4_journal_get_create_access(handle, data_bh);
  1010. if (error) {
  1011. unlock_buffer(data_bh);
  1012. error = -EIO;
  1013. goto out_restore;
  1014. }
  1015. memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
  1016. if (!S_ISDIR(inode->i_mode)) {
  1017. memcpy(data_bh->b_data, buf, inline_size);
  1018. set_buffer_uptodate(data_bh);
  1019. error = ext4_handle_dirty_metadata(handle,
  1020. inode, data_bh);
  1021. } else {
  1022. error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
  1023. buf, inline_size);
  1024. }
  1025. unlock_buffer(data_bh);
  1026. out_restore:
  1027. if (error)
  1028. ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
  1029. out:
  1030. brelse(data_bh);
  1031. kfree(buf);
  1032. return error;
  1033. }
  1034. /*
  1035. * Try to add the new entry to the inline data.
  1036. * If succeeds, return 0. If not, extended the inline dir and copied data to
  1037. * the new created block.
  1038. */
  1039. int ext4_try_add_inline_entry(handle_t *handle, struct dentry *dentry,
  1040. struct inode *inode)
  1041. {
  1042. int ret, inline_size;
  1043. void *inline_start;
  1044. struct ext4_iloc iloc;
  1045. struct inode *dir = dentry->d_parent->d_inode;
  1046. ret = ext4_get_inode_loc(dir, &iloc);
  1047. if (ret)
  1048. return ret;
  1049. down_write(&EXT4_I(dir)->xattr_sem);
  1050. if (!ext4_has_inline_data(dir))
  1051. goto out;
  1052. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1053. EXT4_INLINE_DOTDOT_SIZE;
  1054. inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
  1055. ret = ext4_add_dirent_to_inline(handle, dentry, inode, &iloc,
  1056. inline_start, inline_size);
  1057. if (ret != -ENOSPC)
  1058. goto out;
  1059. /* check whether it can be inserted to inline xattr space. */
  1060. inline_size = EXT4_I(dir)->i_inline_size -
  1061. EXT4_MIN_INLINE_DATA_SIZE;
  1062. if (!inline_size) {
  1063. /* Try to use the xattr space.*/
  1064. ret = ext4_update_inline_dir(handle, dir, &iloc);
  1065. if (ret && ret != -ENOSPC)
  1066. goto out;
  1067. inline_size = EXT4_I(dir)->i_inline_size -
  1068. EXT4_MIN_INLINE_DATA_SIZE;
  1069. }
  1070. if (inline_size) {
  1071. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1072. ret = ext4_add_dirent_to_inline(handle, dentry, inode, &iloc,
  1073. inline_start, inline_size);
  1074. if (ret != -ENOSPC)
  1075. goto out;
  1076. }
  1077. /*
  1078. * The inline space is filled up, so create a new block for it.
  1079. * As the extent tree will be created, we have to save the inline
  1080. * dir first.
  1081. */
  1082. ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
  1083. out:
  1084. ext4_mark_inode_dirty(handle, dir);
  1085. up_write(&EXT4_I(dir)->xattr_sem);
  1086. brelse(iloc.bh);
  1087. return ret;
  1088. }
  1089. /*
  1090. * This function fills a red-black tree with information from an
  1091. * inlined dir. It returns the number directory entries loaded
  1092. * into the tree. If there is an error it is returned in err.
  1093. */
  1094. int htree_inlinedir_to_tree(struct file *dir_file,
  1095. struct inode *dir, ext4_lblk_t block,
  1096. struct dx_hash_info *hinfo,
  1097. __u32 start_hash, __u32 start_minor_hash,
  1098. int *has_inline_data)
  1099. {
  1100. int err = 0, count = 0;
  1101. unsigned int parent_ino;
  1102. int pos;
  1103. struct ext4_dir_entry_2 *de;
  1104. struct inode *inode = file_inode(dir_file);
  1105. int ret, inline_size = 0;
  1106. struct ext4_iloc iloc;
  1107. void *dir_buf = NULL;
  1108. struct ext4_dir_entry_2 fake;
  1109. ret = ext4_get_inode_loc(inode, &iloc);
  1110. if (ret)
  1111. return ret;
  1112. down_read(&EXT4_I(inode)->xattr_sem);
  1113. if (!ext4_has_inline_data(inode)) {
  1114. up_read(&EXT4_I(inode)->xattr_sem);
  1115. *has_inline_data = 0;
  1116. goto out;
  1117. }
  1118. inline_size = ext4_get_inline_size(inode);
  1119. dir_buf = kmalloc(inline_size, GFP_NOFS);
  1120. if (!dir_buf) {
  1121. ret = -ENOMEM;
  1122. up_read(&EXT4_I(inode)->xattr_sem);
  1123. goto out;
  1124. }
  1125. ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
  1126. up_read(&EXT4_I(inode)->xattr_sem);
  1127. if (ret < 0)
  1128. goto out;
  1129. pos = 0;
  1130. parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
  1131. while (pos < inline_size) {
  1132. /*
  1133. * As inlined dir doesn't store any information about '.' and
  1134. * only the inode number of '..' is stored, we have to handle
  1135. * them differently.
  1136. */
  1137. if (pos == 0) {
  1138. fake.inode = cpu_to_le32(inode->i_ino);
  1139. fake.name_len = 1;
  1140. strcpy(fake.name, ".");
  1141. fake.rec_len = ext4_rec_len_to_disk(
  1142. EXT4_DIR_REC_LEN(fake.name_len),
  1143. inline_size);
  1144. ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
  1145. de = &fake;
  1146. pos = EXT4_INLINE_DOTDOT_OFFSET;
  1147. } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) {
  1148. fake.inode = cpu_to_le32(parent_ino);
  1149. fake.name_len = 2;
  1150. strcpy(fake.name, "..");
  1151. fake.rec_len = ext4_rec_len_to_disk(
  1152. EXT4_DIR_REC_LEN(fake.name_len),
  1153. inline_size);
  1154. ext4_set_de_type(inode->i_sb, &fake, S_IFDIR);
  1155. de = &fake;
  1156. pos = EXT4_INLINE_DOTDOT_SIZE;
  1157. } else {
  1158. de = (struct ext4_dir_entry_2 *)(dir_buf + pos);
  1159. pos += ext4_rec_len_from_disk(de->rec_len, inline_size);
  1160. if (ext4_check_dir_entry(inode, dir_file, de,
  1161. iloc.bh, dir_buf,
  1162. inline_size, pos)) {
  1163. ret = count;
  1164. goto out;
  1165. }
  1166. }
  1167. ext4fs_dirhash(de->name, de->name_len, hinfo);
  1168. if ((hinfo->hash < start_hash) ||
  1169. ((hinfo->hash == start_hash) &&
  1170. (hinfo->minor_hash < start_minor_hash)))
  1171. continue;
  1172. if (de->inode == 0)
  1173. continue;
  1174. err = ext4_htree_store_dirent(dir_file,
  1175. hinfo->hash, hinfo->minor_hash, de);
  1176. if (err) {
  1177. count = err;
  1178. goto out;
  1179. }
  1180. count++;
  1181. }
  1182. ret = count;
  1183. out:
  1184. kfree(dir_buf);
  1185. brelse(iloc.bh);
  1186. return ret;
  1187. }
  1188. /*
  1189. * So this function is called when the volume is mkfsed with
  1190. * dir_index disabled. In order to keep f_pos persistent
  1191. * after we convert from an inlined dir to a blocked based,
  1192. * we just pretend that we are a normal dir and return the
  1193. * offset as if '.' and '..' really take place.
  1194. *
  1195. */
  1196. int ext4_read_inline_dir(struct file *filp,
  1197. void *dirent, filldir_t filldir,
  1198. int *has_inline_data)
  1199. {
  1200. int error = 0;
  1201. unsigned int offset, parent_ino;
  1202. int i, stored;
  1203. struct ext4_dir_entry_2 *de;
  1204. struct super_block *sb;
  1205. struct inode *inode = file_inode(filp);
  1206. int ret, inline_size = 0;
  1207. struct ext4_iloc iloc;
  1208. void *dir_buf = NULL;
  1209. int dotdot_offset, dotdot_size, extra_offset, extra_size;
  1210. ret = ext4_get_inode_loc(inode, &iloc);
  1211. if (ret)
  1212. return ret;
  1213. down_read(&EXT4_I(inode)->xattr_sem);
  1214. if (!ext4_has_inline_data(inode)) {
  1215. up_read(&EXT4_I(inode)->xattr_sem);
  1216. *has_inline_data = 0;
  1217. goto out;
  1218. }
  1219. inline_size = ext4_get_inline_size(inode);
  1220. dir_buf = kmalloc(inline_size, GFP_NOFS);
  1221. if (!dir_buf) {
  1222. ret = -ENOMEM;
  1223. up_read(&EXT4_I(inode)->xattr_sem);
  1224. goto out;
  1225. }
  1226. ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
  1227. up_read(&EXT4_I(inode)->xattr_sem);
  1228. if (ret < 0)
  1229. goto out;
  1230. sb = inode->i_sb;
  1231. stored = 0;
  1232. parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
  1233. offset = filp->f_pos;
  1234. /*
  1235. * dotdot_offset and dotdot_size is the real offset and
  1236. * size for ".." and "." if the dir is block based while
  1237. * the real size for them are only EXT4_INLINE_DOTDOT_SIZE.
  1238. * So we will use extra_offset and extra_size to indicate them
  1239. * during the inline dir iteration.
  1240. */
  1241. dotdot_offset = EXT4_DIR_REC_LEN(1);
  1242. dotdot_size = dotdot_offset + EXT4_DIR_REC_LEN(2);
  1243. extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE;
  1244. extra_size = extra_offset + inline_size;
  1245. while (!error && !stored && filp->f_pos < extra_size) {
  1246. revalidate:
  1247. /*
  1248. * If the version has changed since the last call to
  1249. * readdir(2), then we might be pointing to an invalid
  1250. * dirent right now. Scan from the start of the inline
  1251. * dir to make sure.
  1252. */
  1253. if (filp->f_version != inode->i_version) {
  1254. for (i = 0; i < extra_size && i < offset;) {
  1255. /*
  1256. * "." is with offset 0 and
  1257. * ".." is dotdot_offset.
  1258. */
  1259. if (!i) {
  1260. i = dotdot_offset;
  1261. continue;
  1262. } else if (i == dotdot_offset) {
  1263. i = dotdot_size;
  1264. continue;
  1265. }
  1266. /* for other entry, the real offset in
  1267. * the buf has to be tuned accordingly.
  1268. */
  1269. de = (struct ext4_dir_entry_2 *)
  1270. (dir_buf + i - extra_offset);
  1271. /* It's too expensive to do a full
  1272. * dirent test each time round this
  1273. * loop, but we do have to test at
  1274. * least that it is non-zero. A
  1275. * failure will be detected in the
  1276. * dirent test below. */
  1277. if (ext4_rec_len_from_disk(de->rec_len,
  1278. extra_size) < EXT4_DIR_REC_LEN(1))
  1279. break;
  1280. i += ext4_rec_len_from_disk(de->rec_len,
  1281. extra_size);
  1282. }
  1283. offset = i;
  1284. filp->f_pos = offset;
  1285. filp->f_version = inode->i_version;
  1286. }
  1287. while (!error && filp->f_pos < extra_size) {
  1288. if (filp->f_pos == 0) {
  1289. error = filldir(dirent, ".", 1, 0, inode->i_ino,
  1290. DT_DIR);
  1291. if (error)
  1292. break;
  1293. stored++;
  1294. filp->f_pos = dotdot_offset;
  1295. continue;
  1296. }
  1297. if (filp->f_pos == dotdot_offset) {
  1298. error = filldir(dirent, "..", 2,
  1299. dotdot_offset,
  1300. parent_ino, DT_DIR);
  1301. if (error)
  1302. break;
  1303. stored++;
  1304. filp->f_pos = dotdot_size;
  1305. continue;
  1306. }
  1307. de = (struct ext4_dir_entry_2 *)
  1308. (dir_buf + filp->f_pos - extra_offset);
  1309. if (ext4_check_dir_entry(inode, filp, de,
  1310. iloc.bh, dir_buf,
  1311. extra_size, filp->f_pos)) {
  1312. ret = stored;
  1313. goto out;
  1314. }
  1315. if (le32_to_cpu(de->inode)) {
  1316. /* We might block in the next section
  1317. * if the data destination is
  1318. * currently swapped out. So, use a
  1319. * version stamp to detect whether or
  1320. * not the directory has been modified
  1321. * during the copy operation.
  1322. */
  1323. u64 version = filp->f_version;
  1324. error = filldir(dirent, de->name,
  1325. de->name_len,
  1326. filp->f_pos,
  1327. le32_to_cpu(de->inode),
  1328. get_dtype(sb, de->file_type));
  1329. if (error)
  1330. break;
  1331. if (version != filp->f_version)
  1332. goto revalidate;
  1333. stored++;
  1334. }
  1335. filp->f_pos += ext4_rec_len_from_disk(de->rec_len,
  1336. extra_size);
  1337. }
  1338. }
  1339. out:
  1340. kfree(dir_buf);
  1341. brelse(iloc.bh);
  1342. return ret;
  1343. }
  1344. struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
  1345. struct ext4_dir_entry_2 **parent_de,
  1346. int *retval)
  1347. {
  1348. struct ext4_iloc iloc;
  1349. *retval = ext4_get_inode_loc(inode, &iloc);
  1350. if (*retval)
  1351. return NULL;
  1352. *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1353. return iloc.bh;
  1354. }
  1355. /*
  1356. * Try to create the inline data for the new dir.
  1357. * If it succeeds, return 0, otherwise return the error.
  1358. * In case of ENOSPC, the caller should create the normal disk layout dir.
  1359. */
  1360. int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
  1361. struct inode *inode)
  1362. {
  1363. int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
  1364. struct ext4_iloc iloc;
  1365. struct ext4_dir_entry_2 *de;
  1366. ret = ext4_get_inode_loc(inode, &iloc);
  1367. if (ret)
  1368. return ret;
  1369. ret = ext4_prepare_inline_data(handle, inode, inline_size);
  1370. if (ret)
  1371. goto out;
  1372. /*
  1373. * For inline dir, we only save the inode information for the ".."
  1374. * and create a fake dentry to cover the left space.
  1375. */
  1376. de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1377. de->inode = cpu_to_le32(parent->i_ino);
  1378. de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
  1379. de->inode = 0;
  1380. de->rec_len = ext4_rec_len_to_disk(
  1381. inline_size - EXT4_INLINE_DOTDOT_SIZE,
  1382. inline_size);
  1383. set_nlink(inode, 2);
  1384. inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
  1385. out:
  1386. brelse(iloc.bh);
  1387. return ret;
  1388. }
  1389. struct buffer_head *ext4_find_inline_entry(struct inode *dir,
  1390. const struct qstr *d_name,
  1391. struct ext4_dir_entry_2 **res_dir,
  1392. int *has_inline_data)
  1393. {
  1394. int ret;
  1395. struct ext4_iloc iloc;
  1396. void *inline_start;
  1397. int inline_size;
  1398. if (ext4_get_inode_loc(dir, &iloc))
  1399. return NULL;
  1400. down_read(&EXT4_I(dir)->xattr_sem);
  1401. if (!ext4_has_inline_data(dir)) {
  1402. *has_inline_data = 0;
  1403. goto out;
  1404. }
  1405. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1406. EXT4_INLINE_DOTDOT_SIZE;
  1407. inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
  1408. ret = search_dir(iloc.bh, inline_start, inline_size,
  1409. dir, d_name, 0, res_dir);
  1410. if (ret == 1)
  1411. goto out_find;
  1412. if (ret < 0)
  1413. goto out;
  1414. if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
  1415. goto out;
  1416. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1417. inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
  1418. ret = search_dir(iloc.bh, inline_start, inline_size,
  1419. dir, d_name, 0, res_dir);
  1420. if (ret == 1)
  1421. goto out_find;
  1422. out:
  1423. brelse(iloc.bh);
  1424. iloc.bh = NULL;
  1425. out_find:
  1426. up_read(&EXT4_I(dir)->xattr_sem);
  1427. return iloc.bh;
  1428. }
  1429. int ext4_delete_inline_entry(handle_t *handle,
  1430. struct inode *dir,
  1431. struct ext4_dir_entry_2 *de_del,
  1432. struct buffer_head *bh,
  1433. int *has_inline_data)
  1434. {
  1435. int err, inline_size;
  1436. struct ext4_iloc iloc;
  1437. void *inline_start;
  1438. err = ext4_get_inode_loc(dir, &iloc);
  1439. if (err)
  1440. return err;
  1441. down_write(&EXT4_I(dir)->xattr_sem);
  1442. if (!ext4_has_inline_data(dir)) {
  1443. *has_inline_data = 0;
  1444. goto out;
  1445. }
  1446. if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
  1447. EXT4_MIN_INLINE_DATA_SIZE) {
  1448. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1449. EXT4_INLINE_DOTDOT_SIZE;
  1450. inline_size = EXT4_MIN_INLINE_DATA_SIZE -
  1451. EXT4_INLINE_DOTDOT_SIZE;
  1452. } else {
  1453. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1454. inline_size = ext4_get_inline_size(dir) -
  1455. EXT4_MIN_INLINE_DATA_SIZE;
  1456. }
  1457. err = ext4_journal_get_write_access(handle, bh);
  1458. if (err)
  1459. goto out;
  1460. err = ext4_generic_delete_entry(handle, dir, de_del, bh,
  1461. inline_start, inline_size, 0);
  1462. if (err)
  1463. goto out;
  1464. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1465. err = ext4_mark_inode_dirty(handle, dir);
  1466. if (unlikely(err))
  1467. goto out;
  1468. ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
  1469. out:
  1470. up_write(&EXT4_I(dir)->xattr_sem);
  1471. brelse(iloc.bh);
  1472. if (err != -ENOENT)
  1473. ext4_std_error(dir->i_sb, err);
  1474. return err;
  1475. }
  1476. /*
  1477. * Get the inline dentry at offset.
  1478. */
  1479. static inline struct ext4_dir_entry_2 *
  1480. ext4_get_inline_entry(struct inode *inode,
  1481. struct ext4_iloc *iloc,
  1482. unsigned int offset,
  1483. void **inline_start,
  1484. int *inline_size)
  1485. {
  1486. void *inline_pos;
  1487. BUG_ON(offset > ext4_get_inline_size(inode));
  1488. if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
  1489. inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
  1490. *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
  1491. } else {
  1492. inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
  1493. offset -= EXT4_MIN_INLINE_DATA_SIZE;
  1494. *inline_size = ext4_get_inline_size(inode) -
  1495. EXT4_MIN_INLINE_DATA_SIZE;
  1496. }
  1497. if (inline_start)
  1498. *inline_start = inline_pos;
  1499. return (struct ext4_dir_entry_2 *)(inline_pos + offset);
  1500. }
  1501. int empty_inline_dir(struct inode *dir, int *has_inline_data)
  1502. {
  1503. int err, inline_size;
  1504. struct ext4_iloc iloc;
  1505. void *inline_pos;
  1506. unsigned int offset;
  1507. struct ext4_dir_entry_2 *de;
  1508. int ret = 1;
  1509. err = ext4_get_inode_loc(dir, &iloc);
  1510. if (err) {
  1511. EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
  1512. err, dir->i_ino);
  1513. return 1;
  1514. }
  1515. down_read(&EXT4_I(dir)->xattr_sem);
  1516. if (!ext4_has_inline_data(dir)) {
  1517. *has_inline_data = 0;
  1518. goto out;
  1519. }
  1520. de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1521. if (!le32_to_cpu(de->inode)) {
  1522. ext4_warning(dir->i_sb,
  1523. "bad inline directory (dir #%lu) - no `..'",
  1524. dir->i_ino);
  1525. ret = 1;
  1526. goto out;
  1527. }
  1528. offset = EXT4_INLINE_DOTDOT_SIZE;
  1529. while (offset < dir->i_size) {
  1530. de = ext4_get_inline_entry(dir, &iloc, offset,
  1531. &inline_pos, &inline_size);
  1532. if (ext4_check_dir_entry(dir, NULL, de,
  1533. iloc.bh, inline_pos,
  1534. inline_size, offset)) {
  1535. ext4_warning(dir->i_sb,
  1536. "bad inline directory (dir #%lu) - "
  1537. "inode %u, rec_len %u, name_len %d"
  1538. "inline size %d\n",
  1539. dir->i_ino, le32_to_cpu(de->inode),
  1540. le16_to_cpu(de->rec_len), de->name_len,
  1541. inline_size);
  1542. ret = 1;
  1543. goto out;
  1544. }
  1545. if (le32_to_cpu(de->inode)) {
  1546. ret = 0;
  1547. goto out;
  1548. }
  1549. offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
  1550. }
  1551. out:
  1552. up_read(&EXT4_I(dir)->xattr_sem);
  1553. brelse(iloc.bh);
  1554. return ret;
  1555. }
  1556. int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
  1557. {
  1558. int ret;
  1559. down_write(&EXT4_I(inode)->xattr_sem);
  1560. ret = ext4_destroy_inline_data_nolock(handle, inode);
  1561. up_write(&EXT4_I(inode)->xattr_sem);
  1562. return ret;
  1563. }
  1564. int ext4_inline_data_fiemap(struct inode *inode,
  1565. struct fiemap_extent_info *fieinfo,
  1566. int *has_inline)
  1567. {
  1568. __u64 physical = 0;
  1569. __u64 length;
  1570. __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_LAST;
  1571. int error = 0;
  1572. struct ext4_iloc iloc;
  1573. down_read(&EXT4_I(inode)->xattr_sem);
  1574. if (!ext4_has_inline_data(inode)) {
  1575. *has_inline = 0;
  1576. goto out;
  1577. }
  1578. error = ext4_get_inode_loc(inode, &iloc);
  1579. if (error)
  1580. goto out;
  1581. physical = iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
  1582. physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
  1583. physical += offsetof(struct ext4_inode, i_block);
  1584. length = i_size_read(inode);
  1585. if (physical)
  1586. error = fiemap_fill_next_extent(fieinfo, 0, physical,
  1587. length, flags);
  1588. brelse(iloc.bh);
  1589. out:
  1590. up_read(&EXT4_I(inode)->xattr_sem);
  1591. return (error < 0 ? error : 0);
  1592. }
  1593. /*
  1594. * Called during xattr set, and if we can sparse space 'needed',
  1595. * just create the extent tree evict the data to the outer block.
  1596. *
  1597. * We use jbd2 instead of page cache to move data to the 1st block
  1598. * so that the whole transaction can be committed as a whole and
  1599. * the data isn't lost because of the delayed page cache write.
  1600. */
  1601. int ext4_try_to_evict_inline_data(handle_t *handle,
  1602. struct inode *inode,
  1603. int needed)
  1604. {
  1605. int error;
  1606. struct ext4_xattr_entry *entry;
  1607. struct ext4_xattr_ibody_header *header;
  1608. struct ext4_inode *raw_inode;
  1609. struct ext4_iloc iloc;
  1610. error = ext4_get_inode_loc(inode, &iloc);
  1611. if (error)
  1612. return error;
  1613. raw_inode = ext4_raw_inode(&iloc);
  1614. header = IHDR(inode, raw_inode);
  1615. entry = (struct ext4_xattr_entry *)((void *)raw_inode +
  1616. EXT4_I(inode)->i_inline_off);
  1617. if (EXT4_XATTR_LEN(entry->e_name_len) +
  1618. EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)) < needed) {
  1619. error = -ENOSPC;
  1620. goto out;
  1621. }
  1622. error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
  1623. out:
  1624. brelse(iloc.bh);
  1625. return error;
  1626. }
  1627. void ext4_inline_data_truncate(struct inode *inode, int *has_inline)
  1628. {
  1629. handle_t *handle;
  1630. int inline_size, value_len, needed_blocks;
  1631. size_t i_size;
  1632. void *value = NULL;
  1633. struct ext4_xattr_ibody_find is = {
  1634. .s = { .not_found = -ENODATA, },
  1635. };
  1636. struct ext4_xattr_info i = {
  1637. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  1638. .name = EXT4_XATTR_SYSTEM_DATA,
  1639. };
  1640. needed_blocks = ext4_writepage_trans_blocks(inode);
  1641. handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
  1642. if (IS_ERR(handle))
  1643. return;
  1644. down_write(&EXT4_I(inode)->xattr_sem);
  1645. if (!ext4_has_inline_data(inode)) {
  1646. *has_inline = 0;
  1647. ext4_journal_stop(handle);
  1648. return;
  1649. }
  1650. if (ext4_orphan_add(handle, inode))
  1651. goto out;
  1652. if (ext4_get_inode_loc(inode, &is.iloc))
  1653. goto out;
  1654. down_write(&EXT4_I(inode)->i_data_sem);
  1655. i_size = inode->i_size;
  1656. inline_size = ext4_get_inline_size(inode);
  1657. EXT4_I(inode)->i_disksize = i_size;
  1658. if (i_size < inline_size) {
  1659. /* Clear the content in the xattr space. */
  1660. if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
  1661. if (ext4_xattr_ibody_find(inode, &i, &is))
  1662. goto out_error;
  1663. BUG_ON(is.s.not_found);
  1664. value_len = le32_to_cpu(is.s.here->e_value_size);
  1665. value = kmalloc(value_len, GFP_NOFS);
  1666. if (!value)
  1667. goto out_error;
  1668. if (ext4_xattr_ibody_get(inode, i.name_index, i.name,
  1669. value, value_len))
  1670. goto out_error;
  1671. i.value = value;
  1672. i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
  1673. i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
  1674. if (ext4_xattr_ibody_inline_set(handle, inode, &i, &is))
  1675. goto out_error;
  1676. }
  1677. /* Clear the content within i_blocks. */
  1678. if (i_size < EXT4_MIN_INLINE_DATA_SIZE)
  1679. memset(ext4_raw_inode(&is.iloc)->i_block + i_size, 0,
  1680. EXT4_MIN_INLINE_DATA_SIZE - i_size);
  1681. EXT4_I(inode)->i_inline_size = i_size <
  1682. EXT4_MIN_INLINE_DATA_SIZE ?
  1683. EXT4_MIN_INLINE_DATA_SIZE : i_size;
  1684. }
  1685. out_error:
  1686. up_write(&EXT4_I(inode)->i_data_sem);
  1687. out:
  1688. brelse(is.iloc.bh);
  1689. up_write(&EXT4_I(inode)->xattr_sem);
  1690. kfree(value);
  1691. if (inode->i_nlink)
  1692. ext4_orphan_del(handle, inode);
  1693. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  1694. ext4_mark_inode_dirty(handle, inode);
  1695. if (IS_SYNC(inode))
  1696. ext4_handle_sync(handle);
  1697. ext4_journal_stop(handle);
  1698. return;
  1699. }
  1700. int ext4_convert_inline_data(struct inode *inode)
  1701. {
  1702. int error, needed_blocks;
  1703. handle_t *handle;
  1704. struct ext4_iloc iloc;
  1705. if (!ext4_has_inline_data(inode)) {
  1706. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  1707. return 0;
  1708. }
  1709. needed_blocks = ext4_writepage_trans_blocks(inode);
  1710. iloc.bh = NULL;
  1711. error = ext4_get_inode_loc(inode, &iloc);
  1712. if (error)
  1713. return error;
  1714. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
  1715. if (IS_ERR(handle)) {
  1716. error = PTR_ERR(handle);
  1717. goto out_free;
  1718. }
  1719. down_write(&EXT4_I(inode)->xattr_sem);
  1720. if (!ext4_has_inline_data(inode)) {
  1721. up_write(&EXT4_I(inode)->xattr_sem);
  1722. goto out;
  1723. }
  1724. error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
  1725. up_write(&EXT4_I(inode)->xattr_sem);
  1726. out:
  1727. ext4_journal_stop(handle);
  1728. out_free:
  1729. brelse(iloc.bh);
  1730. return error;
  1731. }