inline.c 50 KB

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