inline.c 49 KB

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