inline.c 50 KB

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