inline.c 49 KB

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