extents.c 84 KB

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