inline.c 49 KB

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