inline.c 51 KB

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