extents.c 103 KB

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