inline.c 50 KB

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