extents.c 103 KB

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