extents.c 82 KB

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