inline.c 48 KB

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