extents.c 82 KB

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