inline.c 49 KB

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