extents.c 85 KB

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