inline.c 50 KB

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