inline.c 49 KB

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