extents.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * Architecture independence:
  6. * Copyright (c) 2005, Bull S.A.
  7. * Written by Pierre Peiffer <pierre.peiffer@bull.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public Licens
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  21. */
  22. /*
  23. * Extents support for EXT4
  24. *
  25. * TODO:
  26. * - ext4*_error() should be used in some situations
  27. * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
  28. * - smart tree reduction
  29. */
  30. #include <linux/module.h>
  31. #include <linux/fs.h>
  32. #include <linux/time.h>
  33. #include <linux/ext4_jbd2.h>
  34. #include <linux/jbd.h>
  35. #include <linux/highuid.h>
  36. #include <linux/pagemap.h>
  37. #include <linux/quotaops.h>
  38. #include <linux/string.h>
  39. #include <linux/slab.h>
  40. #include <linux/falloc.h>
  41. #include <linux/ext4_fs_extents.h>
  42. #include <asm/uaccess.h>
  43. /*
  44. * ext_pblock:
  45. * combine low and high parts of physical block number into ext4_fsblk_t
  46. */
  47. static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
  48. {
  49. ext4_fsblk_t block;
  50. block = le32_to_cpu(ex->ee_start);
  51. block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
  52. return block;
  53. }
  54. /*
  55. * idx_pblock:
  56. * combine low and high parts of a leaf physical block number into ext4_fsblk_t
  57. */
  58. static ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
  59. {
  60. ext4_fsblk_t block;
  61. block = le32_to_cpu(ix->ei_leaf);
  62. block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
  63. return block;
  64. }
  65. /*
  66. * ext4_ext_store_pblock:
  67. * stores a large physical block number into an extent struct,
  68. * breaking it into parts
  69. */
  70. static void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
  71. {
  72. ex->ee_start = cpu_to_le32((unsigned long) (pb & 0xffffffff));
  73. ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
  74. }
  75. /*
  76. * ext4_idx_store_pblock:
  77. * stores a large physical block number into an index struct,
  78. * breaking it into parts
  79. */
  80. static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
  81. {
  82. ix->ei_leaf = cpu_to_le32((unsigned long) (pb & 0xffffffff));
  83. ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
  84. }
  85. static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
  86. {
  87. int err;
  88. if (handle->h_buffer_credits > needed)
  89. return handle;
  90. if (!ext4_journal_extend(handle, needed))
  91. return handle;
  92. err = ext4_journal_restart(handle, needed);
  93. return handle;
  94. }
  95. /*
  96. * could return:
  97. * - EROFS
  98. * - ENOMEM
  99. */
  100. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  101. struct ext4_ext_path *path)
  102. {
  103. if (path->p_bh) {
  104. /* path points to block */
  105. return ext4_journal_get_write_access(handle, path->p_bh);
  106. }
  107. /* path points to leaf/index in inode body */
  108. /* we use in-core data, no need to protect them */
  109. return 0;
  110. }
  111. /*
  112. * could return:
  113. * - EROFS
  114. * - ENOMEM
  115. * - EIO
  116. */
  117. static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
  118. struct ext4_ext_path *path)
  119. {
  120. int err;
  121. if (path->p_bh) {
  122. /* path points to block */
  123. err = ext4_journal_dirty_metadata(handle, path->p_bh);
  124. } else {
  125. /* path points to leaf/index in inode body */
  126. err = ext4_mark_inode_dirty(handle, inode);
  127. }
  128. return err;
  129. }
  130. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  131. struct ext4_ext_path *path,
  132. ext4_fsblk_t block)
  133. {
  134. struct ext4_inode_info *ei = EXT4_I(inode);
  135. ext4_fsblk_t bg_start;
  136. ext4_grpblk_t colour;
  137. int depth;
  138. if (path) {
  139. struct ext4_extent *ex;
  140. depth = path->p_depth;
  141. /* try to predict block placement */
  142. ex = path[depth].p_ext;
  143. if (ex)
  144. return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
  145. /* it looks like index is empty;
  146. * try to find starting block from index itself */
  147. if (path[depth].p_bh)
  148. return path[depth].p_bh->b_blocknr;
  149. }
  150. /* OK. use inode's group */
  151. bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
  152. le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
  153. colour = (current->pid % 16) *
  154. (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  155. return bg_start + colour + block;
  156. }
  157. static ext4_fsblk_t
  158. ext4_ext_new_block(handle_t *handle, struct inode *inode,
  159. struct ext4_ext_path *path,
  160. struct ext4_extent *ex, int *err)
  161. {
  162. ext4_fsblk_t goal, newblock;
  163. goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
  164. newblock = ext4_new_block(handle, inode, goal, err);
  165. return newblock;
  166. }
  167. static int ext4_ext_space_block(struct inode *inode)
  168. {
  169. int size;
  170. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  171. / sizeof(struct ext4_extent);
  172. #ifdef AGGRESSIVE_TEST
  173. if (size > 6)
  174. size = 6;
  175. #endif
  176. return size;
  177. }
  178. static int ext4_ext_space_block_idx(struct inode *inode)
  179. {
  180. int size;
  181. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  182. / sizeof(struct ext4_extent_idx);
  183. #ifdef AGGRESSIVE_TEST
  184. if (size > 5)
  185. size = 5;
  186. #endif
  187. return size;
  188. }
  189. static int ext4_ext_space_root(struct inode *inode)
  190. {
  191. int size;
  192. size = sizeof(EXT4_I(inode)->i_data);
  193. size -= sizeof(struct ext4_extent_header);
  194. size /= sizeof(struct ext4_extent);
  195. #ifdef AGGRESSIVE_TEST
  196. if (size > 3)
  197. size = 3;
  198. #endif
  199. return size;
  200. }
  201. static int ext4_ext_space_root_idx(struct inode *inode)
  202. {
  203. int size;
  204. size = sizeof(EXT4_I(inode)->i_data);
  205. size -= sizeof(struct ext4_extent_header);
  206. size /= sizeof(struct ext4_extent_idx);
  207. #ifdef AGGRESSIVE_TEST
  208. if (size > 4)
  209. size = 4;
  210. #endif
  211. return size;
  212. }
  213. static int
  214. ext4_ext_max_entries(struct inode *inode, int depth)
  215. {
  216. int max;
  217. if (depth == ext_depth(inode)) {
  218. if (depth == 0)
  219. max = ext4_ext_space_root(inode);
  220. else
  221. max = ext4_ext_space_root_idx(inode);
  222. } else {
  223. if (depth == 0)
  224. max = ext4_ext_space_block(inode);
  225. else
  226. max = ext4_ext_space_block_idx(inode);
  227. }
  228. return max;
  229. }
  230. static int __ext4_ext_check_header(const char *function, struct inode *inode,
  231. struct ext4_extent_header *eh,
  232. int depth)
  233. {
  234. const char *error_msg;
  235. int max = 0;
  236. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  237. error_msg = "invalid magic";
  238. goto corrupted;
  239. }
  240. if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
  241. error_msg = "unexpected eh_depth";
  242. goto corrupted;
  243. }
  244. if (unlikely(eh->eh_max == 0)) {
  245. error_msg = "invalid eh_max";
  246. goto corrupted;
  247. }
  248. max = ext4_ext_max_entries(inode, depth);
  249. if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
  250. error_msg = "too large eh_max";
  251. goto corrupted;
  252. }
  253. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  254. error_msg = "invalid eh_entries";
  255. goto corrupted;
  256. }
  257. return 0;
  258. corrupted:
  259. ext4_error(inode->i_sb, function,
  260. "bad header in inode #%lu: %s - magic %x, "
  261. "entries %u, max %u(%u), depth %u(%u)",
  262. inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
  263. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
  264. max, le16_to_cpu(eh->eh_depth), depth);
  265. return -EIO;
  266. }
  267. #define ext4_ext_check_header(inode, eh, depth) \
  268. __ext4_ext_check_header(__FUNCTION__, inode, eh, depth)
  269. #ifdef EXT_DEBUG
  270. static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
  271. {
  272. int k, l = path->p_depth;
  273. ext_debug("path:");
  274. for (k = 0; k <= l; k++, path++) {
  275. if (path->p_idx) {
  276. ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
  277. idx_pblock(path->p_idx));
  278. } else if (path->p_ext) {
  279. ext_debug(" %d:%d:%llu ",
  280. le32_to_cpu(path->p_ext->ee_block),
  281. ext4_ext_get_actual_len(path->p_ext),
  282. ext_pblock(path->p_ext));
  283. } else
  284. ext_debug(" []");
  285. }
  286. ext_debug("\n");
  287. }
  288. static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
  289. {
  290. int depth = ext_depth(inode);
  291. struct ext4_extent_header *eh;
  292. struct ext4_extent *ex;
  293. int i;
  294. if (!path)
  295. return;
  296. eh = path[depth].p_hdr;
  297. ex = EXT_FIRST_EXTENT(eh);
  298. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
  299. ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
  300. ext4_ext_get_actual_len(ex), ext_pblock(ex));
  301. }
  302. ext_debug("\n");
  303. }
  304. #else
  305. #define ext4_ext_show_path(inode,path)
  306. #define ext4_ext_show_leaf(inode,path)
  307. #endif
  308. static void ext4_ext_drop_refs(struct ext4_ext_path *path)
  309. {
  310. int depth = path->p_depth;
  311. int i;
  312. for (i = 0; i <= depth; i++, path++)
  313. if (path->p_bh) {
  314. brelse(path->p_bh);
  315. path->p_bh = NULL;
  316. }
  317. }
  318. /*
  319. * ext4_ext_binsearch_idx:
  320. * binary search for the closest index of the given block
  321. * the header must be checked before calling this
  322. */
  323. static void
  324. ext4_ext_binsearch_idx(struct inode *inode, struct ext4_ext_path *path, int block)
  325. {
  326. struct ext4_extent_header *eh = path->p_hdr;
  327. struct ext4_extent_idx *r, *l, *m;
  328. ext_debug("binsearch for %d(idx): ", block);
  329. l = EXT_FIRST_INDEX(eh) + 1;
  330. r = EXT_FIRST_INDEX(eh) + le16_to_cpu(eh->eh_entries) - 1;
  331. while (l <= r) {
  332. m = l + (r - l) / 2;
  333. if (block < le32_to_cpu(m->ei_block))
  334. r = m - 1;
  335. else
  336. l = m + 1;
  337. ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ei_block,
  338. m, m->ei_block, r, r->ei_block);
  339. }
  340. path->p_idx = l - 1;
  341. ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
  342. idx_block(path->p_idx));
  343. #ifdef CHECK_BINSEARCH
  344. {
  345. struct ext4_extent_idx *chix, *ix;
  346. int k;
  347. chix = ix = EXT_FIRST_INDEX(eh);
  348. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
  349. if (k != 0 &&
  350. le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
  351. printk("k=%d, ix=0x%p, first=0x%p\n", k,
  352. ix, EXT_FIRST_INDEX(eh));
  353. printk("%u <= %u\n",
  354. le32_to_cpu(ix->ei_block),
  355. le32_to_cpu(ix[-1].ei_block));
  356. }
  357. BUG_ON(k && le32_to_cpu(ix->ei_block)
  358. <= le32_to_cpu(ix[-1].ei_block));
  359. if (block < le32_to_cpu(ix->ei_block))
  360. break;
  361. chix = ix;
  362. }
  363. BUG_ON(chix != path->p_idx);
  364. }
  365. #endif
  366. }
  367. /*
  368. * ext4_ext_binsearch:
  369. * binary search for closest extent of the given block
  370. * the header must be checked before calling this
  371. */
  372. static void
  373. ext4_ext_binsearch(struct inode *inode, struct ext4_ext_path *path, int block)
  374. {
  375. struct ext4_extent_header *eh = path->p_hdr;
  376. struct ext4_extent *r, *l, *m;
  377. if (eh->eh_entries == 0) {
  378. /*
  379. * this leaf is empty:
  380. * we get such a leaf in split/add case
  381. */
  382. return;
  383. }
  384. ext_debug("binsearch for %d: ", block);
  385. l = EXT_FIRST_EXTENT(eh) + 1;
  386. r = EXT_FIRST_EXTENT(eh) + le16_to_cpu(eh->eh_entries) - 1;
  387. while (l <= r) {
  388. m = l + (r - l) / 2;
  389. if (block < le32_to_cpu(m->ee_block))
  390. r = m - 1;
  391. else
  392. l = m + 1;
  393. ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ee_block,
  394. m, m->ee_block, r, r->ee_block);
  395. }
  396. path->p_ext = l - 1;
  397. ext_debug(" -> %d:%llu:%d ",
  398. le32_to_cpu(path->p_ext->ee_block),
  399. ext_pblock(path->p_ext),
  400. ext4_ext_get_actual_len(path->p_ext));
  401. #ifdef CHECK_BINSEARCH
  402. {
  403. struct ext4_extent *chex, *ex;
  404. int k;
  405. chex = ex = EXT_FIRST_EXTENT(eh);
  406. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
  407. BUG_ON(k && le32_to_cpu(ex->ee_block)
  408. <= le32_to_cpu(ex[-1].ee_block));
  409. if (block < le32_to_cpu(ex->ee_block))
  410. break;
  411. chex = ex;
  412. }
  413. BUG_ON(chex != path->p_ext);
  414. }
  415. #endif
  416. }
  417. int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
  418. {
  419. struct ext4_extent_header *eh;
  420. eh = ext_inode_hdr(inode);
  421. eh->eh_depth = 0;
  422. eh->eh_entries = 0;
  423. eh->eh_magic = EXT4_EXT_MAGIC;
  424. eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
  425. ext4_mark_inode_dirty(handle, inode);
  426. ext4_ext_invalidate_cache(inode);
  427. return 0;
  428. }
  429. struct ext4_ext_path *
  430. ext4_ext_find_extent(struct inode *inode, int block, struct ext4_ext_path *path)
  431. {
  432. struct ext4_extent_header *eh;
  433. struct buffer_head *bh;
  434. short int depth, i, ppos = 0, alloc = 0;
  435. eh = ext_inode_hdr(inode);
  436. depth = ext_depth(inode);
  437. if (ext4_ext_check_header(inode, eh, depth))
  438. return ERR_PTR(-EIO);
  439. /* account possible depth increase */
  440. if (!path) {
  441. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
  442. GFP_NOFS);
  443. if (!path)
  444. return ERR_PTR(-ENOMEM);
  445. alloc = 1;
  446. }
  447. path[0].p_hdr = eh;
  448. i = depth;
  449. /* walk through the tree */
  450. while (i) {
  451. ext_debug("depth %d: num %d, max %d\n",
  452. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  453. ext4_ext_binsearch_idx(inode, path + ppos, block);
  454. path[ppos].p_block = idx_pblock(path[ppos].p_idx);
  455. path[ppos].p_depth = i;
  456. path[ppos].p_ext = NULL;
  457. bh = sb_bread(inode->i_sb, path[ppos].p_block);
  458. if (!bh)
  459. goto err;
  460. eh = ext_block_hdr(bh);
  461. ppos++;
  462. BUG_ON(ppos > depth);
  463. path[ppos].p_bh = bh;
  464. path[ppos].p_hdr = eh;
  465. i--;
  466. if (ext4_ext_check_header(inode, eh, i))
  467. goto err;
  468. }
  469. path[ppos].p_depth = i;
  470. path[ppos].p_hdr = eh;
  471. path[ppos].p_ext = NULL;
  472. path[ppos].p_idx = NULL;
  473. /* find extent */
  474. ext4_ext_binsearch(inode, path + ppos, block);
  475. ext4_ext_show_path(inode, path);
  476. return path;
  477. err:
  478. ext4_ext_drop_refs(path);
  479. if (alloc)
  480. kfree(path);
  481. return ERR_PTR(-EIO);
  482. }
  483. /*
  484. * ext4_ext_insert_index:
  485. * insert new index [@logical;@ptr] into the block at @curp;
  486. * check where to insert: before @curp or after @curp
  487. */
  488. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  489. struct ext4_ext_path *curp,
  490. int logical, ext4_fsblk_t ptr)
  491. {
  492. struct ext4_extent_idx *ix;
  493. int len, err;
  494. err = ext4_ext_get_access(handle, inode, curp);
  495. if (err)
  496. return err;
  497. BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
  498. len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
  499. if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
  500. /* insert after */
  501. if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
  502. len = (len - 1) * sizeof(struct ext4_extent_idx);
  503. len = len < 0 ? 0 : len;
  504. ext_debug("insert new index %d after: %d. "
  505. "move %d from 0x%p to 0x%p\n",
  506. logical, ptr, len,
  507. (curp->p_idx + 1), (curp->p_idx + 2));
  508. memmove(curp->p_idx + 2, curp->p_idx + 1, len);
  509. }
  510. ix = curp->p_idx + 1;
  511. } else {
  512. /* insert before */
  513. len = len * sizeof(struct ext4_extent_idx);
  514. len = len < 0 ? 0 : len;
  515. ext_debug("insert new index %d before: %d. "
  516. "move %d from 0x%p to 0x%p\n",
  517. logical, ptr, len,
  518. curp->p_idx, (curp->p_idx + 1));
  519. memmove(curp->p_idx + 1, curp->p_idx, len);
  520. ix = curp->p_idx;
  521. }
  522. ix->ei_block = cpu_to_le32(logical);
  523. ext4_idx_store_pblock(ix, ptr);
  524. curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
  525. BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
  526. > le16_to_cpu(curp->p_hdr->eh_max));
  527. BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
  528. err = ext4_ext_dirty(handle, inode, curp);
  529. ext4_std_error(inode->i_sb, err);
  530. return err;
  531. }
  532. /*
  533. * ext4_ext_split:
  534. * inserts new subtree into the path, using free index entry
  535. * at depth @at:
  536. * - allocates all needed blocks (new leaf and all intermediate index blocks)
  537. * - makes decision where to split
  538. * - moves remaining extents and index entries (right to the split point)
  539. * into the newly allocated blocks
  540. * - initializes subtree
  541. */
  542. static int ext4_ext_split(handle_t *handle, struct inode *inode,
  543. struct ext4_ext_path *path,
  544. struct ext4_extent *newext, int at)
  545. {
  546. struct buffer_head *bh = NULL;
  547. int depth = ext_depth(inode);
  548. struct ext4_extent_header *neh;
  549. struct ext4_extent_idx *fidx;
  550. struct ext4_extent *ex;
  551. int i = at, k, m, a;
  552. ext4_fsblk_t newblock, oldblock;
  553. __le32 border;
  554. ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
  555. int err = 0;
  556. /* make decision: where to split? */
  557. /* FIXME: now decision is simplest: at current extent */
  558. /* if current leaf will be split, then we should use
  559. * border from split point */
  560. BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
  561. if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
  562. border = path[depth].p_ext[1].ee_block;
  563. ext_debug("leaf will be split."
  564. " next leaf starts at %d\n",
  565. le32_to_cpu(border));
  566. } else {
  567. border = newext->ee_block;
  568. ext_debug("leaf will be added."
  569. " next leaf starts at %d\n",
  570. le32_to_cpu(border));
  571. }
  572. /*
  573. * If error occurs, then we break processing
  574. * and mark filesystem read-only. index won't
  575. * be inserted and tree will be in consistent
  576. * state. Next mount will repair buffers too.
  577. */
  578. /*
  579. * Get array to track all allocated blocks.
  580. * We need this to handle errors and free blocks
  581. * upon them.
  582. */
  583. ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
  584. if (!ablocks)
  585. return -ENOMEM;
  586. /* allocate all needed blocks */
  587. ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
  588. for (a = 0; a < depth - at; a++) {
  589. newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
  590. if (newblock == 0)
  591. goto cleanup;
  592. ablocks[a] = newblock;
  593. }
  594. /* initialize new leaf */
  595. newblock = ablocks[--a];
  596. BUG_ON(newblock == 0);
  597. bh = sb_getblk(inode->i_sb, newblock);
  598. if (!bh) {
  599. err = -EIO;
  600. goto cleanup;
  601. }
  602. lock_buffer(bh);
  603. err = ext4_journal_get_create_access(handle, bh);
  604. if (err)
  605. goto cleanup;
  606. neh = ext_block_hdr(bh);
  607. neh->eh_entries = 0;
  608. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  609. neh->eh_magic = EXT4_EXT_MAGIC;
  610. neh->eh_depth = 0;
  611. ex = EXT_FIRST_EXTENT(neh);
  612. /* move remainder of path[depth] to the new leaf */
  613. BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
  614. /* start copy from next extent */
  615. /* TODO: we could do it by single memmove */
  616. m = 0;
  617. path[depth].p_ext++;
  618. while (path[depth].p_ext <=
  619. EXT_MAX_EXTENT(path[depth].p_hdr)) {
  620. ext_debug("move %d:%llu:%d in new leaf %llu\n",
  621. le32_to_cpu(path[depth].p_ext->ee_block),
  622. ext_pblock(path[depth].p_ext),
  623. ext4_ext_get_actual_len(path[depth].p_ext),
  624. newblock);
  625. /*memmove(ex++, path[depth].p_ext++,
  626. sizeof(struct ext4_extent));
  627. neh->eh_entries++;*/
  628. path[depth].p_ext++;
  629. m++;
  630. }
  631. if (m) {
  632. memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
  633. neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
  634. }
  635. set_buffer_uptodate(bh);
  636. unlock_buffer(bh);
  637. err = ext4_journal_dirty_metadata(handle, bh);
  638. if (err)
  639. goto cleanup;
  640. brelse(bh);
  641. bh = NULL;
  642. /* correct old leaf */
  643. if (m) {
  644. err = ext4_ext_get_access(handle, inode, path + depth);
  645. if (err)
  646. goto cleanup;
  647. path[depth].p_hdr->eh_entries =
  648. cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
  649. err = ext4_ext_dirty(handle, inode, path + depth);
  650. if (err)
  651. goto cleanup;
  652. }
  653. /* create intermediate indexes */
  654. k = depth - at - 1;
  655. BUG_ON(k < 0);
  656. if (k)
  657. ext_debug("create %d intermediate indices\n", k);
  658. /* insert new index into current index block */
  659. /* current depth stored in i var */
  660. i = depth - 1;
  661. while (k--) {
  662. oldblock = newblock;
  663. newblock = ablocks[--a];
  664. bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock);
  665. if (!bh) {
  666. err = -EIO;
  667. goto cleanup;
  668. }
  669. lock_buffer(bh);
  670. err = ext4_journal_get_create_access(handle, bh);
  671. if (err)
  672. goto cleanup;
  673. neh = ext_block_hdr(bh);
  674. neh->eh_entries = cpu_to_le16(1);
  675. neh->eh_magic = EXT4_EXT_MAGIC;
  676. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  677. neh->eh_depth = cpu_to_le16(depth - i);
  678. fidx = EXT_FIRST_INDEX(neh);
  679. fidx->ei_block = border;
  680. ext4_idx_store_pblock(fidx, oldblock);
  681. ext_debug("int.index at %d (block %llu): %lu -> %llu\n", i,
  682. newblock, (unsigned long) le32_to_cpu(border),
  683. oldblock);
  684. /* copy indexes */
  685. m = 0;
  686. path[i].p_idx++;
  687. ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
  688. EXT_MAX_INDEX(path[i].p_hdr));
  689. BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
  690. EXT_LAST_INDEX(path[i].p_hdr));
  691. while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
  692. ext_debug("%d: move %d:%d in new index %llu\n", i,
  693. le32_to_cpu(path[i].p_idx->ei_block),
  694. idx_pblock(path[i].p_idx),
  695. newblock);
  696. /*memmove(++fidx, path[i].p_idx++,
  697. sizeof(struct ext4_extent_idx));
  698. neh->eh_entries++;
  699. BUG_ON(neh->eh_entries > neh->eh_max);*/
  700. path[i].p_idx++;
  701. m++;
  702. }
  703. if (m) {
  704. memmove(++fidx, path[i].p_idx - m,
  705. sizeof(struct ext4_extent_idx) * m);
  706. neh->eh_entries =
  707. cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
  708. }
  709. set_buffer_uptodate(bh);
  710. unlock_buffer(bh);
  711. err = ext4_journal_dirty_metadata(handle, bh);
  712. if (err)
  713. goto cleanup;
  714. brelse(bh);
  715. bh = NULL;
  716. /* correct old index */
  717. if (m) {
  718. err = ext4_ext_get_access(handle, inode, path + i);
  719. if (err)
  720. goto cleanup;
  721. path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
  722. err = ext4_ext_dirty(handle, inode, path + i);
  723. if (err)
  724. goto cleanup;
  725. }
  726. i--;
  727. }
  728. /* insert new index */
  729. err = ext4_ext_insert_index(handle, inode, path + at,
  730. le32_to_cpu(border), newblock);
  731. cleanup:
  732. if (bh) {
  733. if (buffer_locked(bh))
  734. unlock_buffer(bh);
  735. brelse(bh);
  736. }
  737. if (err) {
  738. /* free all allocated blocks in error case */
  739. for (i = 0; i < depth; i++) {
  740. if (!ablocks[i])
  741. continue;
  742. ext4_free_blocks(handle, inode, ablocks[i], 1);
  743. }
  744. }
  745. kfree(ablocks);
  746. return err;
  747. }
  748. /*
  749. * ext4_ext_grow_indepth:
  750. * implements tree growing procedure:
  751. * - allocates new block
  752. * - moves top-level data (index block or leaf) into the new block
  753. * - initializes new top-level, creating index that points to the
  754. * just created block
  755. */
  756. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  757. struct ext4_ext_path *path,
  758. struct ext4_extent *newext)
  759. {
  760. struct ext4_ext_path *curp = path;
  761. struct ext4_extent_header *neh;
  762. struct ext4_extent_idx *fidx;
  763. struct buffer_head *bh;
  764. ext4_fsblk_t newblock;
  765. int err = 0;
  766. newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
  767. if (newblock == 0)
  768. return err;
  769. bh = sb_getblk(inode->i_sb, newblock);
  770. if (!bh) {
  771. err = -EIO;
  772. ext4_std_error(inode->i_sb, err);
  773. return err;
  774. }
  775. lock_buffer(bh);
  776. err = ext4_journal_get_create_access(handle, bh);
  777. if (err) {
  778. unlock_buffer(bh);
  779. goto out;
  780. }
  781. /* move top-level index/leaf into new block */
  782. memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
  783. /* set size of new block */
  784. neh = ext_block_hdr(bh);
  785. /* old root could have indexes or leaves
  786. * so calculate e_max right way */
  787. if (ext_depth(inode))
  788. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  789. else
  790. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  791. neh->eh_magic = EXT4_EXT_MAGIC;
  792. set_buffer_uptodate(bh);
  793. unlock_buffer(bh);
  794. err = ext4_journal_dirty_metadata(handle, bh);
  795. if (err)
  796. goto out;
  797. /* create index in new top-level index: num,max,pointer */
  798. err = ext4_ext_get_access(handle, inode, curp);
  799. if (err)
  800. goto out;
  801. curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
  802. curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
  803. curp->p_hdr->eh_entries = cpu_to_le16(1);
  804. curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
  805. /* FIXME: it works, but actually path[0] can be index */
  806. curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
  807. ext4_idx_store_pblock(curp->p_idx, newblock);
  808. neh = ext_inode_hdr(inode);
  809. fidx = EXT_FIRST_INDEX(neh);
  810. ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
  811. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  812. le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
  813. neh->eh_depth = cpu_to_le16(path->p_depth + 1);
  814. err = ext4_ext_dirty(handle, inode, curp);
  815. out:
  816. brelse(bh);
  817. return err;
  818. }
  819. /*
  820. * ext4_ext_create_new_leaf:
  821. * finds empty index and adds new leaf.
  822. * if no free index is found, then it requests in-depth growing.
  823. */
  824. static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  825. struct ext4_ext_path *path,
  826. struct ext4_extent *newext)
  827. {
  828. struct ext4_ext_path *curp;
  829. int depth, i, err = 0;
  830. repeat:
  831. i = depth = ext_depth(inode);
  832. /* walk up to the tree and look for free index entry */
  833. curp = path + depth;
  834. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  835. i--;
  836. curp--;
  837. }
  838. /* we use already allocated block for index block,
  839. * so subsequent data blocks should be contiguous */
  840. if (EXT_HAS_FREE_INDEX(curp)) {
  841. /* if we found index with free entry, then use that
  842. * entry: create all needed subtree and add new leaf */
  843. err = ext4_ext_split(handle, inode, path, newext, i);
  844. /* refill path */
  845. ext4_ext_drop_refs(path);
  846. path = ext4_ext_find_extent(inode,
  847. le32_to_cpu(newext->ee_block),
  848. path);
  849. if (IS_ERR(path))
  850. err = PTR_ERR(path);
  851. } else {
  852. /* tree is full, time to grow in depth */
  853. err = ext4_ext_grow_indepth(handle, inode, path, newext);
  854. if (err)
  855. goto out;
  856. /* refill path */
  857. ext4_ext_drop_refs(path);
  858. path = ext4_ext_find_extent(inode,
  859. le32_to_cpu(newext->ee_block),
  860. path);
  861. if (IS_ERR(path)) {
  862. err = PTR_ERR(path);
  863. goto out;
  864. }
  865. /*
  866. * only first (depth 0 -> 1) produces free space;
  867. * in all other cases we have to split the grown tree
  868. */
  869. depth = ext_depth(inode);
  870. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  871. /* now we need to split */
  872. goto repeat;
  873. }
  874. }
  875. out:
  876. return err;
  877. }
  878. /*
  879. * ext4_ext_next_allocated_block:
  880. * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
  881. * NOTE: it considers block number from index entry as
  882. * allocated block. Thus, index entries have to be consistent
  883. * with leaves.
  884. */
  885. static unsigned long
  886. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  887. {
  888. int depth;
  889. BUG_ON(path == NULL);
  890. depth = path->p_depth;
  891. if (depth == 0 && path->p_ext == NULL)
  892. return EXT_MAX_BLOCK;
  893. while (depth >= 0) {
  894. if (depth == path->p_depth) {
  895. /* leaf */
  896. if (path[depth].p_ext !=
  897. EXT_LAST_EXTENT(path[depth].p_hdr))
  898. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  899. } else {
  900. /* index */
  901. if (path[depth].p_idx !=
  902. EXT_LAST_INDEX(path[depth].p_hdr))
  903. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  904. }
  905. depth--;
  906. }
  907. return EXT_MAX_BLOCK;
  908. }
  909. /*
  910. * ext4_ext_next_leaf_block:
  911. * returns first allocated block from next leaf or EXT_MAX_BLOCK
  912. */
  913. static unsigned ext4_ext_next_leaf_block(struct inode *inode,
  914. struct ext4_ext_path *path)
  915. {
  916. int depth;
  917. BUG_ON(path == NULL);
  918. depth = path->p_depth;
  919. /* zero-tree has no leaf blocks at all */
  920. if (depth == 0)
  921. return EXT_MAX_BLOCK;
  922. /* go to index block */
  923. depth--;
  924. while (depth >= 0) {
  925. if (path[depth].p_idx !=
  926. EXT_LAST_INDEX(path[depth].p_hdr))
  927. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  928. depth--;
  929. }
  930. return EXT_MAX_BLOCK;
  931. }
  932. /*
  933. * ext4_ext_correct_indexes:
  934. * if leaf gets modified and modified extent is first in the leaf,
  935. * then we have to correct all indexes above.
  936. * TODO: do we need to correct tree in all cases?
  937. */
  938. int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  939. struct ext4_ext_path *path)
  940. {
  941. struct ext4_extent_header *eh;
  942. int depth = ext_depth(inode);
  943. struct ext4_extent *ex;
  944. __le32 border;
  945. int k, err = 0;
  946. eh = path[depth].p_hdr;
  947. ex = path[depth].p_ext;
  948. BUG_ON(ex == NULL);
  949. BUG_ON(eh == NULL);
  950. if (depth == 0) {
  951. /* there is no tree at all */
  952. return 0;
  953. }
  954. if (ex != EXT_FIRST_EXTENT(eh)) {
  955. /* we correct tree if first leaf got modified only */
  956. return 0;
  957. }
  958. /*
  959. * TODO: we need correction if border is smaller than current one
  960. */
  961. k = depth - 1;
  962. border = path[depth].p_ext->ee_block;
  963. err = ext4_ext_get_access(handle, inode, path + k);
  964. if (err)
  965. return err;
  966. path[k].p_idx->ei_block = border;
  967. err = ext4_ext_dirty(handle, inode, path + k);
  968. if (err)
  969. return err;
  970. while (k--) {
  971. /* change all left-side indexes */
  972. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  973. break;
  974. err = ext4_ext_get_access(handle, inode, path + k);
  975. if (err)
  976. break;
  977. path[k].p_idx->ei_block = border;
  978. err = ext4_ext_dirty(handle, inode, path + k);
  979. if (err)
  980. break;
  981. }
  982. return err;
  983. }
  984. static int
  985. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  986. struct ext4_extent *ex2)
  987. {
  988. unsigned short ext1_ee_len, ext2_ee_len, max_len;
  989. /*
  990. * Make sure that either both extents are uninitialized, or
  991. * both are _not_.
  992. */
  993. if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
  994. return 0;
  995. if (ext4_ext_is_uninitialized(ex1))
  996. max_len = EXT_UNINIT_MAX_LEN;
  997. else
  998. max_len = EXT_INIT_MAX_LEN;
  999. ext1_ee_len = ext4_ext_get_actual_len(ex1);
  1000. ext2_ee_len = ext4_ext_get_actual_len(ex2);
  1001. if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
  1002. le32_to_cpu(ex2->ee_block))
  1003. return 0;
  1004. /*
  1005. * To allow future support for preallocated extents to be added
  1006. * as an RO_COMPAT feature, refuse to merge to extents if
  1007. * this can result in the top bit of ee_len being set.
  1008. */
  1009. if (ext1_ee_len + ext2_ee_len > max_len)
  1010. return 0;
  1011. #ifdef AGGRESSIVE_TEST
  1012. if (le16_to_cpu(ex1->ee_len) >= 4)
  1013. return 0;
  1014. #endif
  1015. if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
  1016. return 1;
  1017. return 0;
  1018. }
  1019. /*
  1020. * This function tries to merge the "ex" extent to the next extent in the tree.
  1021. * It always tries to merge towards right. If you want to merge towards
  1022. * left, pass "ex - 1" as argument instead of "ex".
  1023. * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
  1024. * 1 if they got merged.
  1025. */
  1026. int ext4_ext_try_to_merge(struct inode *inode,
  1027. struct ext4_ext_path *path,
  1028. struct ext4_extent *ex)
  1029. {
  1030. struct ext4_extent_header *eh;
  1031. unsigned int depth, len;
  1032. int merge_done = 0;
  1033. int uninitialized = 0;
  1034. depth = ext_depth(inode);
  1035. BUG_ON(path[depth].p_hdr == NULL);
  1036. eh = path[depth].p_hdr;
  1037. while (ex < EXT_LAST_EXTENT(eh)) {
  1038. if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
  1039. break;
  1040. /* merge with next extent! */
  1041. if (ext4_ext_is_uninitialized(ex))
  1042. uninitialized = 1;
  1043. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1044. + ext4_ext_get_actual_len(ex + 1));
  1045. if (uninitialized)
  1046. ext4_ext_mark_uninitialized(ex);
  1047. if (ex + 1 < EXT_LAST_EXTENT(eh)) {
  1048. len = (EXT_LAST_EXTENT(eh) - ex - 1)
  1049. * sizeof(struct ext4_extent);
  1050. memmove(ex + 1, ex + 2, len);
  1051. }
  1052. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries) - 1);
  1053. merge_done = 1;
  1054. WARN_ON(eh->eh_entries == 0);
  1055. if (!eh->eh_entries)
  1056. ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
  1057. "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
  1058. }
  1059. return merge_done;
  1060. }
  1061. /*
  1062. * check if a portion of the "newext" extent overlaps with an
  1063. * existing extent.
  1064. *
  1065. * If there is an overlap discovered, it updates the length of the newext
  1066. * such that there will be no overlap, and then returns 1.
  1067. * If there is no overlap found, it returns 0.
  1068. */
  1069. unsigned int ext4_ext_check_overlap(struct inode *inode,
  1070. struct ext4_extent *newext,
  1071. struct ext4_ext_path *path)
  1072. {
  1073. unsigned long b1, b2;
  1074. unsigned int depth, len1;
  1075. unsigned int ret = 0;
  1076. b1 = le32_to_cpu(newext->ee_block);
  1077. len1 = ext4_ext_get_actual_len(newext);
  1078. depth = ext_depth(inode);
  1079. if (!path[depth].p_ext)
  1080. goto out;
  1081. b2 = le32_to_cpu(path[depth].p_ext->ee_block);
  1082. /*
  1083. * get the next allocated block if the extent in the path
  1084. * is before the requested block(s)
  1085. */
  1086. if (b2 < b1) {
  1087. b2 = ext4_ext_next_allocated_block(path);
  1088. if (b2 == EXT_MAX_BLOCK)
  1089. goto out;
  1090. }
  1091. /* check for wrap through zero */
  1092. if (b1 + len1 < b1) {
  1093. len1 = EXT_MAX_BLOCK - b1;
  1094. newext->ee_len = cpu_to_le16(len1);
  1095. ret = 1;
  1096. }
  1097. /* check for overlap */
  1098. if (b1 + len1 > b2) {
  1099. newext->ee_len = cpu_to_le16(b2 - b1);
  1100. ret = 1;
  1101. }
  1102. out:
  1103. return ret;
  1104. }
  1105. /*
  1106. * ext4_ext_insert_extent:
  1107. * tries to merge requsted extent into the existing extent or
  1108. * inserts requested extent as new one into the tree,
  1109. * creating new leaf in the no-space case.
  1110. */
  1111. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  1112. struct ext4_ext_path *path,
  1113. struct ext4_extent *newext)
  1114. {
  1115. struct ext4_extent_header * eh;
  1116. struct ext4_extent *ex, *fex;
  1117. struct ext4_extent *nearex; /* nearest extent */
  1118. struct ext4_ext_path *npath = NULL;
  1119. int depth, len, err, next;
  1120. unsigned uninitialized = 0;
  1121. BUG_ON(ext4_ext_get_actual_len(newext) == 0);
  1122. depth = ext_depth(inode);
  1123. ex = path[depth].p_ext;
  1124. BUG_ON(path[depth].p_hdr == NULL);
  1125. /* try to insert block into found extent and return */
  1126. if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
  1127. ext_debug("append %d block to %d:%d (from %llu)\n",
  1128. ext4_ext_get_actual_len(newext),
  1129. le32_to_cpu(ex->ee_block),
  1130. ext4_ext_get_actual_len(ex), ext_pblock(ex));
  1131. err = ext4_ext_get_access(handle, inode, path + depth);
  1132. if (err)
  1133. return err;
  1134. /*
  1135. * ext4_can_extents_be_merged should have checked that either
  1136. * both extents are uninitialized, or both aren't. Thus we
  1137. * need to check only one of them here.
  1138. */
  1139. if (ext4_ext_is_uninitialized(ex))
  1140. uninitialized = 1;
  1141. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1142. + ext4_ext_get_actual_len(newext));
  1143. if (uninitialized)
  1144. ext4_ext_mark_uninitialized(ex);
  1145. eh = path[depth].p_hdr;
  1146. nearex = ex;
  1147. goto merge;
  1148. }
  1149. repeat:
  1150. depth = ext_depth(inode);
  1151. eh = path[depth].p_hdr;
  1152. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1153. goto has_space;
  1154. /* probably next leaf has space for us? */
  1155. fex = EXT_LAST_EXTENT(eh);
  1156. next = ext4_ext_next_leaf_block(inode, path);
  1157. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
  1158. && next != EXT_MAX_BLOCK) {
  1159. ext_debug("next leaf block - %d\n", next);
  1160. BUG_ON(npath != NULL);
  1161. npath = ext4_ext_find_extent(inode, next, NULL);
  1162. if (IS_ERR(npath))
  1163. return PTR_ERR(npath);
  1164. BUG_ON(npath->p_depth != path->p_depth);
  1165. eh = npath[depth].p_hdr;
  1166. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1167. ext_debug("next leaf isnt full(%d)\n",
  1168. le16_to_cpu(eh->eh_entries));
  1169. path = npath;
  1170. goto repeat;
  1171. }
  1172. ext_debug("next leaf has no free space(%d,%d)\n",
  1173. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1174. }
  1175. /*
  1176. * There is no free space in the found leaf.
  1177. * We're gonna add a new leaf in the tree.
  1178. */
  1179. err = ext4_ext_create_new_leaf(handle, inode, path, newext);
  1180. if (err)
  1181. goto cleanup;
  1182. depth = ext_depth(inode);
  1183. eh = path[depth].p_hdr;
  1184. has_space:
  1185. nearex = path[depth].p_ext;
  1186. err = ext4_ext_get_access(handle, inode, path + depth);
  1187. if (err)
  1188. goto cleanup;
  1189. if (!nearex) {
  1190. /* there is no extent in this leaf, create first one */
  1191. ext_debug("first extent in the leaf: %d:%llu:%d\n",
  1192. le32_to_cpu(newext->ee_block),
  1193. ext_pblock(newext),
  1194. ext4_ext_get_actual_len(newext));
  1195. path[depth].p_ext = EXT_FIRST_EXTENT(eh);
  1196. } else if (le32_to_cpu(newext->ee_block)
  1197. > le32_to_cpu(nearex->ee_block)) {
  1198. /* BUG_ON(newext->ee_block == nearex->ee_block); */
  1199. if (nearex != EXT_LAST_EXTENT(eh)) {
  1200. len = EXT_MAX_EXTENT(eh) - nearex;
  1201. len = (len - 1) * sizeof(struct ext4_extent);
  1202. len = len < 0 ? 0 : len;
  1203. ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
  1204. "move %d from 0x%p to 0x%p\n",
  1205. le32_to_cpu(newext->ee_block),
  1206. ext_pblock(newext),
  1207. ext4_ext_get_actual_len(newext),
  1208. nearex, len, nearex + 1, nearex + 2);
  1209. memmove(nearex + 2, nearex + 1, len);
  1210. }
  1211. path[depth].p_ext = nearex + 1;
  1212. } else {
  1213. BUG_ON(newext->ee_block == nearex->ee_block);
  1214. len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
  1215. len = len < 0 ? 0 : len;
  1216. ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
  1217. "move %d from 0x%p to 0x%p\n",
  1218. le32_to_cpu(newext->ee_block),
  1219. ext_pblock(newext),
  1220. ext4_ext_get_actual_len(newext),
  1221. nearex, len, nearex + 1, nearex + 2);
  1222. memmove(nearex + 1, nearex, len);
  1223. path[depth].p_ext = nearex;
  1224. }
  1225. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
  1226. nearex = path[depth].p_ext;
  1227. nearex->ee_block = newext->ee_block;
  1228. nearex->ee_start = newext->ee_start;
  1229. nearex->ee_start_hi = newext->ee_start_hi;
  1230. nearex->ee_len = newext->ee_len;
  1231. merge:
  1232. /* try to merge extents to the right */
  1233. ext4_ext_try_to_merge(inode, path, nearex);
  1234. /* try to merge extents to the left */
  1235. /* time to correct all indexes above */
  1236. err = ext4_ext_correct_indexes(handle, inode, path);
  1237. if (err)
  1238. goto cleanup;
  1239. err = ext4_ext_dirty(handle, inode, path + depth);
  1240. cleanup:
  1241. if (npath) {
  1242. ext4_ext_drop_refs(npath);
  1243. kfree(npath);
  1244. }
  1245. ext4_ext_tree_changed(inode);
  1246. ext4_ext_invalidate_cache(inode);
  1247. return err;
  1248. }
  1249. int ext4_ext_walk_space(struct inode *inode, unsigned long block,
  1250. unsigned long num, ext_prepare_callback func,
  1251. void *cbdata)
  1252. {
  1253. struct ext4_ext_path *path = NULL;
  1254. struct ext4_ext_cache cbex;
  1255. struct ext4_extent *ex;
  1256. unsigned long next, start = 0, end = 0;
  1257. unsigned long last = block + num;
  1258. int depth, exists, err = 0;
  1259. BUG_ON(func == NULL);
  1260. BUG_ON(inode == NULL);
  1261. while (block < last && block != EXT_MAX_BLOCK) {
  1262. num = last - block;
  1263. /* find extent for this block */
  1264. path = ext4_ext_find_extent(inode, block, path);
  1265. if (IS_ERR(path)) {
  1266. err = PTR_ERR(path);
  1267. path = NULL;
  1268. break;
  1269. }
  1270. depth = ext_depth(inode);
  1271. BUG_ON(path[depth].p_hdr == NULL);
  1272. ex = path[depth].p_ext;
  1273. next = ext4_ext_next_allocated_block(path);
  1274. exists = 0;
  1275. if (!ex) {
  1276. /* there is no extent yet, so try to allocate
  1277. * all requested space */
  1278. start = block;
  1279. end = block + num;
  1280. } else if (le32_to_cpu(ex->ee_block) > block) {
  1281. /* need to allocate space before found extent */
  1282. start = block;
  1283. end = le32_to_cpu(ex->ee_block);
  1284. if (block + num < end)
  1285. end = block + num;
  1286. } else if (block >= le32_to_cpu(ex->ee_block)
  1287. + ext4_ext_get_actual_len(ex)) {
  1288. /* need to allocate space after found extent */
  1289. start = block;
  1290. end = block + num;
  1291. if (end >= next)
  1292. end = next;
  1293. } else if (block >= le32_to_cpu(ex->ee_block)) {
  1294. /*
  1295. * some part of requested space is covered
  1296. * by found extent
  1297. */
  1298. start = block;
  1299. end = le32_to_cpu(ex->ee_block)
  1300. + ext4_ext_get_actual_len(ex);
  1301. if (block + num < end)
  1302. end = block + num;
  1303. exists = 1;
  1304. } else {
  1305. BUG();
  1306. }
  1307. BUG_ON(end <= start);
  1308. if (!exists) {
  1309. cbex.ec_block = start;
  1310. cbex.ec_len = end - start;
  1311. cbex.ec_start = 0;
  1312. cbex.ec_type = EXT4_EXT_CACHE_GAP;
  1313. } else {
  1314. cbex.ec_block = le32_to_cpu(ex->ee_block);
  1315. cbex.ec_len = ext4_ext_get_actual_len(ex);
  1316. cbex.ec_start = ext_pblock(ex);
  1317. cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
  1318. }
  1319. BUG_ON(cbex.ec_len == 0);
  1320. err = func(inode, path, &cbex, cbdata);
  1321. ext4_ext_drop_refs(path);
  1322. if (err < 0)
  1323. break;
  1324. if (err == EXT_REPEAT)
  1325. continue;
  1326. else if (err == EXT_BREAK) {
  1327. err = 0;
  1328. break;
  1329. }
  1330. if (ext_depth(inode) != depth) {
  1331. /* depth was changed. we have to realloc path */
  1332. kfree(path);
  1333. path = NULL;
  1334. }
  1335. block = cbex.ec_block + cbex.ec_len;
  1336. }
  1337. if (path) {
  1338. ext4_ext_drop_refs(path);
  1339. kfree(path);
  1340. }
  1341. return err;
  1342. }
  1343. static void
  1344. ext4_ext_put_in_cache(struct inode *inode, __u32 block,
  1345. __u32 len, __u32 start, int type)
  1346. {
  1347. struct ext4_ext_cache *cex;
  1348. BUG_ON(len == 0);
  1349. cex = &EXT4_I(inode)->i_cached_extent;
  1350. cex->ec_type = type;
  1351. cex->ec_block = block;
  1352. cex->ec_len = len;
  1353. cex->ec_start = start;
  1354. }
  1355. /*
  1356. * ext4_ext_put_gap_in_cache:
  1357. * calculate boundaries of the gap that the requested block fits into
  1358. * and cache this gap
  1359. */
  1360. static void
  1361. ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
  1362. unsigned long block)
  1363. {
  1364. int depth = ext_depth(inode);
  1365. unsigned long lblock, len;
  1366. struct ext4_extent *ex;
  1367. ex = path[depth].p_ext;
  1368. if (ex == NULL) {
  1369. /* there is no extent yet, so gap is [0;-] */
  1370. lblock = 0;
  1371. len = EXT_MAX_BLOCK;
  1372. ext_debug("cache gap(whole file):");
  1373. } else if (block < le32_to_cpu(ex->ee_block)) {
  1374. lblock = block;
  1375. len = le32_to_cpu(ex->ee_block) - block;
  1376. ext_debug("cache gap(before): %lu [%lu:%lu]",
  1377. (unsigned long) block,
  1378. (unsigned long) le32_to_cpu(ex->ee_block),
  1379. (unsigned long) ext4_ext_get_actual_len(ex));
  1380. } else if (block >= le32_to_cpu(ex->ee_block)
  1381. + ext4_ext_get_actual_len(ex)) {
  1382. lblock = le32_to_cpu(ex->ee_block)
  1383. + ext4_ext_get_actual_len(ex);
  1384. len = ext4_ext_next_allocated_block(path);
  1385. ext_debug("cache gap(after): [%lu:%lu] %lu",
  1386. (unsigned long) le32_to_cpu(ex->ee_block),
  1387. (unsigned long) ext4_ext_get_actual_len(ex),
  1388. (unsigned long) block);
  1389. BUG_ON(len == lblock);
  1390. len = len - lblock;
  1391. } else {
  1392. lblock = len = 0;
  1393. BUG();
  1394. }
  1395. ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len);
  1396. ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
  1397. }
  1398. static int
  1399. ext4_ext_in_cache(struct inode *inode, unsigned long block,
  1400. struct ext4_extent *ex)
  1401. {
  1402. struct ext4_ext_cache *cex;
  1403. cex = &EXT4_I(inode)->i_cached_extent;
  1404. /* has cache valid data? */
  1405. if (cex->ec_type == EXT4_EXT_CACHE_NO)
  1406. return EXT4_EXT_CACHE_NO;
  1407. BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
  1408. cex->ec_type != EXT4_EXT_CACHE_EXTENT);
  1409. if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
  1410. ex->ee_block = cpu_to_le32(cex->ec_block);
  1411. ext4_ext_store_pblock(ex, cex->ec_start);
  1412. ex->ee_len = cpu_to_le16(cex->ec_len);
  1413. ext_debug("%lu cached by %lu:%lu:%llu\n",
  1414. (unsigned long) block,
  1415. (unsigned long) cex->ec_block,
  1416. (unsigned long) cex->ec_len,
  1417. cex->ec_start);
  1418. return cex->ec_type;
  1419. }
  1420. /* not in cache */
  1421. return EXT4_EXT_CACHE_NO;
  1422. }
  1423. /*
  1424. * ext4_ext_rm_idx:
  1425. * removes index from the index block.
  1426. * It's used in truncate case only, thus all requests are for
  1427. * last index in the block only.
  1428. */
  1429. int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  1430. struct ext4_ext_path *path)
  1431. {
  1432. struct buffer_head *bh;
  1433. int err;
  1434. ext4_fsblk_t leaf;
  1435. /* free index block */
  1436. path--;
  1437. leaf = idx_pblock(path->p_idx);
  1438. BUG_ON(path->p_hdr->eh_entries == 0);
  1439. err = ext4_ext_get_access(handle, inode, path);
  1440. if (err)
  1441. return err;
  1442. path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
  1443. err = ext4_ext_dirty(handle, inode, path);
  1444. if (err)
  1445. return err;
  1446. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  1447. bh = sb_find_get_block(inode->i_sb, leaf);
  1448. ext4_forget(handle, 1, inode, bh, leaf);
  1449. ext4_free_blocks(handle, inode, leaf, 1);
  1450. return err;
  1451. }
  1452. /*
  1453. * ext4_ext_calc_credits_for_insert:
  1454. * This routine returns max. credits that the extent tree can consume.
  1455. * It should be OK for low-performance paths like ->writepage()
  1456. * To allow many writing processes to fit into a single transaction,
  1457. * the caller should calculate credits under truncate_mutex and
  1458. * pass the actual path.
  1459. */
  1460. int ext4_ext_calc_credits_for_insert(struct inode *inode,
  1461. struct ext4_ext_path *path)
  1462. {
  1463. int depth, needed;
  1464. if (path) {
  1465. /* probably there is space in leaf? */
  1466. depth = ext_depth(inode);
  1467. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  1468. < le16_to_cpu(path[depth].p_hdr->eh_max))
  1469. return 1;
  1470. }
  1471. /*
  1472. * given 32-bit logical block (4294967296 blocks), max. tree
  1473. * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
  1474. * Let's also add one more level for imbalance.
  1475. */
  1476. depth = 5;
  1477. /* allocation of new data block(s) */
  1478. needed = 2;
  1479. /*
  1480. * tree can be full, so it would need to grow in depth:
  1481. * we need one credit to modify old root, credits for
  1482. * new root will be added in split accounting
  1483. */
  1484. needed += 1;
  1485. /*
  1486. * Index split can happen, we would need:
  1487. * allocate intermediate indexes (bitmap + group)
  1488. * + change two blocks at each level, but root (already included)
  1489. */
  1490. needed += (depth * 2) + (depth * 2);
  1491. /* any allocation modifies superblock */
  1492. needed += 1;
  1493. return needed;
  1494. }
  1495. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  1496. struct ext4_extent *ex,
  1497. unsigned long from, unsigned long to)
  1498. {
  1499. struct buffer_head *bh;
  1500. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  1501. int i;
  1502. #ifdef EXTENTS_STATS
  1503. {
  1504. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1505. spin_lock(&sbi->s_ext_stats_lock);
  1506. sbi->s_ext_blocks += ee_len;
  1507. sbi->s_ext_extents++;
  1508. if (ee_len < sbi->s_ext_min)
  1509. sbi->s_ext_min = ee_len;
  1510. if (ee_len > sbi->s_ext_max)
  1511. sbi->s_ext_max = ee_len;
  1512. if (ext_depth(inode) > sbi->s_depth_max)
  1513. sbi->s_depth_max = ext_depth(inode);
  1514. spin_unlock(&sbi->s_ext_stats_lock);
  1515. }
  1516. #endif
  1517. if (from >= le32_to_cpu(ex->ee_block)
  1518. && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1519. /* tail removal */
  1520. unsigned long num;
  1521. ext4_fsblk_t start;
  1522. num = le32_to_cpu(ex->ee_block) + ee_len - from;
  1523. start = ext_pblock(ex) + ee_len - num;
  1524. ext_debug("free last %lu blocks starting %llu\n", num, start);
  1525. for (i = 0; i < num; i++) {
  1526. bh = sb_find_get_block(inode->i_sb, start + i);
  1527. ext4_forget(handle, 0, inode, bh, start + i);
  1528. }
  1529. ext4_free_blocks(handle, inode, start, num);
  1530. } else if (from == le32_to_cpu(ex->ee_block)
  1531. && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1532. printk("strange request: removal %lu-%lu from %u:%u\n",
  1533. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1534. } else {
  1535. printk("strange request: removal(2) %lu-%lu from %u:%u\n",
  1536. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1537. }
  1538. return 0;
  1539. }
  1540. static int
  1541. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  1542. struct ext4_ext_path *path, unsigned long start)
  1543. {
  1544. int err = 0, correct_index = 0;
  1545. int depth = ext_depth(inode), credits;
  1546. struct ext4_extent_header *eh;
  1547. unsigned a, b, block, num;
  1548. unsigned long ex_ee_block;
  1549. unsigned short ex_ee_len;
  1550. unsigned uninitialized = 0;
  1551. struct ext4_extent *ex;
  1552. /* the header must be checked already in ext4_ext_remove_space() */
  1553. ext_debug("truncate since %lu in leaf\n", start);
  1554. if (!path[depth].p_hdr)
  1555. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  1556. eh = path[depth].p_hdr;
  1557. BUG_ON(eh == NULL);
  1558. /* find where to start removing */
  1559. ex = EXT_LAST_EXTENT(eh);
  1560. ex_ee_block = le32_to_cpu(ex->ee_block);
  1561. if (ext4_ext_is_uninitialized(ex))
  1562. uninitialized = 1;
  1563. ex_ee_len = ext4_ext_get_actual_len(ex);
  1564. while (ex >= EXT_FIRST_EXTENT(eh) &&
  1565. ex_ee_block + ex_ee_len > start) {
  1566. ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
  1567. path[depth].p_ext = ex;
  1568. a = ex_ee_block > start ? ex_ee_block : start;
  1569. b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
  1570. ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
  1571. ext_debug(" border %u:%u\n", a, b);
  1572. if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
  1573. block = 0;
  1574. num = 0;
  1575. BUG();
  1576. } else if (a != ex_ee_block) {
  1577. /* remove tail of the extent */
  1578. block = ex_ee_block;
  1579. num = a - block;
  1580. } else if (b != ex_ee_block + ex_ee_len - 1) {
  1581. /* remove head of the extent */
  1582. block = a;
  1583. num = b - a;
  1584. /* there is no "make a hole" API yet */
  1585. BUG();
  1586. } else {
  1587. /* remove whole extent: excellent! */
  1588. block = ex_ee_block;
  1589. num = 0;
  1590. BUG_ON(a != ex_ee_block);
  1591. BUG_ON(b != ex_ee_block + ex_ee_len - 1);
  1592. }
  1593. /* at present, extent can't cross block group: */
  1594. /* leaf + bitmap + group desc + sb + inode */
  1595. credits = 5;
  1596. if (ex == EXT_FIRST_EXTENT(eh)) {
  1597. correct_index = 1;
  1598. credits += (ext_depth(inode)) + 1;
  1599. }
  1600. #ifdef CONFIG_QUOTA
  1601. credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1602. #endif
  1603. handle = ext4_ext_journal_restart(handle, credits);
  1604. if (IS_ERR(handle)) {
  1605. err = PTR_ERR(handle);
  1606. goto out;
  1607. }
  1608. err = ext4_ext_get_access(handle, inode, path + depth);
  1609. if (err)
  1610. goto out;
  1611. err = ext4_remove_blocks(handle, inode, ex, a, b);
  1612. if (err)
  1613. goto out;
  1614. if (num == 0) {
  1615. /* this extent is removed; mark slot entirely unused */
  1616. ext4_ext_store_pblock(ex, 0);
  1617. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
  1618. }
  1619. ex->ee_block = cpu_to_le32(block);
  1620. ex->ee_len = cpu_to_le16(num);
  1621. /*
  1622. * Do not mark uninitialized if all the blocks in the
  1623. * extent have been removed.
  1624. */
  1625. if (uninitialized && num)
  1626. ext4_ext_mark_uninitialized(ex);
  1627. err = ext4_ext_dirty(handle, inode, path + depth);
  1628. if (err)
  1629. goto out;
  1630. ext_debug("new extent: %u:%u:%llu\n", block, num,
  1631. ext_pblock(ex));
  1632. ex--;
  1633. ex_ee_block = le32_to_cpu(ex->ee_block);
  1634. ex_ee_len = ext4_ext_get_actual_len(ex);
  1635. }
  1636. if (correct_index && eh->eh_entries)
  1637. err = ext4_ext_correct_indexes(handle, inode, path);
  1638. /* if this leaf is free, then we should
  1639. * remove it from index block above */
  1640. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  1641. err = ext4_ext_rm_idx(handle, inode, path + depth);
  1642. out:
  1643. return err;
  1644. }
  1645. /*
  1646. * ext4_ext_more_to_rm:
  1647. * returns 1 if current index has to be freed (even partial)
  1648. */
  1649. static int
  1650. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  1651. {
  1652. BUG_ON(path->p_idx == NULL);
  1653. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  1654. return 0;
  1655. /*
  1656. * if truncate on deeper level happened, it wasn't partial,
  1657. * so we have to consider current index for truncation
  1658. */
  1659. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  1660. return 0;
  1661. return 1;
  1662. }
  1663. int ext4_ext_remove_space(struct inode *inode, unsigned long start)
  1664. {
  1665. struct super_block *sb = inode->i_sb;
  1666. int depth = ext_depth(inode);
  1667. struct ext4_ext_path *path;
  1668. handle_t *handle;
  1669. int i = 0, err = 0;
  1670. ext_debug("truncate since %lu\n", start);
  1671. /* probably first extent we're gonna free will be last in block */
  1672. handle = ext4_journal_start(inode, depth + 1);
  1673. if (IS_ERR(handle))
  1674. return PTR_ERR(handle);
  1675. ext4_ext_invalidate_cache(inode);
  1676. /*
  1677. * We start scanning from right side, freeing all the blocks
  1678. * after i_size and walking into the tree depth-wise.
  1679. */
  1680. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
  1681. if (path == NULL) {
  1682. ext4_journal_stop(handle);
  1683. return -ENOMEM;
  1684. }
  1685. path[0].p_hdr = ext_inode_hdr(inode);
  1686. if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
  1687. err = -EIO;
  1688. goto out;
  1689. }
  1690. path[0].p_depth = depth;
  1691. while (i >= 0 && err == 0) {
  1692. if (i == depth) {
  1693. /* this is leaf block */
  1694. err = ext4_ext_rm_leaf(handle, inode, path, start);
  1695. /* root level has p_bh == NULL, brelse() eats this */
  1696. brelse(path[i].p_bh);
  1697. path[i].p_bh = NULL;
  1698. i--;
  1699. continue;
  1700. }
  1701. /* this is index block */
  1702. if (!path[i].p_hdr) {
  1703. ext_debug("initialize header\n");
  1704. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  1705. }
  1706. if (!path[i].p_idx) {
  1707. /* this level hasn't been touched yet */
  1708. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  1709. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  1710. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  1711. path[i].p_hdr,
  1712. le16_to_cpu(path[i].p_hdr->eh_entries));
  1713. } else {
  1714. /* we were already here, see at next index */
  1715. path[i].p_idx--;
  1716. }
  1717. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  1718. i, EXT_FIRST_INDEX(path[i].p_hdr),
  1719. path[i].p_idx);
  1720. if (ext4_ext_more_to_rm(path + i)) {
  1721. struct buffer_head *bh;
  1722. /* go to the next level */
  1723. ext_debug("move to level %d (block %llu)\n",
  1724. i + 1, idx_pblock(path[i].p_idx));
  1725. memset(path + i + 1, 0, sizeof(*path));
  1726. bh = sb_bread(sb, idx_pblock(path[i].p_idx));
  1727. if (!bh) {
  1728. /* should we reset i_size? */
  1729. err = -EIO;
  1730. break;
  1731. }
  1732. if (WARN_ON(i + 1 > depth)) {
  1733. err = -EIO;
  1734. break;
  1735. }
  1736. if (ext4_ext_check_header(inode, ext_block_hdr(bh),
  1737. depth - i - 1)) {
  1738. err = -EIO;
  1739. break;
  1740. }
  1741. path[i + 1].p_bh = bh;
  1742. /* save actual number of indexes since this
  1743. * number is changed at the next iteration */
  1744. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  1745. i++;
  1746. } else {
  1747. /* we finished processing this index, go up */
  1748. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  1749. /* index is empty, remove it;
  1750. * handle must be already prepared by the
  1751. * truncatei_leaf() */
  1752. err = ext4_ext_rm_idx(handle, inode, path + i);
  1753. }
  1754. /* root level has p_bh == NULL, brelse() eats this */
  1755. brelse(path[i].p_bh);
  1756. path[i].p_bh = NULL;
  1757. i--;
  1758. ext_debug("return to level %d\n", i);
  1759. }
  1760. }
  1761. /* TODO: flexible tree reduction should be here */
  1762. if (path->p_hdr->eh_entries == 0) {
  1763. /*
  1764. * truncate to zero freed all the tree,
  1765. * so we need to correct eh_depth
  1766. */
  1767. err = ext4_ext_get_access(handle, inode, path);
  1768. if (err == 0) {
  1769. ext_inode_hdr(inode)->eh_depth = 0;
  1770. ext_inode_hdr(inode)->eh_max =
  1771. cpu_to_le16(ext4_ext_space_root(inode));
  1772. err = ext4_ext_dirty(handle, inode, path);
  1773. }
  1774. }
  1775. out:
  1776. ext4_ext_tree_changed(inode);
  1777. ext4_ext_drop_refs(path);
  1778. kfree(path);
  1779. ext4_journal_stop(handle);
  1780. return err;
  1781. }
  1782. /*
  1783. * called at mount time
  1784. */
  1785. void ext4_ext_init(struct super_block *sb)
  1786. {
  1787. /*
  1788. * possible initialization would be here
  1789. */
  1790. if (test_opt(sb, EXTENTS)) {
  1791. printk("EXT4-fs: file extents enabled");
  1792. #ifdef AGGRESSIVE_TEST
  1793. printk(", aggressive tests");
  1794. #endif
  1795. #ifdef CHECK_BINSEARCH
  1796. printk(", check binsearch");
  1797. #endif
  1798. #ifdef EXTENTS_STATS
  1799. printk(", stats");
  1800. #endif
  1801. printk("\n");
  1802. #ifdef EXTENTS_STATS
  1803. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  1804. EXT4_SB(sb)->s_ext_min = 1 << 30;
  1805. EXT4_SB(sb)->s_ext_max = 0;
  1806. #endif
  1807. }
  1808. }
  1809. /*
  1810. * called at umount time
  1811. */
  1812. void ext4_ext_release(struct super_block *sb)
  1813. {
  1814. if (!test_opt(sb, EXTENTS))
  1815. return;
  1816. #ifdef EXTENTS_STATS
  1817. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  1818. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1819. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  1820. sbi->s_ext_blocks, sbi->s_ext_extents,
  1821. sbi->s_ext_blocks / sbi->s_ext_extents);
  1822. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  1823. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  1824. }
  1825. #endif
  1826. }
  1827. /*
  1828. * This function is called by ext4_ext_get_blocks() if someone tries to write
  1829. * to an uninitialized extent. It may result in splitting the uninitialized
  1830. * extent into multiple extents (upto three - one initialized and two
  1831. * uninitialized).
  1832. * There are three possibilities:
  1833. * a> There is no split required: Entire extent should be initialized
  1834. * b> Splits in two extents: Write is happening at either end of the extent
  1835. * c> Splits in three extents: Somone is writing in middle of the extent
  1836. */
  1837. int ext4_ext_convert_to_initialized(handle_t *handle, struct inode *inode,
  1838. struct ext4_ext_path *path,
  1839. ext4_fsblk_t iblock,
  1840. unsigned long max_blocks)
  1841. {
  1842. struct ext4_extent *ex, newex;
  1843. struct ext4_extent *ex1 = NULL;
  1844. struct ext4_extent *ex2 = NULL;
  1845. struct ext4_extent *ex3 = NULL;
  1846. struct ext4_extent_header *eh;
  1847. unsigned int allocated, ee_block, ee_len, depth;
  1848. ext4_fsblk_t newblock;
  1849. int err = 0;
  1850. int ret = 0;
  1851. depth = ext_depth(inode);
  1852. eh = path[depth].p_hdr;
  1853. ex = path[depth].p_ext;
  1854. ee_block = le32_to_cpu(ex->ee_block);
  1855. ee_len = ext4_ext_get_actual_len(ex);
  1856. allocated = ee_len - (iblock - ee_block);
  1857. newblock = iblock - ee_block + ext_pblock(ex);
  1858. ex2 = ex;
  1859. /* ex1: ee_block to iblock - 1 : uninitialized */
  1860. if (iblock > ee_block) {
  1861. ex1 = ex;
  1862. ex1->ee_len = cpu_to_le16(iblock - ee_block);
  1863. ext4_ext_mark_uninitialized(ex1);
  1864. ex2 = &newex;
  1865. }
  1866. /*
  1867. * for sanity, update the length of the ex2 extent before
  1868. * we insert ex3, if ex1 is NULL. This is to avoid temporary
  1869. * overlap of blocks.
  1870. */
  1871. if (!ex1 && allocated > max_blocks)
  1872. ex2->ee_len = cpu_to_le16(max_blocks);
  1873. /* ex3: to ee_block + ee_len : uninitialised */
  1874. if (allocated > max_blocks) {
  1875. unsigned int newdepth;
  1876. ex3 = &newex;
  1877. ex3->ee_block = cpu_to_le32(iblock + max_blocks);
  1878. ext4_ext_store_pblock(ex3, newblock + max_blocks);
  1879. ex3->ee_len = cpu_to_le16(allocated - max_blocks);
  1880. ext4_ext_mark_uninitialized(ex3);
  1881. err = ext4_ext_insert_extent(handle, inode, path, ex3);
  1882. if (err)
  1883. goto out;
  1884. /*
  1885. * The depth, and hence eh & ex might change
  1886. * as part of the insert above.
  1887. */
  1888. newdepth = ext_depth(inode);
  1889. if (newdepth != depth) {
  1890. depth = newdepth;
  1891. path = ext4_ext_find_extent(inode, iblock, NULL);
  1892. if (IS_ERR(path)) {
  1893. err = PTR_ERR(path);
  1894. path = NULL;
  1895. goto out;
  1896. }
  1897. eh = path[depth].p_hdr;
  1898. ex = path[depth].p_ext;
  1899. if (ex2 != &newex)
  1900. ex2 = ex;
  1901. }
  1902. allocated = max_blocks;
  1903. }
  1904. /*
  1905. * If there was a change of depth as part of the
  1906. * insertion of ex3 above, we need to update the length
  1907. * of the ex1 extent again here
  1908. */
  1909. if (ex1 && ex1 != ex) {
  1910. ex1 = ex;
  1911. ex1->ee_len = cpu_to_le16(iblock - ee_block);
  1912. ext4_ext_mark_uninitialized(ex1);
  1913. ex2 = &newex;
  1914. }
  1915. /* ex2: iblock to iblock + maxblocks-1 : initialised */
  1916. ex2->ee_block = cpu_to_le32(iblock);
  1917. ex2->ee_start = cpu_to_le32(newblock);
  1918. ext4_ext_store_pblock(ex2, newblock);
  1919. ex2->ee_len = cpu_to_le16(allocated);
  1920. if (ex2 != ex)
  1921. goto insert;
  1922. err = ext4_ext_get_access(handle, inode, path + depth);
  1923. if (err)
  1924. goto out;
  1925. /*
  1926. * New (initialized) extent starts from the first block
  1927. * in the current extent. i.e., ex2 == ex
  1928. * We have to see if it can be merged with the extent
  1929. * on the left.
  1930. */
  1931. if (ex2 > EXT_FIRST_EXTENT(eh)) {
  1932. /*
  1933. * To merge left, pass "ex2 - 1" to try_to_merge(),
  1934. * since it merges towards right _only_.
  1935. */
  1936. ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
  1937. if (ret) {
  1938. err = ext4_ext_correct_indexes(handle, inode, path);
  1939. if (err)
  1940. goto out;
  1941. depth = ext_depth(inode);
  1942. ex2--;
  1943. }
  1944. }
  1945. /*
  1946. * Try to Merge towards right. This might be required
  1947. * only when the whole extent is being written to.
  1948. * i.e. ex2 == ex and ex3 == NULL.
  1949. */
  1950. if (!ex3) {
  1951. ret = ext4_ext_try_to_merge(inode, path, ex2);
  1952. if (ret) {
  1953. err = ext4_ext_correct_indexes(handle, inode, path);
  1954. if (err)
  1955. goto out;
  1956. }
  1957. }
  1958. /* Mark modified extent as dirty */
  1959. err = ext4_ext_dirty(handle, inode, path + depth);
  1960. goto out;
  1961. insert:
  1962. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  1963. out:
  1964. return err ? err : allocated;
  1965. }
  1966. int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
  1967. ext4_fsblk_t iblock,
  1968. unsigned long max_blocks, struct buffer_head *bh_result,
  1969. int create, int extend_disksize)
  1970. {
  1971. struct ext4_ext_path *path = NULL;
  1972. struct ext4_extent_header *eh;
  1973. struct ext4_extent newex, *ex;
  1974. ext4_fsblk_t goal, newblock;
  1975. int err = 0, depth, ret;
  1976. unsigned long allocated = 0;
  1977. __clear_bit(BH_New, &bh_result->b_state);
  1978. ext_debug("blocks %d/%lu requested for inode %u\n", (int) iblock,
  1979. max_blocks, (unsigned) inode->i_ino);
  1980. mutex_lock(&EXT4_I(inode)->truncate_mutex);
  1981. /* check in cache */
  1982. goal = ext4_ext_in_cache(inode, iblock, &newex);
  1983. if (goal) {
  1984. if (goal == EXT4_EXT_CACHE_GAP) {
  1985. if (!create) {
  1986. /*
  1987. * block isn't allocated yet and
  1988. * user doesn't want to allocate it
  1989. */
  1990. goto out2;
  1991. }
  1992. /* we should allocate requested block */
  1993. } else if (goal == EXT4_EXT_CACHE_EXTENT) {
  1994. /* block is already allocated */
  1995. newblock = iblock
  1996. - le32_to_cpu(newex.ee_block)
  1997. + ext_pblock(&newex);
  1998. /* number of remaining blocks in the extent */
  1999. allocated = le16_to_cpu(newex.ee_len) -
  2000. (iblock - le32_to_cpu(newex.ee_block));
  2001. goto out;
  2002. } else {
  2003. BUG();
  2004. }
  2005. }
  2006. /* find extent for this block */
  2007. path = ext4_ext_find_extent(inode, iblock, NULL);
  2008. if (IS_ERR(path)) {
  2009. err = PTR_ERR(path);
  2010. path = NULL;
  2011. goto out2;
  2012. }
  2013. depth = ext_depth(inode);
  2014. /*
  2015. * consistent leaf must not be empty;
  2016. * this situation is possible, though, _during_ tree modification;
  2017. * this is why assert can't be put in ext4_ext_find_extent()
  2018. */
  2019. BUG_ON(path[depth].p_ext == NULL && depth != 0);
  2020. eh = path[depth].p_hdr;
  2021. ex = path[depth].p_ext;
  2022. if (ex) {
  2023. unsigned long ee_block = le32_to_cpu(ex->ee_block);
  2024. ext4_fsblk_t ee_start = ext_pblock(ex);
  2025. unsigned short ee_len;
  2026. /*
  2027. * Uninitialized extents are treated as holes, except that
  2028. * we split out initialized portions during a write.
  2029. */
  2030. ee_len = ext4_ext_get_actual_len(ex);
  2031. /* if found extent covers block, simply return it */
  2032. if (iblock >= ee_block && iblock < ee_block + ee_len) {
  2033. newblock = iblock - ee_block + ee_start;
  2034. /* number of remaining blocks in the extent */
  2035. allocated = ee_len - (iblock - ee_block);
  2036. ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock,
  2037. ee_block, ee_len, newblock);
  2038. /* Do not put uninitialized extent in the cache */
  2039. if (!ext4_ext_is_uninitialized(ex)) {
  2040. ext4_ext_put_in_cache(inode, ee_block,
  2041. ee_len, ee_start,
  2042. EXT4_EXT_CACHE_EXTENT);
  2043. goto out;
  2044. }
  2045. if (create == EXT4_CREATE_UNINITIALIZED_EXT)
  2046. goto out;
  2047. if (!create)
  2048. goto out2;
  2049. ret = ext4_ext_convert_to_initialized(handle, inode,
  2050. path, iblock,
  2051. max_blocks);
  2052. if (ret <= 0)
  2053. goto out2;
  2054. else
  2055. allocated = ret;
  2056. goto outnew;
  2057. }
  2058. }
  2059. /*
  2060. * requested block isn't allocated yet;
  2061. * we couldn't try to create block if create flag is zero
  2062. */
  2063. if (!create) {
  2064. /*
  2065. * put just found gap into cache to speed up
  2066. * subsequent requests
  2067. */
  2068. ext4_ext_put_gap_in_cache(inode, path, iblock);
  2069. goto out2;
  2070. }
  2071. /*
  2072. * Okay, we need to do block allocation. Lazily initialize the block
  2073. * allocation info here if necessary.
  2074. */
  2075. if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
  2076. ext4_init_block_alloc_info(inode);
  2077. /* allocate new block */
  2078. goal = ext4_ext_find_goal(inode, path, iblock);
  2079. /*
  2080. * See if request is beyond maximum number of blocks we can have in
  2081. * a single extent. For an initialized extent this limit is
  2082. * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
  2083. * EXT_UNINIT_MAX_LEN.
  2084. */
  2085. if (max_blocks > EXT_INIT_MAX_LEN &&
  2086. create != EXT4_CREATE_UNINITIALIZED_EXT)
  2087. max_blocks = EXT_INIT_MAX_LEN;
  2088. else if (max_blocks > EXT_UNINIT_MAX_LEN &&
  2089. create == EXT4_CREATE_UNINITIALIZED_EXT)
  2090. max_blocks = EXT_UNINIT_MAX_LEN;
  2091. /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
  2092. newex.ee_block = cpu_to_le32(iblock);
  2093. newex.ee_len = cpu_to_le16(max_blocks);
  2094. err = ext4_ext_check_overlap(inode, &newex, path);
  2095. if (err)
  2096. allocated = le16_to_cpu(newex.ee_len);
  2097. else
  2098. allocated = max_blocks;
  2099. newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err);
  2100. if (!newblock)
  2101. goto out2;
  2102. ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
  2103. goal, newblock, allocated);
  2104. /* try to insert new extent into found leaf and return */
  2105. ext4_ext_store_pblock(&newex, newblock);
  2106. newex.ee_len = cpu_to_le16(allocated);
  2107. if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
  2108. ext4_ext_mark_uninitialized(&newex);
  2109. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  2110. if (err) {
  2111. /* free data blocks we just allocated */
  2112. ext4_free_blocks(handle, inode, ext_pblock(&newex),
  2113. le16_to_cpu(newex.ee_len));
  2114. goto out2;
  2115. }
  2116. if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
  2117. EXT4_I(inode)->i_disksize = inode->i_size;
  2118. /* previous routine could use block we allocated */
  2119. newblock = ext_pblock(&newex);
  2120. outnew:
  2121. __set_bit(BH_New, &bh_result->b_state);
  2122. /* Cache only when it is _not_ an uninitialized extent */
  2123. if (create != EXT4_CREATE_UNINITIALIZED_EXT)
  2124. ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
  2125. EXT4_EXT_CACHE_EXTENT);
  2126. out:
  2127. if (allocated > max_blocks)
  2128. allocated = max_blocks;
  2129. ext4_ext_show_leaf(inode, path);
  2130. __set_bit(BH_Mapped, &bh_result->b_state);
  2131. bh_result->b_bdev = inode->i_sb->s_bdev;
  2132. bh_result->b_blocknr = newblock;
  2133. out2:
  2134. if (path) {
  2135. ext4_ext_drop_refs(path);
  2136. kfree(path);
  2137. }
  2138. mutex_unlock(&EXT4_I(inode)->truncate_mutex);
  2139. return err ? err : allocated;
  2140. }
  2141. void ext4_ext_truncate(struct inode * inode, struct page *page)
  2142. {
  2143. struct address_space *mapping = inode->i_mapping;
  2144. struct super_block *sb = inode->i_sb;
  2145. unsigned long last_block;
  2146. handle_t *handle;
  2147. int err = 0;
  2148. /*
  2149. * probably first extent we're gonna free will be last in block
  2150. */
  2151. err = ext4_writepage_trans_blocks(inode) + 3;
  2152. handle = ext4_journal_start(inode, err);
  2153. if (IS_ERR(handle)) {
  2154. if (page) {
  2155. clear_highpage(page);
  2156. flush_dcache_page(page);
  2157. unlock_page(page);
  2158. page_cache_release(page);
  2159. }
  2160. return;
  2161. }
  2162. if (page)
  2163. ext4_block_truncate_page(handle, page, mapping, inode->i_size);
  2164. mutex_lock(&EXT4_I(inode)->truncate_mutex);
  2165. ext4_ext_invalidate_cache(inode);
  2166. /*
  2167. * TODO: optimization is possible here.
  2168. * Probably we need not scan at all,
  2169. * because page truncation is enough.
  2170. */
  2171. if (ext4_orphan_add(handle, inode))
  2172. goto out_stop;
  2173. /* we have to know where to truncate from in crash case */
  2174. EXT4_I(inode)->i_disksize = inode->i_size;
  2175. ext4_mark_inode_dirty(handle, inode);
  2176. last_block = (inode->i_size + sb->s_blocksize - 1)
  2177. >> EXT4_BLOCK_SIZE_BITS(sb);
  2178. err = ext4_ext_remove_space(inode, last_block);
  2179. /* In a multi-transaction truncate, we only make the final
  2180. * transaction synchronous.
  2181. */
  2182. if (IS_SYNC(inode))
  2183. handle->h_sync = 1;
  2184. out_stop:
  2185. /*
  2186. * If this was a simple ftruncate() and the file will remain alive,
  2187. * then we need to clear up the orphan record which we created above.
  2188. * However, if this was a real unlink then we were called by
  2189. * ext4_delete_inode(), and we allow that function to clean up the
  2190. * orphan info for us.
  2191. */
  2192. if (inode->i_nlink)
  2193. ext4_orphan_del(handle, inode);
  2194. mutex_unlock(&EXT4_I(inode)->truncate_mutex);
  2195. ext4_journal_stop(handle);
  2196. }
  2197. /*
  2198. * ext4_ext_writepage_trans_blocks:
  2199. * calculate max number of blocks we could modify
  2200. * in order to allocate new block for an inode
  2201. */
  2202. int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
  2203. {
  2204. int needed;
  2205. needed = ext4_ext_calc_credits_for_insert(inode, NULL);
  2206. /* caller wants to allocate num blocks, but note it includes sb */
  2207. needed = needed * num - (num - 1);
  2208. #ifdef CONFIG_QUOTA
  2209. needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  2210. #endif
  2211. return needed;
  2212. }
  2213. /*
  2214. * preallocate space for a file. This implements ext4's fallocate inode
  2215. * operation, which gets called from sys_fallocate system call.
  2216. * For block-mapped files, posix_fallocate should fall back to the method
  2217. * of writing zeroes to the required new blocks (the same behavior which is
  2218. * expected for file systems which do not support fallocate() system call).
  2219. */
  2220. long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
  2221. {
  2222. handle_t *handle;
  2223. ext4_fsblk_t block, max_blocks;
  2224. ext4_fsblk_t nblocks = 0;
  2225. int ret = 0;
  2226. int ret2 = 0;
  2227. int retries = 0;
  2228. struct buffer_head map_bh;
  2229. unsigned int credits, blkbits = inode->i_blkbits;
  2230. /*
  2231. * currently supporting (pre)allocate mode for extent-based
  2232. * files _only_
  2233. */
  2234. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  2235. return -EOPNOTSUPP;
  2236. /* preallocation to directories is currently not supported */
  2237. if (S_ISDIR(inode->i_mode))
  2238. return -ENODEV;
  2239. block = offset >> blkbits;
  2240. max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
  2241. - block;
  2242. /*
  2243. * credits to insert 1 extent into extent tree + buffers to be able to
  2244. * modify 1 super block, 1 block bitmap and 1 group descriptor.
  2245. */
  2246. credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
  2247. retry:
  2248. while (ret >= 0 && ret < max_blocks) {
  2249. block = block + ret;
  2250. max_blocks = max_blocks - ret;
  2251. handle = ext4_journal_start(inode, credits);
  2252. if (IS_ERR(handle)) {
  2253. ret = PTR_ERR(handle);
  2254. break;
  2255. }
  2256. ret = ext4_ext_get_blocks(handle, inode, block,
  2257. max_blocks, &map_bh,
  2258. EXT4_CREATE_UNINITIALIZED_EXT, 0);
  2259. WARN_ON(!ret);
  2260. if (!ret) {
  2261. ext4_error(inode->i_sb, "ext4_fallocate",
  2262. "ext4_ext_get_blocks returned 0! inode#%lu"
  2263. ", block=%llu, max_blocks=%llu",
  2264. inode->i_ino, block, max_blocks);
  2265. ret = -EIO;
  2266. ext4_mark_inode_dirty(handle, inode);
  2267. ret2 = ext4_journal_stop(handle);
  2268. break;
  2269. }
  2270. if (ret > 0) {
  2271. /* check wrap through sign-bit/zero here */
  2272. if ((block + ret) < 0 || (block + ret) < block) {
  2273. ret = -EIO;
  2274. ext4_mark_inode_dirty(handle, inode);
  2275. ret2 = ext4_journal_stop(handle);
  2276. break;
  2277. }
  2278. if (buffer_new(&map_bh) && ((block + ret) >
  2279. (EXT4_BLOCK_ALIGN(i_size_read(inode), blkbits)
  2280. >> blkbits)))
  2281. nblocks = nblocks + ret;
  2282. }
  2283. /* Update ctime if new blocks get allocated */
  2284. if (nblocks) {
  2285. struct timespec now;
  2286. now = current_fs_time(inode->i_sb);
  2287. if (!timespec_equal(&inode->i_ctime, &now))
  2288. inode->i_ctime = now;
  2289. }
  2290. ext4_mark_inode_dirty(handle, inode);
  2291. ret2 = ext4_journal_stop(handle);
  2292. if (ret2)
  2293. break;
  2294. }
  2295. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  2296. goto retry;
  2297. /*
  2298. * Time to update the file size.
  2299. * Update only when preallocation was requested beyond the file size.
  2300. */
  2301. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  2302. (offset + len) > i_size_read(inode)) {
  2303. if (ret > 0) {
  2304. /*
  2305. * if no error, we assume preallocation succeeded
  2306. * completely
  2307. */
  2308. mutex_lock(&inode->i_mutex);
  2309. i_size_write(inode, offset + len);
  2310. EXT4_I(inode)->i_disksize = i_size_read(inode);
  2311. mutex_unlock(&inode->i_mutex);
  2312. } else if (ret < 0 && nblocks) {
  2313. /* Handle partial allocation scenario */
  2314. loff_t newsize;
  2315. mutex_lock(&inode->i_mutex);
  2316. newsize = (nblocks << blkbits) + i_size_read(inode);
  2317. i_size_write(inode, EXT4_BLOCK_ALIGN(newsize, blkbits));
  2318. EXT4_I(inode)->i_disksize = i_size_read(inode);
  2319. mutex_unlock(&inode->i_mutex);
  2320. }
  2321. }
  2322. return ret > 0 ? ret2 : ret;
  2323. }