inline.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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_SIZE 4
  22. int ext4_get_inline_size(struct inode *inode)
  23. {
  24. if (EXT4_I(inode)->i_inline_off)
  25. return EXT4_I(inode)->i_inline_size;
  26. return 0;
  27. }
  28. static int get_max_inline_xattr_value_size(struct inode *inode,
  29. struct ext4_iloc *iloc)
  30. {
  31. struct ext4_xattr_ibody_header *header;
  32. struct ext4_xattr_entry *entry;
  33. struct ext4_inode *raw_inode;
  34. int free, min_offs;
  35. min_offs = EXT4_SB(inode->i_sb)->s_inode_size -
  36. EXT4_GOOD_OLD_INODE_SIZE -
  37. EXT4_I(inode)->i_extra_isize -
  38. sizeof(struct ext4_xattr_ibody_header);
  39. /*
  40. * We need to subtract another sizeof(__u32) since an in-inode xattr
  41. * needs an empty 4 bytes to indicate the gap between the xattr entry
  42. * and the name/value pair.
  43. */
  44. if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
  45. return EXT4_XATTR_SIZE(min_offs -
  46. EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) -
  47. EXT4_XATTR_ROUND - sizeof(__u32));
  48. raw_inode = ext4_raw_inode(iloc);
  49. header = IHDR(inode, raw_inode);
  50. entry = IFIRST(header);
  51. /* Compute min_offs. */
  52. for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
  53. if (!entry->e_value_block && entry->e_value_size) {
  54. size_t offs = le16_to_cpu(entry->e_value_offs);
  55. if (offs < min_offs)
  56. min_offs = offs;
  57. }
  58. }
  59. free = min_offs -
  60. ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32);
  61. if (EXT4_I(inode)->i_inline_off) {
  62. entry = (struct ext4_xattr_entry *)
  63. ((void *)raw_inode + EXT4_I(inode)->i_inline_off);
  64. free += le32_to_cpu(entry->e_value_size);
  65. goto out;
  66. }
  67. free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA));
  68. if (free > EXT4_XATTR_ROUND)
  69. free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND);
  70. else
  71. free = 0;
  72. out:
  73. return free;
  74. }
  75. /*
  76. * Get the maximum size we now can store in an inode.
  77. * If we can't find the space for a xattr entry, don't use the space
  78. * of the extents since we have no space to indicate the inline data.
  79. */
  80. int ext4_get_max_inline_size(struct inode *inode)
  81. {
  82. int error, max_inline_size;
  83. struct ext4_iloc iloc;
  84. if (EXT4_I(inode)->i_extra_isize == 0)
  85. return 0;
  86. error = ext4_get_inode_loc(inode, &iloc);
  87. if (error) {
  88. ext4_error_inode(inode, __func__, __LINE__, 0,
  89. "can't get inode location %lu",
  90. inode->i_ino);
  91. return 0;
  92. }
  93. down_read(&EXT4_I(inode)->xattr_sem);
  94. max_inline_size = get_max_inline_xattr_value_size(inode, &iloc);
  95. up_read(&EXT4_I(inode)->xattr_sem);
  96. brelse(iloc.bh);
  97. if (!max_inline_size)
  98. return 0;
  99. return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE;
  100. }
  101. int ext4_has_inline_data(struct inode *inode)
  102. {
  103. return ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA) &&
  104. EXT4_I(inode)->i_inline_off;
  105. }
  106. /*
  107. * this function does not take xattr_sem, which is OK because it is
  108. * currently only used in a code path coming form ext4_iget, before
  109. * the new inode has been unlocked
  110. */
  111. int ext4_find_inline_data_nolock(struct inode *inode)
  112. {
  113. struct ext4_xattr_ibody_find is = {
  114. .s = { .not_found = -ENODATA, },
  115. };
  116. struct ext4_xattr_info i = {
  117. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  118. .name = EXT4_XATTR_SYSTEM_DATA,
  119. };
  120. int error;
  121. if (EXT4_I(inode)->i_extra_isize == 0)
  122. return 0;
  123. error = ext4_get_inode_loc(inode, &is.iloc);
  124. if (error)
  125. return error;
  126. error = ext4_xattr_ibody_find(inode, &i, &is);
  127. if (error)
  128. goto out;
  129. if (!is.s.not_found) {
  130. EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
  131. (void *)ext4_raw_inode(&is.iloc));
  132. EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE +
  133. le32_to_cpu(is.s.here->e_value_size);
  134. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  135. }
  136. out:
  137. brelse(is.iloc.bh);
  138. return error;
  139. }
  140. static int ext4_read_inline_data(struct inode *inode, void *buffer,
  141. unsigned int len,
  142. struct ext4_iloc *iloc)
  143. {
  144. struct ext4_xattr_entry *entry;
  145. struct ext4_xattr_ibody_header *header;
  146. int cp_len = 0;
  147. struct ext4_inode *raw_inode;
  148. if (!len)
  149. return 0;
  150. BUG_ON(len > EXT4_I(inode)->i_inline_size);
  151. cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ?
  152. len : EXT4_MIN_INLINE_DATA_SIZE;
  153. raw_inode = ext4_raw_inode(iloc);
  154. memcpy(buffer, (void *)(raw_inode->i_block), cp_len);
  155. len -= cp_len;
  156. buffer += cp_len;
  157. if (!len)
  158. goto out;
  159. header = IHDR(inode, raw_inode);
  160. entry = (struct ext4_xattr_entry *)((void *)raw_inode +
  161. EXT4_I(inode)->i_inline_off);
  162. len = min_t(unsigned int, len,
  163. (unsigned int)le32_to_cpu(entry->e_value_size));
  164. memcpy(buffer,
  165. (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len);
  166. cp_len += len;
  167. out:
  168. return cp_len;
  169. }
  170. /*
  171. * write the buffer to the inline inode.
  172. * If 'create' is set, we don't need to do the extra copy in the xattr
  173. * value since it is already handled by ext4_xattr_ibody_inline_set.
  174. * That saves us one memcpy.
  175. */
  176. void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc,
  177. void *buffer, loff_t pos, unsigned int len)
  178. {
  179. struct ext4_xattr_entry *entry;
  180. struct ext4_xattr_ibody_header *header;
  181. struct ext4_inode *raw_inode;
  182. int cp_len = 0;
  183. BUG_ON(!EXT4_I(inode)->i_inline_off);
  184. BUG_ON(pos + len > EXT4_I(inode)->i_inline_size);
  185. raw_inode = ext4_raw_inode(iloc);
  186. buffer += pos;
  187. if (pos < EXT4_MIN_INLINE_DATA_SIZE) {
  188. cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ?
  189. EXT4_MIN_INLINE_DATA_SIZE - pos : len;
  190. memcpy((void *)raw_inode->i_block + pos, buffer, cp_len);
  191. len -= cp_len;
  192. buffer += cp_len;
  193. pos += cp_len;
  194. }
  195. if (!len)
  196. return;
  197. pos -= EXT4_MIN_INLINE_DATA_SIZE;
  198. header = IHDR(inode, raw_inode);
  199. entry = (struct ext4_xattr_entry *)((void *)raw_inode +
  200. EXT4_I(inode)->i_inline_off);
  201. memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos,
  202. buffer, len);
  203. }
  204. static int ext4_create_inline_data(handle_t *handle,
  205. struct inode *inode, unsigned len)
  206. {
  207. int error;
  208. void *value = NULL;
  209. struct ext4_xattr_ibody_find is = {
  210. .s = { .not_found = -ENODATA, },
  211. };
  212. struct ext4_xattr_info i = {
  213. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  214. .name = EXT4_XATTR_SYSTEM_DATA,
  215. };
  216. error = ext4_get_inode_loc(inode, &is.iloc);
  217. if (error)
  218. return error;
  219. error = ext4_journal_get_write_access(handle, is.iloc.bh);
  220. if (error)
  221. goto out;
  222. if (len > EXT4_MIN_INLINE_DATA_SIZE) {
  223. value = EXT4_ZERO_XATTR_VALUE;
  224. len -= EXT4_MIN_INLINE_DATA_SIZE;
  225. } else {
  226. value = "";
  227. len = 0;
  228. }
  229. /* Insert the the xttr entry. */
  230. i.value = value;
  231. i.value_len = len;
  232. error = ext4_xattr_ibody_find(inode, &i, &is);
  233. if (error)
  234. goto out;
  235. BUG_ON(!is.s.not_found);
  236. error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
  237. if (error) {
  238. if (error == -ENOSPC)
  239. ext4_clear_inode_state(inode,
  240. EXT4_STATE_MAY_INLINE_DATA);
  241. goto out;
  242. }
  243. memset((void *)ext4_raw_inode(&is.iloc)->i_block,
  244. 0, EXT4_MIN_INLINE_DATA_SIZE);
  245. EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here -
  246. (void *)ext4_raw_inode(&is.iloc));
  247. EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE;
  248. ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
  249. ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA);
  250. get_bh(is.iloc.bh);
  251. error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
  252. out:
  253. brelse(is.iloc.bh);
  254. return error;
  255. }
  256. static int ext4_update_inline_data(handle_t *handle, struct inode *inode,
  257. unsigned int len)
  258. {
  259. int error;
  260. void *value = NULL;
  261. struct ext4_xattr_ibody_find is = {
  262. .s = { .not_found = -ENODATA, },
  263. };
  264. struct ext4_xattr_info i = {
  265. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  266. .name = EXT4_XATTR_SYSTEM_DATA,
  267. };
  268. /* If the old space is ok, write the data directly. */
  269. if (len <= EXT4_I(inode)->i_inline_size)
  270. return 0;
  271. error = ext4_get_inode_loc(inode, &is.iloc);
  272. if (error)
  273. return error;
  274. error = ext4_xattr_ibody_find(inode, &i, &is);
  275. if (error)
  276. goto out;
  277. BUG_ON(is.s.not_found);
  278. len -= EXT4_MIN_INLINE_DATA_SIZE;
  279. value = kzalloc(len, GFP_NOFS);
  280. if (!value)
  281. goto out;
  282. error = ext4_xattr_ibody_get(inode, i.name_index, i.name,
  283. value, len);
  284. if (error == -ENODATA)
  285. goto out;
  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. int ext4_prepare_inline_data(handle_t *handle, struct inode *inode,
  308. unsigned int len)
  309. {
  310. int ret, size;
  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. down_write(&EXT4_I(inode)->xattr_sem);
  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. up_write(&EXT4_I(inode)->xattr_sem);
  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. error = ext4_journal_get_write_access(handle, is.iloc.bh);
  348. if (error)
  349. goto out;
  350. error = ext4_xattr_ibody_inline_set(handle, inode, &i, &is);
  351. if (error)
  352. goto out;
  353. memset((void *)ext4_raw_inode(&is.iloc)->i_block,
  354. 0, EXT4_MIN_INLINE_DATA_SIZE);
  355. if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
  356. EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  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_CACHE_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_CACHE_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;
  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. down_write(&EXT4_I(inode)->xattr_sem);
  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, ext4_get_block_write);
  481. else
  482. ret = __block_write_begin(page, from, to, ext4_get_block);
  483. if (!ret && ext4_should_journal_data(inode)) {
  484. ret = ext4_walk_page_buffers(handle, page_buffers(page),
  485. from, to, NULL,
  486. do_journal_get_write_access);
  487. }
  488. if (ret) {
  489. unlock_page(page);
  490. page_cache_release(page);
  491. ext4_orphan_add(handle, inode);
  492. up_write(&EXT4_I(inode)->xattr_sem);
  493. sem_held = 0;
  494. ext4_journal_stop(handle);
  495. handle = NULL;
  496. ext4_truncate_failed_write(inode);
  497. /*
  498. * If truncate failed early the inode might
  499. * still be on the orphan list; we need to
  500. * make sure the inode is removed from the
  501. * orphan list in that case.
  502. */
  503. if (inode->i_nlink)
  504. ext4_orphan_del(NULL, inode);
  505. }
  506. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  507. goto retry;
  508. block_commit_write(page, from, to);
  509. out:
  510. if (page) {
  511. unlock_page(page);
  512. page_cache_release(page);
  513. }
  514. if (sem_held)
  515. up_write(&EXT4_I(inode)->xattr_sem);
  516. if (handle)
  517. ext4_journal_stop(handle);
  518. brelse(iloc.bh);
  519. return ret;
  520. }
  521. /*
  522. * Try to write data in the inode.
  523. * If the inode has inline data, check whether the new write can be
  524. * in the inode also. If not, create the page the handle, move the data
  525. * to the page make it update and let the later codes create extent for it.
  526. */
  527. int ext4_try_to_write_inline_data(struct address_space *mapping,
  528. struct inode *inode,
  529. loff_t pos, unsigned len,
  530. unsigned flags,
  531. struct page **pagep)
  532. {
  533. int ret;
  534. handle_t *handle;
  535. struct page *page;
  536. struct ext4_iloc iloc;
  537. if (pos + len > ext4_get_max_inline_size(inode))
  538. goto convert;
  539. ret = ext4_get_inode_loc(inode, &iloc);
  540. if (ret)
  541. return ret;
  542. /*
  543. * The possible write could happen in the inode,
  544. * so try to reserve the space in inode first.
  545. */
  546. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  547. if (IS_ERR(handle)) {
  548. ret = PTR_ERR(handle);
  549. handle = NULL;
  550. goto out;
  551. }
  552. ret = ext4_prepare_inline_data(handle, inode, pos + len);
  553. if (ret && ret != -ENOSPC)
  554. goto out;
  555. /* We don't have space in inline inode, so convert it to extent. */
  556. if (ret == -ENOSPC) {
  557. ext4_journal_stop(handle);
  558. brelse(iloc.bh);
  559. goto convert;
  560. }
  561. flags |= AOP_FLAG_NOFS;
  562. page = grab_cache_page_write_begin(mapping, 0, flags);
  563. if (!page) {
  564. ret = -ENOMEM;
  565. goto out;
  566. }
  567. *pagep = page;
  568. down_read(&EXT4_I(inode)->xattr_sem);
  569. if (!ext4_has_inline_data(inode)) {
  570. ret = 0;
  571. unlock_page(page);
  572. page_cache_release(page);
  573. goto out_up_read;
  574. }
  575. if (!PageUptodate(page)) {
  576. ret = ext4_read_inline_page(inode, page);
  577. if (ret < 0)
  578. goto out_up_read;
  579. }
  580. ret = 1;
  581. handle = NULL;
  582. out_up_read:
  583. up_read(&EXT4_I(inode)->xattr_sem);
  584. out:
  585. if (handle)
  586. ext4_journal_stop(handle);
  587. brelse(iloc.bh);
  588. return ret;
  589. convert:
  590. return ext4_convert_inline_data_to_extent(mapping,
  591. inode, flags);
  592. }
  593. int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
  594. unsigned copied, struct page *page)
  595. {
  596. int ret;
  597. void *kaddr;
  598. struct ext4_iloc iloc;
  599. if (unlikely(copied < len)) {
  600. if (!PageUptodate(page)) {
  601. copied = 0;
  602. goto out;
  603. }
  604. }
  605. ret = ext4_get_inode_loc(inode, &iloc);
  606. if (ret) {
  607. ext4_std_error(inode->i_sb, ret);
  608. copied = 0;
  609. goto out;
  610. }
  611. down_write(&EXT4_I(inode)->xattr_sem);
  612. BUG_ON(!ext4_has_inline_data(inode));
  613. kaddr = kmap_atomic(page);
  614. ext4_write_inline_data(inode, &iloc, kaddr, pos, len);
  615. kunmap_atomic(kaddr);
  616. SetPageUptodate(page);
  617. /* clear page dirty so that writepages wouldn't work for us. */
  618. ClearPageDirty(page);
  619. up_write(&EXT4_I(inode)->xattr_sem);
  620. brelse(iloc.bh);
  621. out:
  622. return copied;
  623. }
  624. struct buffer_head *
  625. ext4_journalled_write_inline_data(struct inode *inode,
  626. unsigned len,
  627. struct page *page)
  628. {
  629. int ret;
  630. void *kaddr;
  631. struct ext4_iloc iloc;
  632. ret = ext4_get_inode_loc(inode, &iloc);
  633. if (ret) {
  634. ext4_std_error(inode->i_sb, ret);
  635. return NULL;
  636. }
  637. down_write(&EXT4_I(inode)->xattr_sem);
  638. kaddr = kmap_atomic(page);
  639. ext4_write_inline_data(inode, &iloc, kaddr, 0, len);
  640. kunmap_atomic(kaddr);
  641. up_write(&EXT4_I(inode)->xattr_sem);
  642. return iloc.bh;
  643. }
  644. /*
  645. * Try to make the page cache and handle ready for the inline data case.
  646. * We can call this function in 2 cases:
  647. * 1. The inode is created and the first write exceeds inline size. We can
  648. * clear the inode state safely.
  649. * 2. The inode has inline data, then we need to read the data, make it
  650. * update and dirty so that ext4_da_writepages can handle it. We don't
  651. * need to start the journal since the file's metatdata isn't changed now.
  652. */
  653. static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping,
  654. struct inode *inode,
  655. unsigned flags,
  656. void **fsdata)
  657. {
  658. int ret = 0, inline_size;
  659. struct page *page;
  660. page = grab_cache_page_write_begin(mapping, 0, flags);
  661. if (!page)
  662. return -ENOMEM;
  663. down_read(&EXT4_I(inode)->xattr_sem);
  664. if (!ext4_has_inline_data(inode)) {
  665. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  666. goto out;
  667. }
  668. inline_size = ext4_get_inline_size(inode);
  669. if (!PageUptodate(page)) {
  670. ret = ext4_read_inline_page(inode, page);
  671. if (ret < 0)
  672. goto out;
  673. }
  674. ret = __block_write_begin(page, 0, inline_size,
  675. ext4_da_get_block_prep);
  676. if (ret) {
  677. ext4_truncate_failed_write(inode);
  678. goto out;
  679. }
  680. SetPageDirty(page);
  681. SetPageUptodate(page);
  682. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  683. *fsdata = (void *)CONVERT_INLINE_DATA;
  684. out:
  685. up_read(&EXT4_I(inode)->xattr_sem);
  686. if (page) {
  687. unlock_page(page);
  688. page_cache_release(page);
  689. }
  690. return ret;
  691. }
  692. /*
  693. * Prepare the write for the inline data.
  694. * If the the data can be written into the inode, we just read
  695. * the page and make it uptodate, and start the journal.
  696. * Otherwise read the page, makes it dirty so that it can be
  697. * handle in writepages(the i_disksize update is left to the
  698. * normal ext4_da_write_end).
  699. */
  700. int ext4_da_write_inline_data_begin(struct address_space *mapping,
  701. struct inode *inode,
  702. loff_t pos, unsigned len,
  703. unsigned flags,
  704. struct page **pagep,
  705. void **fsdata)
  706. {
  707. int ret, inline_size;
  708. handle_t *handle;
  709. struct page *page;
  710. struct ext4_iloc iloc;
  711. ret = ext4_get_inode_loc(inode, &iloc);
  712. if (ret)
  713. return ret;
  714. handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
  715. if (IS_ERR(handle)) {
  716. ret = PTR_ERR(handle);
  717. handle = NULL;
  718. goto out;
  719. }
  720. inline_size = ext4_get_max_inline_size(inode);
  721. ret = -ENOSPC;
  722. if (inline_size >= pos + len) {
  723. ret = ext4_prepare_inline_data(handle, inode, pos + len);
  724. if (ret && ret != -ENOSPC)
  725. goto out;
  726. }
  727. if (ret == -ENOSPC) {
  728. ret = ext4_da_convert_inline_data_to_extent(mapping,
  729. inode,
  730. flags,
  731. fsdata);
  732. goto out;
  733. }
  734. /*
  735. * We cannot recurse into the filesystem as the transaction
  736. * is already started.
  737. */
  738. flags |= AOP_FLAG_NOFS;
  739. page = grab_cache_page_write_begin(mapping, 0, flags);
  740. if (!page) {
  741. ret = -ENOMEM;
  742. goto out;
  743. }
  744. down_read(&EXT4_I(inode)->xattr_sem);
  745. if (!ext4_has_inline_data(inode)) {
  746. ret = 0;
  747. goto out_release_page;
  748. }
  749. if (!PageUptodate(page)) {
  750. ret = ext4_read_inline_page(inode, page);
  751. if (ret < 0)
  752. goto out_release_page;
  753. }
  754. up_read(&EXT4_I(inode)->xattr_sem);
  755. *pagep = page;
  756. handle = NULL;
  757. brelse(iloc.bh);
  758. return 1;
  759. out_release_page:
  760. up_read(&EXT4_I(inode)->xattr_sem);
  761. unlock_page(page);
  762. page_cache_release(page);
  763. out:
  764. if (handle)
  765. ext4_journal_stop(handle);
  766. brelse(iloc.bh);
  767. return ret;
  768. }
  769. int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
  770. unsigned len, unsigned copied,
  771. struct page *page)
  772. {
  773. int i_size_changed = 0;
  774. copied = ext4_write_inline_data_end(inode, pos, len, copied, page);
  775. /*
  776. * No need to use i_size_read() here, the i_size
  777. * cannot change under us because we hold i_mutex.
  778. *
  779. * But it's important to update i_size while still holding page lock:
  780. * page writeout could otherwise come in and zero beyond i_size.
  781. */
  782. if (pos+copied > inode->i_size) {
  783. i_size_write(inode, pos+copied);
  784. i_size_changed = 1;
  785. }
  786. unlock_page(page);
  787. page_cache_release(page);
  788. /*
  789. * Don't mark the inode dirty under page lock. First, it unnecessarily
  790. * makes the holding time of page lock longer. Second, it forces lock
  791. * ordering of page lock and transaction start for journaling
  792. * filesystems.
  793. */
  794. if (i_size_changed)
  795. mark_inode_dirty(inode);
  796. return copied;
  797. }
  798. #ifdef INLINE_DIR_DEBUG
  799. void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh,
  800. void *inline_start, int inline_size)
  801. {
  802. int offset;
  803. unsigned short de_len;
  804. struct ext4_dir_entry_2 *de = inline_start;
  805. void *dlimit = inline_start + inline_size;
  806. trace_printk("inode %lu\n", dir->i_ino);
  807. offset = 0;
  808. while ((void *)de < dlimit) {
  809. de_len = ext4_rec_len_from_disk(de->rec_len, inline_size);
  810. trace_printk("de: off %u rlen %u name %*.s nlen %u ino %u\n",
  811. offset, de_len, de->name_len, de->name,
  812. de->name_len, le32_to_cpu(de->inode));
  813. if (ext4_check_dir_entry(dir, NULL, de, bh,
  814. inline_start, inline_size, offset))
  815. BUG();
  816. offset += de_len;
  817. de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
  818. }
  819. }
  820. #else
  821. #define ext4_show_inline_dir(dir, bh, inline_start, inline_size)
  822. #endif
  823. /*
  824. * Add a new entry into a inline dir.
  825. * It will return -ENOSPC if no space is available, and -EIO
  826. * and -EEXIST if directory entry already exists.
  827. */
  828. static int ext4_add_dirent_to_inline(handle_t *handle,
  829. struct dentry *dentry,
  830. struct inode *inode,
  831. struct ext4_iloc *iloc,
  832. void *inline_start, int inline_size)
  833. {
  834. struct inode *dir = dentry->d_parent->d_inode;
  835. const char *name = dentry->d_name.name;
  836. int namelen = dentry->d_name.len;
  837. unsigned short reclen;
  838. int err;
  839. struct ext4_dir_entry_2 *de;
  840. reclen = EXT4_DIR_REC_LEN(namelen);
  841. err = ext4_find_dest_de(dir, inode, iloc->bh,
  842. inline_start, inline_size,
  843. name, namelen, &de);
  844. if (err)
  845. return err;
  846. err = ext4_journal_get_write_access(handle, iloc->bh);
  847. if (err)
  848. return err;
  849. ext4_insert_dentry(inode, de, inline_size, name, namelen);
  850. ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size);
  851. /*
  852. * XXX shouldn't update any times until successful
  853. * completion of syscall, but too many callers depend
  854. * on this.
  855. *
  856. * XXX similarly, too many callers depend on
  857. * ext4_new_inode() setting the times, but error
  858. * recovery deletes the inode, so the worst that can
  859. * happen is that the times are slightly out of date
  860. * and/or different from the directory change time.
  861. */
  862. dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
  863. ext4_update_dx_flag(dir);
  864. dir->i_version++;
  865. ext4_mark_inode_dirty(handle, dir);
  866. return 1;
  867. }
  868. static void *ext4_get_inline_xattr_pos(struct inode *inode,
  869. struct ext4_iloc *iloc)
  870. {
  871. struct ext4_xattr_entry *entry;
  872. struct ext4_xattr_ibody_header *header;
  873. BUG_ON(!EXT4_I(inode)->i_inline_off);
  874. header = IHDR(inode, ext4_raw_inode(iloc));
  875. entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) +
  876. EXT4_I(inode)->i_inline_off);
  877. return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs);
  878. }
  879. /* Set the final de to cover the whole block. */
  880. static void ext4_update_final_de(void *de_buf, int old_size, int new_size)
  881. {
  882. struct ext4_dir_entry_2 *de, *prev_de;
  883. void *limit;
  884. int de_len;
  885. de = (struct ext4_dir_entry_2 *)de_buf;
  886. if (old_size) {
  887. limit = de_buf + old_size;
  888. do {
  889. prev_de = de;
  890. de_len = ext4_rec_len_from_disk(de->rec_len, old_size);
  891. de_buf += de_len;
  892. de = (struct ext4_dir_entry_2 *)de_buf;
  893. } while (de_buf < limit);
  894. prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size -
  895. old_size, new_size);
  896. } else {
  897. /* this is just created, so create an empty entry. */
  898. de->inode = 0;
  899. de->rec_len = ext4_rec_len_to_disk(new_size, new_size);
  900. }
  901. }
  902. static int ext4_update_inline_dir(handle_t *handle, struct inode *dir,
  903. struct ext4_iloc *iloc)
  904. {
  905. int ret;
  906. int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE;
  907. int new_size = get_max_inline_xattr_value_size(dir, iloc);
  908. if (new_size - old_size <= EXT4_DIR_REC_LEN(1))
  909. return -ENOSPC;
  910. ret = ext4_update_inline_data(handle, dir,
  911. new_size + EXT4_MIN_INLINE_DATA_SIZE);
  912. if (ret)
  913. return ret;
  914. ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size,
  915. EXT4_I(dir)->i_inline_size -
  916. EXT4_MIN_INLINE_DATA_SIZE);
  917. dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size;
  918. return 0;
  919. }
  920. static void ext4_restore_inline_data(handle_t *handle, struct inode *inode,
  921. struct ext4_iloc *iloc,
  922. void *buf, int inline_size)
  923. {
  924. ext4_create_inline_data(handle, inode, inline_size);
  925. ext4_write_inline_data(inode, iloc, buf, 0, inline_size);
  926. ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  927. }
  928. static int ext4_finish_convert_inline_dir(handle_t *handle,
  929. struct inode *inode,
  930. struct buffer_head *dir_block,
  931. void *buf,
  932. int inline_size)
  933. {
  934. int err, csum_size = 0, header_size = 0;
  935. struct ext4_dir_entry_2 *de;
  936. struct ext4_dir_entry_tail *t;
  937. void *target = dir_block->b_data;
  938. /*
  939. * First create "." and ".." and then copy the dir information
  940. * back to the block.
  941. */
  942. de = (struct ext4_dir_entry_2 *)target;
  943. de = ext4_init_dot_dotdot(inode, de,
  944. inode->i_sb->s_blocksize, csum_size,
  945. le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1);
  946. header_size = (void *)de - target;
  947. memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE,
  948. inline_size - EXT4_INLINE_DOTDOT_SIZE);
  949. if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  950. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  951. csum_size = sizeof(struct ext4_dir_entry_tail);
  952. inode->i_size = inode->i_sb->s_blocksize;
  953. i_size_write(inode, inode->i_sb->s_blocksize);
  954. EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
  955. ext4_update_final_de(dir_block->b_data,
  956. inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size,
  957. inode->i_sb->s_blocksize - csum_size);
  958. if (csum_size) {
  959. t = EXT4_DIRENT_TAIL(dir_block->b_data,
  960. inode->i_sb->s_blocksize);
  961. initialize_dirent_tail(t, inode->i_sb->s_blocksize);
  962. }
  963. set_buffer_uptodate(dir_block);
  964. err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
  965. if (err)
  966. goto out;
  967. set_buffer_verified(dir_block);
  968. out:
  969. return err;
  970. }
  971. static int ext4_convert_inline_data_nolock(handle_t *handle,
  972. struct inode *inode,
  973. struct ext4_iloc *iloc)
  974. {
  975. int error;
  976. void *buf = NULL;
  977. struct buffer_head *data_bh = NULL;
  978. struct ext4_map_blocks map;
  979. int inline_size;
  980. inline_size = ext4_get_inline_size(inode);
  981. buf = kmalloc(inline_size, GFP_NOFS);
  982. if (!buf) {
  983. error = -ENOMEM;
  984. goto out;
  985. }
  986. error = ext4_read_inline_data(inode, buf, inline_size, iloc);
  987. if (error < 0)
  988. goto out;
  989. error = ext4_destroy_inline_data_nolock(handle, inode);
  990. if (error)
  991. goto out;
  992. map.m_lblk = 0;
  993. map.m_len = 1;
  994. map.m_flags = 0;
  995. error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE);
  996. if (error < 0)
  997. goto out_restore;
  998. if (!(map.m_flags & EXT4_MAP_MAPPED)) {
  999. error = -EIO;
  1000. goto out_restore;
  1001. }
  1002. data_bh = sb_getblk(inode->i_sb, map.m_pblk);
  1003. if (!data_bh) {
  1004. error = -ENOMEM;
  1005. goto out_restore;
  1006. }
  1007. lock_buffer(data_bh);
  1008. error = ext4_journal_get_create_access(handle, data_bh);
  1009. if (error) {
  1010. unlock_buffer(data_bh);
  1011. error = -EIO;
  1012. goto out_restore;
  1013. }
  1014. memset(data_bh->b_data, 0, inode->i_sb->s_blocksize);
  1015. if (!S_ISDIR(inode->i_mode)) {
  1016. memcpy(data_bh->b_data, buf, inline_size);
  1017. set_buffer_uptodate(data_bh);
  1018. error = ext4_handle_dirty_metadata(handle,
  1019. inode, data_bh);
  1020. } else {
  1021. error = ext4_finish_convert_inline_dir(handle, inode, data_bh,
  1022. buf, inline_size);
  1023. }
  1024. unlock_buffer(data_bh);
  1025. out_restore:
  1026. if (error)
  1027. ext4_restore_inline_data(handle, inode, iloc, buf, inline_size);
  1028. out:
  1029. brelse(data_bh);
  1030. kfree(buf);
  1031. return error;
  1032. }
  1033. /*
  1034. * Try to add the new entry to the inline data.
  1035. * If succeeds, return 0. If not, extended the inline dir and copied data to
  1036. * the new created block.
  1037. */
  1038. int ext4_try_add_inline_entry(handle_t *handle, struct dentry *dentry,
  1039. struct inode *inode)
  1040. {
  1041. int ret, inline_size;
  1042. void *inline_start;
  1043. struct ext4_iloc iloc;
  1044. struct inode *dir = dentry->d_parent->d_inode;
  1045. ret = ext4_get_inode_loc(dir, &iloc);
  1046. if (ret)
  1047. return ret;
  1048. down_write(&EXT4_I(dir)->xattr_sem);
  1049. if (!ext4_has_inline_data(dir))
  1050. goto out;
  1051. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1052. EXT4_INLINE_DOTDOT_SIZE;
  1053. inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
  1054. ret = ext4_add_dirent_to_inline(handle, dentry, inode, &iloc,
  1055. inline_start, inline_size);
  1056. if (ret != -ENOSPC)
  1057. goto out;
  1058. /* check whether it can be inserted to inline xattr space. */
  1059. inline_size = EXT4_I(dir)->i_inline_size -
  1060. EXT4_MIN_INLINE_DATA_SIZE;
  1061. if (!inline_size) {
  1062. /* Try to use the xattr space.*/
  1063. ret = ext4_update_inline_dir(handle, dir, &iloc);
  1064. if (ret && ret != -ENOSPC)
  1065. goto out;
  1066. inline_size = EXT4_I(dir)->i_inline_size -
  1067. EXT4_MIN_INLINE_DATA_SIZE;
  1068. }
  1069. if (inline_size) {
  1070. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1071. ret = ext4_add_dirent_to_inline(handle, dentry, inode, &iloc,
  1072. inline_start, inline_size);
  1073. if (ret != -ENOSPC)
  1074. goto out;
  1075. }
  1076. /*
  1077. * The inline space is filled up, so create a new block for it.
  1078. * As the extent tree will be created, we have to save the inline
  1079. * dir first.
  1080. */
  1081. ret = ext4_convert_inline_data_nolock(handle, dir, &iloc);
  1082. out:
  1083. ext4_mark_inode_dirty(handle, dir);
  1084. up_write(&EXT4_I(dir)->xattr_sem);
  1085. brelse(iloc.bh);
  1086. return ret;
  1087. }
  1088. int ext4_read_inline_dir(struct file *filp,
  1089. void *dirent, filldir_t filldir,
  1090. int *has_inline_data)
  1091. {
  1092. int error = 0;
  1093. unsigned int offset, parent_ino;
  1094. int i, stored;
  1095. struct ext4_dir_entry_2 *de;
  1096. struct super_block *sb;
  1097. struct inode *inode = file_inode(filp);
  1098. int ret, inline_size = 0;
  1099. struct ext4_iloc iloc;
  1100. void *dir_buf = NULL;
  1101. ret = ext4_get_inode_loc(inode, &iloc);
  1102. if (ret)
  1103. return ret;
  1104. down_read(&EXT4_I(inode)->xattr_sem);
  1105. if (!ext4_has_inline_data(inode)) {
  1106. up_read(&EXT4_I(inode)->xattr_sem);
  1107. *has_inline_data = 0;
  1108. goto out;
  1109. }
  1110. inline_size = ext4_get_inline_size(inode);
  1111. dir_buf = kmalloc(inline_size, GFP_NOFS);
  1112. if (!dir_buf) {
  1113. ret = -ENOMEM;
  1114. up_read(&EXT4_I(inode)->xattr_sem);
  1115. goto out;
  1116. }
  1117. ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc);
  1118. up_read(&EXT4_I(inode)->xattr_sem);
  1119. if (ret < 0)
  1120. goto out;
  1121. sb = inode->i_sb;
  1122. stored = 0;
  1123. parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode);
  1124. while (!error && !stored && filp->f_pos < inode->i_size) {
  1125. revalidate:
  1126. /*
  1127. * If the version has changed since the last call to
  1128. * readdir(2), then we might be pointing to an invalid
  1129. * dirent right now. Scan from the start of the inline
  1130. * dir to make sure.
  1131. */
  1132. if (filp->f_version != inode->i_version) {
  1133. for (i = 0;
  1134. i < inode->i_size && i < offset;) {
  1135. if (!i) {
  1136. /* skip "." and ".." if needed. */
  1137. i += EXT4_INLINE_DOTDOT_SIZE;
  1138. continue;
  1139. }
  1140. de = (struct ext4_dir_entry_2 *)
  1141. (dir_buf + i);
  1142. /* It's too expensive to do a full
  1143. * dirent test each time round this
  1144. * loop, but we do have to test at
  1145. * least that it is non-zero. A
  1146. * failure will be detected in the
  1147. * dirent test below. */
  1148. if (ext4_rec_len_from_disk(de->rec_len,
  1149. inline_size) < EXT4_DIR_REC_LEN(1))
  1150. break;
  1151. i += ext4_rec_len_from_disk(de->rec_len,
  1152. inline_size);
  1153. }
  1154. offset = i;
  1155. filp->f_pos = offset;
  1156. filp->f_version = inode->i_version;
  1157. }
  1158. while (!error && filp->f_pos < inode->i_size) {
  1159. if (filp->f_pos == 0) {
  1160. error = filldir(dirent, ".", 1, 0, inode->i_ino,
  1161. DT_DIR);
  1162. if (error)
  1163. break;
  1164. stored++;
  1165. error = filldir(dirent, "..", 2, 0, parent_ino,
  1166. DT_DIR);
  1167. if (error)
  1168. break;
  1169. stored++;
  1170. filp->f_pos = offset = EXT4_INLINE_DOTDOT_SIZE;
  1171. continue;
  1172. }
  1173. de = (struct ext4_dir_entry_2 *)(dir_buf + offset);
  1174. if (ext4_check_dir_entry(inode, filp, de,
  1175. iloc.bh, dir_buf,
  1176. inline_size, offset)) {
  1177. ret = stored;
  1178. goto out;
  1179. }
  1180. offset += ext4_rec_len_from_disk(de->rec_len,
  1181. inline_size);
  1182. if (le32_to_cpu(de->inode)) {
  1183. /* We might block in the next section
  1184. * if the data destination is
  1185. * currently swapped out. So, use a
  1186. * version stamp to detect whether or
  1187. * not the directory has been modified
  1188. * during the copy operation.
  1189. */
  1190. u64 version = filp->f_version;
  1191. error = filldir(dirent, de->name,
  1192. de->name_len,
  1193. filp->f_pos,
  1194. le32_to_cpu(de->inode),
  1195. get_dtype(sb, de->file_type));
  1196. if (error)
  1197. break;
  1198. if (version != filp->f_version)
  1199. goto revalidate;
  1200. stored++;
  1201. }
  1202. filp->f_pos += ext4_rec_len_from_disk(de->rec_len,
  1203. inline_size);
  1204. }
  1205. offset = 0;
  1206. }
  1207. out:
  1208. kfree(dir_buf);
  1209. brelse(iloc.bh);
  1210. return ret;
  1211. }
  1212. struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
  1213. struct ext4_dir_entry_2 **parent_de,
  1214. int *retval)
  1215. {
  1216. struct ext4_iloc iloc;
  1217. *retval = ext4_get_inode_loc(inode, &iloc);
  1218. if (*retval)
  1219. return NULL;
  1220. *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1221. return iloc.bh;
  1222. }
  1223. /*
  1224. * Try to create the inline data for the new dir.
  1225. * If it succeeds, return 0, otherwise return the error.
  1226. * In case of ENOSPC, the caller should create the normal disk layout dir.
  1227. */
  1228. int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent,
  1229. struct inode *inode)
  1230. {
  1231. int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE;
  1232. struct ext4_iloc iloc;
  1233. struct ext4_dir_entry_2 *de;
  1234. ret = ext4_get_inode_loc(inode, &iloc);
  1235. if (ret)
  1236. return ret;
  1237. ret = ext4_prepare_inline_data(handle, inode, inline_size);
  1238. if (ret)
  1239. goto out;
  1240. /*
  1241. * For inline dir, we only save the inode information for the ".."
  1242. * and create a fake dentry to cover the left space.
  1243. */
  1244. de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1245. de->inode = cpu_to_le32(parent->i_ino);
  1246. de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE);
  1247. de->inode = 0;
  1248. de->rec_len = ext4_rec_len_to_disk(
  1249. inline_size - EXT4_INLINE_DOTDOT_SIZE,
  1250. inline_size);
  1251. set_nlink(inode, 2);
  1252. inode->i_size = EXT4_I(inode)->i_disksize = inline_size;
  1253. out:
  1254. brelse(iloc.bh);
  1255. return ret;
  1256. }
  1257. struct buffer_head *ext4_find_inline_entry(struct inode *dir,
  1258. const struct qstr *d_name,
  1259. struct ext4_dir_entry_2 **res_dir,
  1260. int *has_inline_data)
  1261. {
  1262. int ret;
  1263. struct ext4_iloc iloc;
  1264. void *inline_start;
  1265. int inline_size;
  1266. if (ext4_get_inode_loc(dir, &iloc))
  1267. return NULL;
  1268. down_read(&EXT4_I(dir)->xattr_sem);
  1269. if (!ext4_has_inline_data(dir)) {
  1270. *has_inline_data = 0;
  1271. goto out;
  1272. }
  1273. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1274. EXT4_INLINE_DOTDOT_SIZE;
  1275. inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE;
  1276. ret = search_dir(iloc.bh, inline_start, inline_size,
  1277. dir, d_name, 0, res_dir);
  1278. if (ret == 1)
  1279. goto out_find;
  1280. if (ret < 0)
  1281. goto out;
  1282. if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE)
  1283. goto out;
  1284. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1285. inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE;
  1286. ret = search_dir(iloc.bh, inline_start, inline_size,
  1287. dir, d_name, 0, res_dir);
  1288. if (ret == 1)
  1289. goto out_find;
  1290. out:
  1291. brelse(iloc.bh);
  1292. iloc.bh = NULL;
  1293. out_find:
  1294. up_read(&EXT4_I(dir)->xattr_sem);
  1295. return iloc.bh;
  1296. }
  1297. int ext4_delete_inline_entry(handle_t *handle,
  1298. struct inode *dir,
  1299. struct ext4_dir_entry_2 *de_del,
  1300. struct buffer_head *bh,
  1301. int *has_inline_data)
  1302. {
  1303. int err, inline_size;
  1304. struct ext4_iloc iloc;
  1305. void *inline_start;
  1306. err = ext4_get_inode_loc(dir, &iloc);
  1307. if (err)
  1308. return err;
  1309. down_write(&EXT4_I(dir)->xattr_sem);
  1310. if (!ext4_has_inline_data(dir)) {
  1311. *has_inline_data = 0;
  1312. goto out;
  1313. }
  1314. if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) <
  1315. EXT4_MIN_INLINE_DATA_SIZE) {
  1316. inline_start = (void *)ext4_raw_inode(&iloc)->i_block +
  1317. EXT4_INLINE_DOTDOT_SIZE;
  1318. inline_size = EXT4_MIN_INLINE_DATA_SIZE -
  1319. EXT4_INLINE_DOTDOT_SIZE;
  1320. } else {
  1321. inline_start = ext4_get_inline_xattr_pos(dir, &iloc);
  1322. inline_size = ext4_get_inline_size(dir) -
  1323. EXT4_MIN_INLINE_DATA_SIZE;
  1324. }
  1325. err = ext4_journal_get_write_access(handle, bh);
  1326. if (err)
  1327. goto out;
  1328. err = ext4_generic_delete_entry(handle, dir, de_del, bh,
  1329. inline_start, inline_size, 0);
  1330. if (err)
  1331. goto out;
  1332. BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
  1333. err = ext4_mark_inode_dirty(handle, dir);
  1334. if (unlikely(err))
  1335. goto out;
  1336. ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size);
  1337. out:
  1338. up_write(&EXT4_I(dir)->xattr_sem);
  1339. brelse(iloc.bh);
  1340. if (err != -ENOENT)
  1341. ext4_std_error(dir->i_sb, err);
  1342. return err;
  1343. }
  1344. /*
  1345. * Get the inline dentry at offset.
  1346. */
  1347. static inline struct ext4_dir_entry_2 *
  1348. ext4_get_inline_entry(struct inode *inode,
  1349. struct ext4_iloc *iloc,
  1350. unsigned int offset,
  1351. void **inline_start,
  1352. int *inline_size)
  1353. {
  1354. void *inline_pos;
  1355. BUG_ON(offset > ext4_get_inline_size(inode));
  1356. if (offset < EXT4_MIN_INLINE_DATA_SIZE) {
  1357. inline_pos = (void *)ext4_raw_inode(iloc)->i_block;
  1358. *inline_size = EXT4_MIN_INLINE_DATA_SIZE;
  1359. } else {
  1360. inline_pos = ext4_get_inline_xattr_pos(inode, iloc);
  1361. offset -= EXT4_MIN_INLINE_DATA_SIZE;
  1362. *inline_size = ext4_get_inline_size(inode) -
  1363. EXT4_MIN_INLINE_DATA_SIZE;
  1364. }
  1365. if (inline_start)
  1366. *inline_start = inline_pos;
  1367. return (struct ext4_dir_entry_2 *)(inline_pos + offset);
  1368. }
  1369. int empty_inline_dir(struct inode *dir, int *has_inline_data)
  1370. {
  1371. int err, inline_size;
  1372. struct ext4_iloc iloc;
  1373. void *inline_pos;
  1374. unsigned int offset;
  1375. struct ext4_dir_entry_2 *de;
  1376. int ret = 1;
  1377. err = ext4_get_inode_loc(dir, &iloc);
  1378. if (err) {
  1379. EXT4_ERROR_INODE(dir, "error %d getting inode %lu block",
  1380. err, dir->i_ino);
  1381. return 1;
  1382. }
  1383. down_read(&EXT4_I(dir)->xattr_sem);
  1384. if (!ext4_has_inline_data(dir)) {
  1385. *has_inline_data = 0;
  1386. goto out;
  1387. }
  1388. de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block;
  1389. if (!le32_to_cpu(de->inode)) {
  1390. ext4_warning(dir->i_sb,
  1391. "bad inline directory (dir #%lu) - no `..'",
  1392. dir->i_ino);
  1393. ret = 1;
  1394. goto out;
  1395. }
  1396. offset = EXT4_INLINE_DOTDOT_SIZE;
  1397. while (offset < dir->i_size) {
  1398. de = ext4_get_inline_entry(dir, &iloc, offset,
  1399. &inline_pos, &inline_size);
  1400. if (ext4_check_dir_entry(dir, NULL, de,
  1401. iloc.bh, inline_pos,
  1402. inline_size, offset)) {
  1403. ext4_warning(dir->i_sb,
  1404. "bad inline directory (dir #%lu) - "
  1405. "inode %u, rec_len %u, name_len %d"
  1406. "inline size %d\n",
  1407. dir->i_ino, le32_to_cpu(de->inode),
  1408. le16_to_cpu(de->rec_len), de->name_len,
  1409. inline_size);
  1410. ret = 1;
  1411. goto out;
  1412. }
  1413. if (le32_to_cpu(de->inode)) {
  1414. ret = 0;
  1415. goto out;
  1416. }
  1417. offset += ext4_rec_len_from_disk(de->rec_len, inline_size);
  1418. }
  1419. out:
  1420. up_read(&EXT4_I(dir)->xattr_sem);
  1421. brelse(iloc.bh);
  1422. return ret;
  1423. }
  1424. int ext4_destroy_inline_data(handle_t *handle, struct inode *inode)
  1425. {
  1426. int ret;
  1427. down_write(&EXT4_I(inode)->xattr_sem);
  1428. ret = ext4_destroy_inline_data_nolock(handle, inode);
  1429. up_write(&EXT4_I(inode)->xattr_sem);
  1430. return ret;
  1431. }
  1432. int ext4_inline_data_fiemap(struct inode *inode,
  1433. struct fiemap_extent_info *fieinfo,
  1434. int *has_inline)
  1435. {
  1436. __u64 physical = 0;
  1437. __u64 length;
  1438. __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_LAST;
  1439. int error = 0;
  1440. struct ext4_iloc iloc;
  1441. down_read(&EXT4_I(inode)->xattr_sem);
  1442. if (!ext4_has_inline_data(inode)) {
  1443. *has_inline = 0;
  1444. goto out;
  1445. }
  1446. error = ext4_get_inode_loc(inode, &iloc);
  1447. if (error)
  1448. goto out;
  1449. physical = iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits;
  1450. physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data;
  1451. physical += offsetof(struct ext4_inode, i_block);
  1452. length = i_size_read(inode);
  1453. if (physical)
  1454. error = fiemap_fill_next_extent(fieinfo, 0, physical,
  1455. length, flags);
  1456. brelse(iloc.bh);
  1457. out:
  1458. up_read(&EXT4_I(inode)->xattr_sem);
  1459. return (error < 0 ? error : 0);
  1460. }
  1461. /*
  1462. * Called during xattr set, and if we can sparse space 'needed',
  1463. * just create the extent tree evict the data to the outer block.
  1464. *
  1465. * We use jbd2 instead of page cache to move data to the 1st block
  1466. * so that the whole transaction can be committed as a whole and
  1467. * the data isn't lost because of the delayed page cache write.
  1468. */
  1469. int ext4_try_to_evict_inline_data(handle_t *handle,
  1470. struct inode *inode,
  1471. int needed)
  1472. {
  1473. int error;
  1474. struct ext4_xattr_entry *entry;
  1475. struct ext4_xattr_ibody_header *header;
  1476. struct ext4_inode *raw_inode;
  1477. struct ext4_iloc iloc;
  1478. error = ext4_get_inode_loc(inode, &iloc);
  1479. if (error)
  1480. return error;
  1481. raw_inode = ext4_raw_inode(&iloc);
  1482. header = IHDR(inode, raw_inode);
  1483. entry = (struct ext4_xattr_entry *)((void *)raw_inode +
  1484. EXT4_I(inode)->i_inline_off);
  1485. if (EXT4_XATTR_LEN(entry->e_name_len) +
  1486. EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)) < needed) {
  1487. error = -ENOSPC;
  1488. goto out;
  1489. }
  1490. error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
  1491. out:
  1492. brelse(iloc.bh);
  1493. return error;
  1494. }
  1495. void ext4_inline_data_truncate(struct inode *inode, int *has_inline)
  1496. {
  1497. handle_t *handle;
  1498. int inline_size, value_len, needed_blocks;
  1499. size_t i_size;
  1500. void *value = NULL;
  1501. struct ext4_xattr_ibody_find is = {
  1502. .s = { .not_found = -ENODATA, },
  1503. };
  1504. struct ext4_xattr_info i = {
  1505. .name_index = EXT4_XATTR_INDEX_SYSTEM,
  1506. .name = EXT4_XATTR_SYSTEM_DATA,
  1507. };
  1508. needed_blocks = ext4_writepage_trans_blocks(inode);
  1509. handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks);
  1510. if (IS_ERR(handle))
  1511. return;
  1512. down_write(&EXT4_I(inode)->xattr_sem);
  1513. if (!ext4_has_inline_data(inode)) {
  1514. *has_inline = 0;
  1515. ext4_journal_stop(handle);
  1516. return;
  1517. }
  1518. if (ext4_orphan_add(handle, inode))
  1519. goto out;
  1520. if (ext4_get_inode_loc(inode, &is.iloc))
  1521. goto out;
  1522. down_write(&EXT4_I(inode)->i_data_sem);
  1523. i_size = inode->i_size;
  1524. inline_size = ext4_get_inline_size(inode);
  1525. EXT4_I(inode)->i_disksize = i_size;
  1526. if (i_size < inline_size) {
  1527. /* Clear the content in the xattr space. */
  1528. if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) {
  1529. if (ext4_xattr_ibody_find(inode, &i, &is))
  1530. goto out_error;
  1531. BUG_ON(is.s.not_found);
  1532. value_len = le32_to_cpu(is.s.here->e_value_size);
  1533. value = kmalloc(value_len, GFP_NOFS);
  1534. if (!value)
  1535. goto out_error;
  1536. if (ext4_xattr_ibody_get(inode, i.name_index, i.name,
  1537. value, value_len))
  1538. goto out_error;
  1539. i.value = value;
  1540. i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ?
  1541. i_size - EXT4_MIN_INLINE_DATA_SIZE : 0;
  1542. if (ext4_xattr_ibody_inline_set(handle, inode, &i, &is))
  1543. goto out_error;
  1544. }
  1545. /* Clear the content within i_blocks. */
  1546. if (i_size < EXT4_MIN_INLINE_DATA_SIZE)
  1547. memset(ext4_raw_inode(&is.iloc)->i_block + i_size, 0,
  1548. EXT4_MIN_INLINE_DATA_SIZE - i_size);
  1549. EXT4_I(inode)->i_inline_size = i_size <
  1550. EXT4_MIN_INLINE_DATA_SIZE ?
  1551. EXT4_MIN_INLINE_DATA_SIZE : i_size;
  1552. }
  1553. out_error:
  1554. up_write(&EXT4_I(inode)->i_data_sem);
  1555. out:
  1556. brelse(is.iloc.bh);
  1557. up_write(&EXT4_I(inode)->xattr_sem);
  1558. kfree(value);
  1559. if (inode->i_nlink)
  1560. ext4_orphan_del(handle, inode);
  1561. inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
  1562. ext4_mark_inode_dirty(handle, inode);
  1563. if (IS_SYNC(inode))
  1564. ext4_handle_sync(handle);
  1565. ext4_journal_stop(handle);
  1566. return;
  1567. }
  1568. int ext4_convert_inline_data(struct inode *inode)
  1569. {
  1570. int error, needed_blocks;
  1571. handle_t *handle;
  1572. struct ext4_iloc iloc;
  1573. if (!ext4_has_inline_data(inode)) {
  1574. ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
  1575. return 0;
  1576. }
  1577. needed_blocks = ext4_writepage_trans_blocks(inode);
  1578. iloc.bh = NULL;
  1579. error = ext4_get_inode_loc(inode, &iloc);
  1580. if (error)
  1581. return error;
  1582. handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
  1583. if (IS_ERR(handle)) {
  1584. error = PTR_ERR(handle);
  1585. goto out_free;
  1586. }
  1587. down_write(&EXT4_I(inode)->xattr_sem);
  1588. if (!ext4_has_inline_data(inode)) {
  1589. up_write(&EXT4_I(inode)->xattr_sem);
  1590. goto out;
  1591. }
  1592. error = ext4_convert_inline_data_nolock(handle, inode, &iloc);
  1593. up_write(&EXT4_I(inode)->xattr_sem);
  1594. out:
  1595. ext4_journal_stop(handle);
  1596. out_free:
  1597. brelse(iloc.bh);
  1598. return error;
  1599. }