inline.c 49 KB

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