extents.c 76 KB

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