inode.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/statfs.h>
  34. #include <linux/compat.h>
  35. #include <linux/bit_spinlock.h>
  36. #include <linux/version.h>
  37. #include <linux/xattr.h>
  38. #include <linux/posix_acl.h>
  39. #include "ctree.h"
  40. #include "disk-io.h"
  41. #include "transaction.h"
  42. #include "btrfs_inode.h"
  43. #include "ioctl.h"
  44. #include "print-tree.h"
  45. #include "volumes.h"
  46. #include "ordered-data.h"
  47. #include "xattr.h"
  48. #include "compat.h"
  49. #include "tree-log.h"
  50. struct btrfs_iget_args {
  51. u64 ino;
  52. struct btrfs_root *root;
  53. };
  54. static struct inode_operations btrfs_dir_inode_operations;
  55. static struct inode_operations btrfs_symlink_inode_operations;
  56. static struct inode_operations btrfs_dir_ro_inode_operations;
  57. static struct inode_operations btrfs_special_inode_operations;
  58. static struct inode_operations btrfs_file_inode_operations;
  59. static struct address_space_operations btrfs_aops;
  60. static struct address_space_operations btrfs_symlink_aops;
  61. static struct file_operations btrfs_dir_file_operations;
  62. static struct extent_io_ops btrfs_extent_io_ops;
  63. static struct kmem_cache *btrfs_inode_cachep;
  64. struct kmem_cache *btrfs_trans_handle_cachep;
  65. struct kmem_cache *btrfs_transaction_cachep;
  66. struct kmem_cache *btrfs_bit_radix_cachep;
  67. struct kmem_cache *btrfs_path_cachep;
  68. #define S_SHIFT 12
  69. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  70. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  71. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  72. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  73. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  74. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  75. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  76. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  77. };
  78. static void btrfs_truncate(struct inode *inode);
  79. /*
  80. * a very lame attempt at stopping writes when the FS is 85% full. There
  81. * are countless ways this is incorrect, but it is better than nothing.
  82. */
  83. int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
  84. int for_del)
  85. {
  86. u64 total;
  87. u64 used;
  88. u64 thresh;
  89. unsigned long flags;
  90. int ret = 0;
  91. spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
  92. total = btrfs_super_total_bytes(&root->fs_info->super_copy);
  93. used = btrfs_super_bytes_used(&root->fs_info->super_copy);
  94. if (for_del)
  95. thresh = total * 90;
  96. else
  97. thresh = total * 85;
  98. do_div(thresh, 100);
  99. if (used + root->fs_info->delalloc_bytes + num_required > thresh)
  100. ret = -ENOSPC;
  101. spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
  102. return ret;
  103. }
  104. /*
  105. * when extent_io.c finds a delayed allocation range in the file,
  106. * the call backs end up in this code. The basic idea is to
  107. * allocate extents on disk for the range, and create ordered data structs
  108. * in ram to track those extents.
  109. */
  110. static int cow_file_range(struct inode *inode, u64 start, u64 end)
  111. {
  112. struct btrfs_root *root = BTRFS_I(inode)->root;
  113. struct btrfs_trans_handle *trans;
  114. u64 alloc_hint = 0;
  115. u64 num_bytes;
  116. u64 cur_alloc_size;
  117. u64 blocksize = root->sectorsize;
  118. u64 orig_num_bytes;
  119. struct btrfs_key ins;
  120. struct extent_map *em;
  121. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  122. int ret = 0;
  123. trans = btrfs_join_transaction(root, 1);
  124. BUG_ON(!trans);
  125. btrfs_set_trans_block_group(trans, inode);
  126. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  127. num_bytes = max(blocksize, num_bytes);
  128. orig_num_bytes = num_bytes;
  129. if (alloc_hint == EXTENT_MAP_INLINE)
  130. goto out;
  131. BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
  132. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  133. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  134. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  135. while(num_bytes > 0) {
  136. cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
  137. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  138. root->sectorsize, 0, alloc_hint,
  139. (u64)-1, &ins, 1);
  140. if (ret) {
  141. WARN_ON(1);
  142. goto out;
  143. }
  144. em = alloc_extent_map(GFP_NOFS);
  145. em->start = start;
  146. em->len = ins.offset;
  147. em->block_start = ins.objectid;
  148. em->bdev = root->fs_info->fs_devices->latest_bdev;
  149. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  150. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  151. while(1) {
  152. spin_lock(&em_tree->lock);
  153. ret = add_extent_mapping(em_tree, em);
  154. spin_unlock(&em_tree->lock);
  155. if (ret != -EEXIST) {
  156. free_extent_map(em);
  157. break;
  158. }
  159. btrfs_drop_extent_cache(inode, start,
  160. start + ins.offset - 1, 0);
  161. }
  162. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  163. cur_alloc_size = ins.offset;
  164. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  165. ins.offset, 0);
  166. BUG_ON(ret);
  167. if (num_bytes < cur_alloc_size) {
  168. printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
  169. cur_alloc_size);
  170. break;
  171. }
  172. num_bytes -= cur_alloc_size;
  173. alloc_hint = ins.objectid + ins.offset;
  174. start += cur_alloc_size;
  175. }
  176. out:
  177. btrfs_end_transaction(trans, root);
  178. return ret;
  179. }
  180. /*
  181. * when nowcow writeback call back. This checks for snapshots or COW copies
  182. * of the extents that exist in the file, and COWs the file as required.
  183. *
  184. * If no cow copies or snapshots exist, we write directly to the existing
  185. * blocks on disk
  186. */
  187. static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
  188. {
  189. u64 extent_start;
  190. u64 extent_end;
  191. u64 bytenr;
  192. u64 loops = 0;
  193. u64 total_fs_bytes;
  194. struct btrfs_root *root = BTRFS_I(inode)->root;
  195. struct btrfs_block_group_cache *block_group;
  196. struct btrfs_trans_handle *trans;
  197. struct extent_buffer *leaf;
  198. int found_type;
  199. struct btrfs_path *path;
  200. struct btrfs_file_extent_item *item;
  201. int ret;
  202. int err = 0;
  203. struct btrfs_key found_key;
  204. total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  205. path = btrfs_alloc_path();
  206. BUG_ON(!path);
  207. trans = btrfs_join_transaction(root, 1);
  208. BUG_ON(!trans);
  209. again:
  210. ret = btrfs_lookup_file_extent(NULL, root, path,
  211. inode->i_ino, start, 0);
  212. if (ret < 0) {
  213. err = ret;
  214. goto out;
  215. }
  216. if (ret != 0) {
  217. if (path->slots[0] == 0)
  218. goto not_found;
  219. path->slots[0]--;
  220. }
  221. leaf = path->nodes[0];
  222. item = btrfs_item_ptr(leaf, path->slots[0],
  223. struct btrfs_file_extent_item);
  224. /* are we inside the extent that was found? */
  225. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  226. found_type = btrfs_key_type(&found_key);
  227. if (found_key.objectid != inode->i_ino ||
  228. found_type != BTRFS_EXTENT_DATA_KEY)
  229. goto not_found;
  230. found_type = btrfs_file_extent_type(leaf, item);
  231. extent_start = found_key.offset;
  232. if (found_type == BTRFS_FILE_EXTENT_REG) {
  233. u64 extent_num_bytes;
  234. extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
  235. extent_end = extent_start + extent_num_bytes;
  236. err = 0;
  237. if (loops && start != extent_start)
  238. goto not_found;
  239. if (start < extent_start || start >= extent_end)
  240. goto not_found;
  241. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  242. if (bytenr == 0)
  243. goto not_found;
  244. if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
  245. goto not_found;
  246. /*
  247. * we may be called by the resizer, make sure we're inside
  248. * the limits of the FS
  249. */
  250. block_group = btrfs_lookup_block_group(root->fs_info,
  251. bytenr);
  252. if (!block_group || block_group->ro)
  253. goto not_found;
  254. bytenr += btrfs_file_extent_offset(leaf, item);
  255. extent_num_bytes = min(end + 1, extent_end) - start;
  256. ret = btrfs_add_ordered_extent(inode, start, bytenr,
  257. extent_num_bytes, 1);
  258. if (ret) {
  259. err = ret;
  260. goto out;
  261. }
  262. btrfs_release_path(root, path);
  263. start = extent_end;
  264. if (start <= end) {
  265. loops++;
  266. goto again;
  267. }
  268. } else {
  269. not_found:
  270. btrfs_end_transaction(trans, root);
  271. btrfs_free_path(path);
  272. return cow_file_range(inode, start, end);
  273. }
  274. out:
  275. WARN_ON(err);
  276. btrfs_end_transaction(trans, root);
  277. btrfs_free_path(path);
  278. return err;
  279. }
  280. /*
  281. * extent_io.c call back to do delayed allocation processing
  282. */
  283. static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
  284. {
  285. struct btrfs_root *root = BTRFS_I(inode)->root;
  286. int ret;
  287. if (btrfs_test_opt(root, NODATACOW) ||
  288. btrfs_test_flag(inode, NODATACOW))
  289. ret = run_delalloc_nocow(inode, start, end);
  290. else
  291. ret = cow_file_range(inode, start, end);
  292. return ret;
  293. }
  294. /*
  295. * extent_io.c set_bit_hook, used to track delayed allocation
  296. * bytes in this file, and to maintain the list of inodes that
  297. * have pending delalloc work to be done.
  298. */
  299. int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
  300. unsigned long old, unsigned long bits)
  301. {
  302. unsigned long flags;
  303. if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  304. struct btrfs_root *root = BTRFS_I(inode)->root;
  305. spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
  306. BTRFS_I(inode)->delalloc_bytes += end - start + 1;
  307. root->fs_info->delalloc_bytes += end - start + 1;
  308. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  309. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  310. &root->fs_info->delalloc_inodes);
  311. }
  312. spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
  313. }
  314. return 0;
  315. }
  316. /*
  317. * extent_io.c clear_bit_hook, see set_bit_hook for why
  318. */
  319. int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
  320. unsigned long old, unsigned long bits)
  321. {
  322. if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  323. struct btrfs_root *root = BTRFS_I(inode)->root;
  324. unsigned long flags;
  325. spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
  326. if (end - start + 1 > root->fs_info->delalloc_bytes) {
  327. printk("warning: delalloc account %Lu %Lu\n",
  328. end - start + 1, root->fs_info->delalloc_bytes);
  329. root->fs_info->delalloc_bytes = 0;
  330. BTRFS_I(inode)->delalloc_bytes = 0;
  331. } else {
  332. root->fs_info->delalloc_bytes -= end - start + 1;
  333. BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
  334. }
  335. if (BTRFS_I(inode)->delalloc_bytes == 0 &&
  336. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  337. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  338. }
  339. spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
  340. }
  341. return 0;
  342. }
  343. /*
  344. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  345. * we don't create bios that span stripes or chunks
  346. */
  347. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  348. size_t size, struct bio *bio)
  349. {
  350. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  351. struct btrfs_mapping_tree *map_tree;
  352. u64 logical = (u64)bio->bi_sector << 9;
  353. u64 length = 0;
  354. u64 map_length;
  355. int ret;
  356. length = bio->bi_size;
  357. map_tree = &root->fs_info->mapping_tree;
  358. map_length = length;
  359. ret = btrfs_map_block(map_tree, READ, logical,
  360. &map_length, NULL, 0);
  361. if (map_length < length + size) {
  362. return 1;
  363. }
  364. return 0;
  365. }
  366. /*
  367. * in order to insert checksums into the metadata in large chunks,
  368. * we wait until bio submission time. All the pages in the bio are
  369. * checksummed and sums are attached onto the ordered extent record.
  370. *
  371. * At IO completion time the cums attached on the ordered extent record
  372. * are inserted into the btree
  373. */
  374. int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  375. int mirror_num)
  376. {
  377. struct btrfs_root *root = BTRFS_I(inode)->root;
  378. int ret = 0;
  379. ret = btrfs_csum_one_bio(root, inode, bio);
  380. BUG_ON(ret);
  381. return btrfs_map_bio(root, rw, bio, mirror_num, 1);
  382. }
  383. /*
  384. * extent_io.c submission hook. This does the right thing for csum calculation on write,
  385. * or reading the csums from the tree before a read
  386. */
  387. int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  388. int mirror_num)
  389. {
  390. struct btrfs_root *root = BTRFS_I(inode)->root;
  391. int ret = 0;
  392. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  393. BUG_ON(ret);
  394. if (btrfs_test_opt(root, NODATASUM) ||
  395. btrfs_test_flag(inode, NODATASUM)) {
  396. goto mapit;
  397. }
  398. if (!(rw & (1 << BIO_RW))) {
  399. btrfs_lookup_bio_sums(root, inode, bio);
  400. goto mapit;
  401. }
  402. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  403. inode, rw, bio, mirror_num,
  404. __btrfs_submit_bio_hook);
  405. mapit:
  406. return btrfs_map_bio(root, rw, bio, mirror_num, 0);
  407. }
  408. /*
  409. * given a list of ordered sums record them in the inode. This happens
  410. * at IO completion time based on sums calculated at bio submission time.
  411. */
  412. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  413. struct inode *inode, u64 file_offset,
  414. struct list_head *list)
  415. {
  416. struct list_head *cur;
  417. struct btrfs_ordered_sum *sum;
  418. btrfs_set_trans_block_group(trans, inode);
  419. list_for_each(cur, list) {
  420. sum = list_entry(cur, struct btrfs_ordered_sum, list);
  421. btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
  422. inode, sum);
  423. }
  424. return 0;
  425. }
  426. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
  427. {
  428. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  429. GFP_NOFS);
  430. }
  431. /* see btrfs_writepage_start_hook for details on why this is required */
  432. struct btrfs_writepage_fixup {
  433. struct page *page;
  434. struct btrfs_work work;
  435. };
  436. void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  437. {
  438. struct btrfs_writepage_fixup *fixup;
  439. struct btrfs_ordered_extent *ordered;
  440. struct page *page;
  441. struct inode *inode;
  442. u64 page_start;
  443. u64 page_end;
  444. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  445. page = fixup->page;
  446. again:
  447. lock_page(page);
  448. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  449. ClearPageChecked(page);
  450. goto out_page;
  451. }
  452. inode = page->mapping->host;
  453. page_start = page_offset(page);
  454. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  455. lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  456. /* already ordered? We're done */
  457. if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  458. EXTENT_ORDERED, 0)) {
  459. goto out;
  460. }
  461. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  462. if (ordered) {
  463. unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
  464. page_end, GFP_NOFS);
  465. unlock_page(page);
  466. btrfs_start_ordered_extent(inode, ordered, 1);
  467. goto again;
  468. }
  469. btrfs_set_extent_delalloc(inode, page_start, page_end);
  470. ClearPageChecked(page);
  471. out:
  472. unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  473. out_page:
  474. unlock_page(page);
  475. page_cache_release(page);
  476. }
  477. /*
  478. * There are a few paths in the higher layers of the kernel that directly
  479. * set the page dirty bit without asking the filesystem if it is a
  480. * good idea. This causes problems because we want to make sure COW
  481. * properly happens and the data=ordered rules are followed.
  482. *
  483. * In our case any range that doesn't have the EXTENT_ORDERED bit set
  484. * hasn't been properly setup for IO. We kick off an async process
  485. * to fix it up. The async helper will wait for ordered extents, set
  486. * the delalloc bit and make it safe to write the page.
  487. */
  488. int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  489. {
  490. struct inode *inode = page->mapping->host;
  491. struct btrfs_writepage_fixup *fixup;
  492. struct btrfs_root *root = BTRFS_I(inode)->root;
  493. int ret;
  494. ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
  495. EXTENT_ORDERED, 0);
  496. if (ret)
  497. return 0;
  498. if (PageChecked(page))
  499. return -EAGAIN;
  500. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  501. if (!fixup)
  502. return -EAGAIN;
  503. SetPageChecked(page);
  504. page_cache_get(page);
  505. fixup->work.func = btrfs_writepage_fixup_worker;
  506. fixup->page = page;
  507. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  508. return -EAGAIN;
  509. }
  510. /* as ordered data IO finishes, this gets called so we can finish
  511. * an ordered extent if the range of bytes in the file it covers are
  512. * fully written.
  513. */
  514. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
  515. {
  516. struct btrfs_root *root = BTRFS_I(inode)->root;
  517. struct btrfs_trans_handle *trans;
  518. struct btrfs_ordered_extent *ordered_extent;
  519. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  520. struct btrfs_file_extent_item *extent_item;
  521. struct btrfs_path *path = NULL;
  522. struct extent_buffer *leaf;
  523. u64 alloc_hint = 0;
  524. struct list_head list;
  525. struct btrfs_key ins;
  526. int ret;
  527. ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
  528. if (!ret)
  529. return 0;
  530. trans = btrfs_join_transaction(root, 1);
  531. ordered_extent = btrfs_lookup_ordered_extent(inode, start);
  532. BUG_ON(!ordered_extent);
  533. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
  534. goto nocow;
  535. path = btrfs_alloc_path();
  536. BUG_ON(!path);
  537. lock_extent(io_tree, ordered_extent->file_offset,
  538. ordered_extent->file_offset + ordered_extent->len - 1,
  539. GFP_NOFS);
  540. INIT_LIST_HEAD(&list);
  541. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  542. ret = btrfs_drop_extents(trans, root, inode,
  543. ordered_extent->file_offset,
  544. ordered_extent->file_offset +
  545. ordered_extent->len,
  546. ordered_extent->file_offset, &alloc_hint);
  547. BUG_ON(ret);
  548. ins.objectid = inode->i_ino;
  549. ins.offset = ordered_extent->file_offset;
  550. ins.type = BTRFS_EXTENT_DATA_KEY;
  551. ret = btrfs_insert_empty_item(trans, root, path, &ins,
  552. sizeof(*extent_item));
  553. BUG_ON(ret);
  554. leaf = path->nodes[0];
  555. extent_item = btrfs_item_ptr(leaf, path->slots[0],
  556. struct btrfs_file_extent_item);
  557. btrfs_set_file_extent_generation(leaf, extent_item, trans->transid);
  558. btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG);
  559. btrfs_set_file_extent_disk_bytenr(leaf, extent_item,
  560. ordered_extent->start);
  561. btrfs_set_file_extent_disk_num_bytes(leaf, extent_item,
  562. ordered_extent->len);
  563. btrfs_set_file_extent_offset(leaf, extent_item, 0);
  564. btrfs_set_file_extent_num_bytes(leaf, extent_item,
  565. ordered_extent->len);
  566. btrfs_mark_buffer_dirty(leaf);
  567. btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
  568. ordered_extent->file_offset +
  569. ordered_extent->len - 1, 0);
  570. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  571. ins.objectid = ordered_extent->start;
  572. ins.offset = ordered_extent->len;
  573. ins.type = BTRFS_EXTENT_ITEM_KEY;
  574. ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
  575. root->root_key.objectid,
  576. trans->transid, inode->i_ino, &ins);
  577. BUG_ON(ret);
  578. btrfs_release_path(root, path);
  579. inode_add_bytes(inode, ordered_extent->len);
  580. unlock_extent(io_tree, ordered_extent->file_offset,
  581. ordered_extent->file_offset + ordered_extent->len - 1,
  582. GFP_NOFS);
  583. nocow:
  584. add_pending_csums(trans, inode, ordered_extent->file_offset,
  585. &ordered_extent->list);
  586. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  587. btrfs_ordered_update_i_size(inode, ordered_extent);
  588. btrfs_update_inode(trans, root, inode);
  589. btrfs_remove_ordered_extent(inode, ordered_extent);
  590. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  591. /* once for us */
  592. btrfs_put_ordered_extent(ordered_extent);
  593. /* once for the tree */
  594. btrfs_put_ordered_extent(ordered_extent);
  595. btrfs_end_transaction(trans, root);
  596. if (path)
  597. btrfs_free_path(path);
  598. return 0;
  599. }
  600. int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  601. struct extent_state *state, int uptodate)
  602. {
  603. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  604. }
  605. /*
  606. * When IO fails, either with EIO or csum verification fails, we
  607. * try other mirrors that might have a good copy of the data. This
  608. * io_failure_record is used to record state as we go through all the
  609. * mirrors. If another mirror has good data, the page is set up to date
  610. * and things continue. If a good mirror can't be found, the original
  611. * bio end_io callback is called to indicate things have failed.
  612. */
  613. struct io_failure_record {
  614. struct page *page;
  615. u64 start;
  616. u64 len;
  617. u64 logical;
  618. int last_mirror;
  619. };
  620. int btrfs_io_failed_hook(struct bio *failed_bio,
  621. struct page *page, u64 start, u64 end,
  622. struct extent_state *state)
  623. {
  624. struct io_failure_record *failrec = NULL;
  625. u64 private;
  626. struct extent_map *em;
  627. struct inode *inode = page->mapping->host;
  628. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  629. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  630. struct bio *bio;
  631. int num_copies;
  632. int ret;
  633. int rw;
  634. u64 logical;
  635. ret = get_state_private(failure_tree, start, &private);
  636. if (ret) {
  637. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  638. if (!failrec)
  639. return -ENOMEM;
  640. failrec->start = start;
  641. failrec->len = end - start + 1;
  642. failrec->last_mirror = 0;
  643. spin_lock(&em_tree->lock);
  644. em = lookup_extent_mapping(em_tree, start, failrec->len);
  645. if (em->start > start || em->start + em->len < start) {
  646. free_extent_map(em);
  647. em = NULL;
  648. }
  649. spin_unlock(&em_tree->lock);
  650. if (!em || IS_ERR(em)) {
  651. kfree(failrec);
  652. return -EIO;
  653. }
  654. logical = start - em->start;
  655. logical = em->block_start + logical;
  656. failrec->logical = logical;
  657. free_extent_map(em);
  658. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  659. EXTENT_DIRTY, GFP_NOFS);
  660. set_state_private(failure_tree, start,
  661. (u64)(unsigned long)failrec);
  662. } else {
  663. failrec = (struct io_failure_record *)(unsigned long)private;
  664. }
  665. num_copies = btrfs_num_copies(
  666. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  667. failrec->logical, failrec->len);
  668. failrec->last_mirror++;
  669. if (!state) {
  670. spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
  671. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  672. failrec->start,
  673. EXTENT_LOCKED);
  674. if (state && state->start != failrec->start)
  675. state = NULL;
  676. spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
  677. }
  678. if (!state || failrec->last_mirror > num_copies) {
  679. set_state_private(failure_tree, failrec->start, 0);
  680. clear_extent_bits(failure_tree, failrec->start,
  681. failrec->start + failrec->len - 1,
  682. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  683. kfree(failrec);
  684. return -EIO;
  685. }
  686. bio = bio_alloc(GFP_NOFS, 1);
  687. bio->bi_private = state;
  688. bio->bi_end_io = failed_bio->bi_end_io;
  689. bio->bi_sector = failrec->logical >> 9;
  690. bio->bi_bdev = failed_bio->bi_bdev;
  691. bio->bi_size = 0;
  692. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  693. if (failed_bio->bi_rw & (1 << BIO_RW))
  694. rw = WRITE;
  695. else
  696. rw = READ;
  697. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  698. failrec->last_mirror);
  699. return 0;
  700. }
  701. /*
  702. * each time an IO finishes, we do a fast check in the IO failure tree
  703. * to see if we need to process or clean up an io_failure_record
  704. */
  705. int btrfs_clean_io_failures(struct inode *inode, u64 start)
  706. {
  707. u64 private;
  708. u64 private_failure;
  709. struct io_failure_record *failure;
  710. int ret;
  711. private = 0;
  712. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  713. (u64)-1, 1, EXTENT_DIRTY)) {
  714. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  715. start, &private_failure);
  716. if (ret == 0) {
  717. failure = (struct io_failure_record *)(unsigned long)
  718. private_failure;
  719. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  720. failure->start, 0);
  721. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  722. failure->start,
  723. failure->start + failure->len - 1,
  724. EXTENT_DIRTY | EXTENT_LOCKED,
  725. GFP_NOFS);
  726. kfree(failure);
  727. }
  728. }
  729. return 0;
  730. }
  731. /*
  732. * when reads are done, we need to check csums to verify the data is correct
  733. * if there's a match, we allow the bio to finish. If not, we go through
  734. * the io_failure_record routines to find good copies
  735. */
  736. int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  737. struct extent_state *state)
  738. {
  739. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  740. struct inode *inode = page->mapping->host;
  741. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  742. char *kaddr;
  743. u64 private = ~(u32)0;
  744. int ret;
  745. struct btrfs_root *root = BTRFS_I(inode)->root;
  746. u32 csum = ~(u32)0;
  747. unsigned long flags;
  748. if (btrfs_test_opt(root, NODATASUM) ||
  749. btrfs_test_flag(inode, NODATASUM))
  750. return 0;
  751. if (state && state->start == start) {
  752. private = state->private;
  753. ret = 0;
  754. } else {
  755. ret = get_state_private(io_tree, start, &private);
  756. }
  757. local_irq_save(flags);
  758. kaddr = kmap_atomic(page, KM_IRQ0);
  759. if (ret) {
  760. goto zeroit;
  761. }
  762. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  763. btrfs_csum_final(csum, (char *)&csum);
  764. if (csum != private) {
  765. goto zeroit;
  766. }
  767. kunmap_atomic(kaddr, KM_IRQ0);
  768. local_irq_restore(flags);
  769. /* if the io failure tree for this inode is non-empty,
  770. * check to see if we've recovered from a failed IO
  771. */
  772. btrfs_clean_io_failures(inode, start);
  773. return 0;
  774. zeroit:
  775. printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
  776. page->mapping->host->i_ino, (unsigned long long)start, csum,
  777. private);
  778. memset(kaddr + offset, 1, end - start + 1);
  779. flush_dcache_page(page);
  780. kunmap_atomic(kaddr, KM_IRQ0);
  781. local_irq_restore(flags);
  782. if (private == 0)
  783. return 0;
  784. return -EIO;
  785. }
  786. /*
  787. * This creates an orphan entry for the given inode in case something goes
  788. * wrong in the middle of an unlink/truncate.
  789. */
  790. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  791. {
  792. struct btrfs_root *root = BTRFS_I(inode)->root;
  793. int ret = 0;
  794. spin_lock(&root->list_lock);
  795. /* already on the orphan list, we're good */
  796. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  797. spin_unlock(&root->list_lock);
  798. return 0;
  799. }
  800. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  801. spin_unlock(&root->list_lock);
  802. /*
  803. * insert an orphan item to track this unlinked/truncated file
  804. */
  805. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  806. return ret;
  807. }
  808. /*
  809. * We have done the truncate/delete so we can go ahead and remove the orphan
  810. * item for this particular inode.
  811. */
  812. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  813. {
  814. struct btrfs_root *root = BTRFS_I(inode)->root;
  815. int ret = 0;
  816. spin_lock(&root->list_lock);
  817. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  818. spin_unlock(&root->list_lock);
  819. return 0;
  820. }
  821. list_del_init(&BTRFS_I(inode)->i_orphan);
  822. if (!trans) {
  823. spin_unlock(&root->list_lock);
  824. return 0;
  825. }
  826. spin_unlock(&root->list_lock);
  827. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  828. return ret;
  829. }
  830. /*
  831. * this cleans up any orphans that may be left on the list from the last use
  832. * of this root.
  833. */
  834. void btrfs_orphan_cleanup(struct btrfs_root *root)
  835. {
  836. struct btrfs_path *path;
  837. struct extent_buffer *leaf;
  838. struct btrfs_item *item;
  839. struct btrfs_key key, found_key;
  840. struct btrfs_trans_handle *trans;
  841. struct inode *inode;
  842. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  843. /* don't do orphan cleanup if the fs is readonly. */
  844. if (root->fs_info->sb->s_flags & MS_RDONLY)
  845. return;
  846. path = btrfs_alloc_path();
  847. if (!path)
  848. return;
  849. path->reada = -1;
  850. key.objectid = BTRFS_ORPHAN_OBJECTID;
  851. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  852. key.offset = (u64)-1;
  853. while (1) {
  854. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  855. if (ret < 0) {
  856. printk(KERN_ERR "Error searching slot for orphan: %d"
  857. "\n", ret);
  858. break;
  859. }
  860. /*
  861. * if ret == 0 means we found what we were searching for, which
  862. * is weird, but possible, so only screw with path if we didnt
  863. * find the key and see if we have stuff that matches
  864. */
  865. if (ret > 0) {
  866. if (path->slots[0] == 0)
  867. break;
  868. path->slots[0]--;
  869. }
  870. /* pull out the item */
  871. leaf = path->nodes[0];
  872. item = btrfs_item_nr(leaf, path->slots[0]);
  873. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  874. /* make sure the item matches what we want */
  875. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  876. break;
  877. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  878. break;
  879. /* release the path since we're done with it */
  880. btrfs_release_path(root, path);
  881. /*
  882. * this is where we are basically btrfs_lookup, without the
  883. * crossing root thing. we store the inode number in the
  884. * offset of the orphan item.
  885. */
  886. inode = btrfs_iget_locked(root->fs_info->sb,
  887. found_key.offset, root);
  888. if (!inode)
  889. break;
  890. if (inode->i_state & I_NEW) {
  891. BTRFS_I(inode)->root = root;
  892. /* have to set the location manually */
  893. BTRFS_I(inode)->location.objectid = inode->i_ino;
  894. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  895. BTRFS_I(inode)->location.offset = 0;
  896. btrfs_read_locked_inode(inode);
  897. unlock_new_inode(inode);
  898. }
  899. /*
  900. * add this inode to the orphan list so btrfs_orphan_del does
  901. * the proper thing when we hit it
  902. */
  903. spin_lock(&root->list_lock);
  904. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  905. spin_unlock(&root->list_lock);
  906. /*
  907. * if this is a bad inode, means we actually succeeded in
  908. * removing the inode, but not the orphan record, which means
  909. * we need to manually delete the orphan since iput will just
  910. * do a destroy_inode
  911. */
  912. if (is_bad_inode(inode)) {
  913. trans = btrfs_start_transaction(root, 1);
  914. btrfs_orphan_del(trans, inode);
  915. btrfs_end_transaction(trans, root);
  916. iput(inode);
  917. continue;
  918. }
  919. /* if we have links, this was a truncate, lets do that */
  920. if (inode->i_nlink) {
  921. nr_truncate++;
  922. btrfs_truncate(inode);
  923. } else {
  924. nr_unlink++;
  925. }
  926. /* this will do delete_inode and everything for us */
  927. iput(inode);
  928. }
  929. if (nr_unlink)
  930. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  931. if (nr_truncate)
  932. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  933. btrfs_free_path(path);
  934. }
  935. /*
  936. * read an inode from the btree into the in-memory inode
  937. */
  938. void btrfs_read_locked_inode(struct inode *inode)
  939. {
  940. struct btrfs_path *path;
  941. struct extent_buffer *leaf;
  942. struct btrfs_inode_item *inode_item;
  943. struct btrfs_timespec *tspec;
  944. struct btrfs_root *root = BTRFS_I(inode)->root;
  945. struct btrfs_key location;
  946. u64 alloc_group_block;
  947. u32 rdev;
  948. int ret;
  949. path = btrfs_alloc_path();
  950. BUG_ON(!path);
  951. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  952. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  953. if (ret)
  954. goto make_bad;
  955. leaf = path->nodes[0];
  956. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  957. struct btrfs_inode_item);
  958. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  959. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  960. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  961. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  962. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  963. tspec = btrfs_inode_atime(inode_item);
  964. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  965. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  966. tspec = btrfs_inode_mtime(inode_item);
  967. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  968. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  969. tspec = btrfs_inode_ctime(inode_item);
  970. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  971. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  972. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  973. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  974. inode->i_generation = BTRFS_I(inode)->generation;
  975. inode->i_rdev = 0;
  976. rdev = btrfs_inode_rdev(leaf, inode_item);
  977. BTRFS_I(inode)->index_cnt = (u64)-1;
  978. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  979. BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
  980. alloc_group_block);
  981. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  982. if (!BTRFS_I(inode)->block_group) {
  983. BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
  984. NULL, 0,
  985. BTRFS_BLOCK_GROUP_METADATA, 0);
  986. }
  987. btrfs_free_path(path);
  988. inode_item = NULL;
  989. switch (inode->i_mode & S_IFMT) {
  990. case S_IFREG:
  991. inode->i_mapping->a_ops = &btrfs_aops;
  992. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  993. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  994. inode->i_fop = &btrfs_file_operations;
  995. inode->i_op = &btrfs_file_inode_operations;
  996. break;
  997. case S_IFDIR:
  998. inode->i_fop = &btrfs_dir_file_operations;
  999. if (root == root->fs_info->tree_root)
  1000. inode->i_op = &btrfs_dir_ro_inode_operations;
  1001. else
  1002. inode->i_op = &btrfs_dir_inode_operations;
  1003. break;
  1004. case S_IFLNK:
  1005. inode->i_op = &btrfs_symlink_inode_operations;
  1006. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  1007. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1008. break;
  1009. default:
  1010. init_special_inode(inode, inode->i_mode, rdev);
  1011. break;
  1012. }
  1013. return;
  1014. make_bad:
  1015. btrfs_free_path(path);
  1016. make_bad_inode(inode);
  1017. }
  1018. /*
  1019. * given a leaf and an inode, copy the inode fields into the leaf
  1020. */
  1021. static void fill_inode_item(struct btrfs_trans_handle *trans,
  1022. struct extent_buffer *leaf,
  1023. struct btrfs_inode_item *item,
  1024. struct inode *inode)
  1025. {
  1026. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  1027. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  1028. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  1029. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  1030. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  1031. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  1032. inode->i_atime.tv_sec);
  1033. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  1034. inode->i_atime.tv_nsec);
  1035. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  1036. inode->i_mtime.tv_sec);
  1037. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  1038. inode->i_mtime.tv_nsec);
  1039. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  1040. inode->i_ctime.tv_sec);
  1041. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  1042. inode->i_ctime.tv_nsec);
  1043. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  1044. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  1045. btrfs_set_inode_transid(leaf, item, trans->transid);
  1046. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  1047. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  1048. btrfs_set_inode_block_group(leaf, item,
  1049. BTRFS_I(inode)->block_group->key.objectid);
  1050. }
  1051. /*
  1052. * copy everything in the in-memory inode into the btree.
  1053. */
  1054. int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
  1055. struct btrfs_root *root,
  1056. struct inode *inode)
  1057. {
  1058. struct btrfs_inode_item *inode_item;
  1059. struct btrfs_path *path;
  1060. struct extent_buffer *leaf;
  1061. int ret;
  1062. path = btrfs_alloc_path();
  1063. BUG_ON(!path);
  1064. ret = btrfs_lookup_inode(trans, root, path,
  1065. &BTRFS_I(inode)->location, 1);
  1066. if (ret) {
  1067. if (ret > 0)
  1068. ret = -ENOENT;
  1069. goto failed;
  1070. }
  1071. leaf = path->nodes[0];
  1072. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1073. struct btrfs_inode_item);
  1074. fill_inode_item(trans, leaf, inode_item, inode);
  1075. btrfs_mark_buffer_dirty(leaf);
  1076. btrfs_set_inode_last_trans(trans, inode);
  1077. ret = 0;
  1078. failed:
  1079. btrfs_free_path(path);
  1080. return ret;
  1081. }
  1082. /*
  1083. * unlink helper that gets used here in inode.c and in the tree logging
  1084. * recovery code. It remove a link in a directory with a given name, and
  1085. * also drops the back refs in the inode to the directory
  1086. */
  1087. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  1088. struct btrfs_root *root,
  1089. struct inode *dir, struct inode *inode,
  1090. const char *name, int name_len)
  1091. {
  1092. struct btrfs_path *path;
  1093. int ret = 0;
  1094. struct extent_buffer *leaf;
  1095. struct btrfs_dir_item *di;
  1096. struct btrfs_key key;
  1097. u64 index;
  1098. path = btrfs_alloc_path();
  1099. if (!path) {
  1100. ret = -ENOMEM;
  1101. goto err;
  1102. }
  1103. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  1104. name, name_len, -1);
  1105. if (IS_ERR(di)) {
  1106. ret = PTR_ERR(di);
  1107. goto err;
  1108. }
  1109. if (!di) {
  1110. ret = -ENOENT;
  1111. goto err;
  1112. }
  1113. leaf = path->nodes[0];
  1114. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  1115. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  1116. if (ret)
  1117. goto err;
  1118. btrfs_release_path(root, path);
  1119. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  1120. inode->i_ino,
  1121. dir->i_ino, &index);
  1122. if (ret) {
  1123. printk("failed to delete reference to %.*s, "
  1124. "inode %lu parent %lu\n", name_len, name,
  1125. inode->i_ino, dir->i_ino);
  1126. goto err;
  1127. }
  1128. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  1129. index, name, name_len, -1);
  1130. if (IS_ERR(di)) {
  1131. ret = PTR_ERR(di);
  1132. goto err;
  1133. }
  1134. if (!di) {
  1135. ret = -ENOENT;
  1136. goto err;
  1137. }
  1138. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  1139. btrfs_release_path(root, path);
  1140. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  1141. inode, dir->i_ino);
  1142. BUG_ON(ret != 0 && ret != -ENOENT);
  1143. if (ret != -ENOENT)
  1144. BTRFS_I(dir)->log_dirty_trans = trans->transid;
  1145. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  1146. dir, index);
  1147. BUG_ON(ret);
  1148. err:
  1149. btrfs_free_path(path);
  1150. if (ret)
  1151. goto out;
  1152. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  1153. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  1154. btrfs_update_inode(trans, root, dir);
  1155. btrfs_drop_nlink(inode);
  1156. ret = btrfs_update_inode(trans, root, inode);
  1157. dir->i_sb->s_dirt = 1;
  1158. out:
  1159. return ret;
  1160. }
  1161. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  1162. {
  1163. struct btrfs_root *root;
  1164. struct btrfs_trans_handle *trans;
  1165. struct inode *inode = dentry->d_inode;
  1166. int ret;
  1167. unsigned long nr = 0;
  1168. root = BTRFS_I(dir)->root;
  1169. ret = btrfs_check_free_space(root, 1, 1);
  1170. if (ret)
  1171. goto fail;
  1172. trans = btrfs_start_transaction(root, 1);
  1173. btrfs_set_trans_block_group(trans, dir);
  1174. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  1175. dentry->d_name.name, dentry->d_name.len);
  1176. if (inode->i_nlink == 0)
  1177. ret = btrfs_orphan_add(trans, inode);
  1178. nr = trans->blocks_used;
  1179. btrfs_end_transaction_throttle(trans, root);
  1180. fail:
  1181. btrfs_btree_balance_dirty(root, nr);
  1182. return ret;
  1183. }
  1184. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  1185. {
  1186. struct inode *inode = dentry->d_inode;
  1187. int err = 0;
  1188. int ret;
  1189. struct btrfs_root *root = BTRFS_I(dir)->root;
  1190. struct btrfs_trans_handle *trans;
  1191. unsigned long nr = 0;
  1192. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
  1193. return -ENOTEMPTY;
  1194. }
  1195. ret = btrfs_check_free_space(root, 1, 1);
  1196. if (ret)
  1197. goto fail;
  1198. trans = btrfs_start_transaction(root, 1);
  1199. btrfs_set_trans_block_group(trans, dir);
  1200. err = btrfs_orphan_add(trans, inode);
  1201. if (err)
  1202. goto fail_trans;
  1203. /* now the directory is empty */
  1204. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  1205. dentry->d_name.name, dentry->d_name.len);
  1206. if (!err) {
  1207. btrfs_i_size_write(inode, 0);
  1208. }
  1209. fail_trans:
  1210. nr = trans->blocks_used;
  1211. ret = btrfs_end_transaction_throttle(trans, root);
  1212. fail:
  1213. btrfs_btree_balance_dirty(root, nr);
  1214. if (ret && !err)
  1215. err = ret;
  1216. return err;
  1217. }
  1218. /*
  1219. * when truncating bytes in a file, it is possible to avoid reading
  1220. * the leaves that contain only checksum items. This can be the
  1221. * majority of the IO required to delete a large file, but it must
  1222. * be done carefully.
  1223. *
  1224. * The keys in the level just above the leaves are checked to make sure
  1225. * the lowest key in a given leaf is a csum key, and starts at an offset
  1226. * after the new size.
  1227. *
  1228. * Then the key for the next leaf is checked to make sure it also has
  1229. * a checksum item for the same file. If it does, we know our target leaf
  1230. * contains only checksum items, and it can be safely freed without reading
  1231. * it.
  1232. *
  1233. * This is just an optimization targeted at large files. It may do
  1234. * nothing. It will return 0 unless things went badly.
  1235. */
  1236. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  1237. struct btrfs_root *root,
  1238. struct btrfs_path *path,
  1239. struct inode *inode, u64 new_size)
  1240. {
  1241. struct btrfs_key key;
  1242. int ret;
  1243. int nritems;
  1244. struct btrfs_key found_key;
  1245. struct btrfs_key other_key;
  1246. path->lowest_level = 1;
  1247. key.objectid = inode->i_ino;
  1248. key.type = BTRFS_CSUM_ITEM_KEY;
  1249. key.offset = new_size;
  1250. again:
  1251. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1252. if (ret < 0)
  1253. goto out;
  1254. if (path->nodes[1] == NULL) {
  1255. ret = 0;
  1256. goto out;
  1257. }
  1258. ret = 0;
  1259. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  1260. nritems = btrfs_header_nritems(path->nodes[1]);
  1261. if (!nritems)
  1262. goto out;
  1263. if (path->slots[1] >= nritems)
  1264. goto next_node;
  1265. /* did we find a key greater than anything we want to delete? */
  1266. if (found_key.objectid > inode->i_ino ||
  1267. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  1268. goto out;
  1269. /* we check the next key in the node to make sure the leave contains
  1270. * only checksum items. This comparison doesn't work if our
  1271. * leaf is the last one in the node
  1272. */
  1273. if (path->slots[1] + 1 >= nritems) {
  1274. next_node:
  1275. /* search forward from the last key in the node, this
  1276. * will bring us into the next node in the tree
  1277. */
  1278. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  1279. /* unlikely, but we inc below, so check to be safe */
  1280. if (found_key.offset == (u64)-1)
  1281. goto out;
  1282. /* search_forward needs a path with locks held, do the
  1283. * search again for the original key. It is possible
  1284. * this will race with a balance and return a path that
  1285. * we could modify, but this drop is just an optimization
  1286. * and is allowed to miss some leaves.
  1287. */
  1288. btrfs_release_path(root, path);
  1289. found_key.offset++;
  1290. /* setup a max key for search_forward */
  1291. other_key.offset = (u64)-1;
  1292. other_key.type = key.type;
  1293. other_key.objectid = key.objectid;
  1294. path->keep_locks = 1;
  1295. ret = btrfs_search_forward(root, &found_key, &other_key,
  1296. path, 0, 0);
  1297. path->keep_locks = 0;
  1298. if (ret || found_key.objectid != key.objectid ||
  1299. found_key.type != key.type) {
  1300. ret = 0;
  1301. goto out;
  1302. }
  1303. key.offset = found_key.offset;
  1304. btrfs_release_path(root, path);
  1305. cond_resched();
  1306. goto again;
  1307. }
  1308. /* we know there's one more slot after us in the tree,
  1309. * read that key so we can verify it is also a checksum item
  1310. */
  1311. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  1312. if (found_key.objectid < inode->i_ino)
  1313. goto next_key;
  1314. if (found_key.type != key.type || found_key.offset < new_size)
  1315. goto next_key;
  1316. /*
  1317. * if the key for the next leaf isn't a csum key from this objectid,
  1318. * we can't be sure there aren't good items inside this leaf.
  1319. * Bail out
  1320. */
  1321. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  1322. goto out;
  1323. /*
  1324. * it is safe to delete this leaf, it contains only
  1325. * csum items from this inode at an offset >= new_size
  1326. */
  1327. ret = btrfs_del_leaf(trans, root, path,
  1328. btrfs_node_blockptr(path->nodes[1],
  1329. path->slots[1]));
  1330. BUG_ON(ret);
  1331. next_key:
  1332. btrfs_release_path(root, path);
  1333. if (other_key.objectid == inode->i_ino &&
  1334. other_key.type == key.type && other_key.offset > key.offset) {
  1335. key.offset = other_key.offset;
  1336. cond_resched();
  1337. goto again;
  1338. }
  1339. ret = 0;
  1340. out:
  1341. /* fixup any changes we've made to the path */
  1342. path->lowest_level = 0;
  1343. path->keep_locks = 0;
  1344. btrfs_release_path(root, path);
  1345. return ret;
  1346. }
  1347. /*
  1348. * this can truncate away extent items, csum items and directory items.
  1349. * It starts at a high offset and removes keys until it can't find
  1350. * any higher than new_size
  1351. *
  1352. * csum items that cross the new i_size are truncated to the new size
  1353. * as well.
  1354. *
  1355. * min_type is the minimum key type to truncate down to. If set to 0, this
  1356. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  1357. */
  1358. noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  1359. struct btrfs_root *root,
  1360. struct inode *inode,
  1361. u64 new_size, u32 min_type)
  1362. {
  1363. int ret;
  1364. struct btrfs_path *path;
  1365. struct btrfs_key key;
  1366. struct btrfs_key found_key;
  1367. u32 found_type;
  1368. struct extent_buffer *leaf;
  1369. struct btrfs_file_extent_item *fi;
  1370. u64 extent_start = 0;
  1371. u64 extent_num_bytes = 0;
  1372. u64 item_end = 0;
  1373. u64 root_gen = 0;
  1374. u64 root_owner = 0;
  1375. int found_extent;
  1376. int del_item;
  1377. int pending_del_nr = 0;
  1378. int pending_del_slot = 0;
  1379. int extent_type = -1;
  1380. u64 mask = root->sectorsize - 1;
  1381. if (root->ref_cows)
  1382. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  1383. path = btrfs_alloc_path();
  1384. path->reada = -1;
  1385. BUG_ON(!path);
  1386. /* FIXME, add redo link to tree so we don't leak on crash */
  1387. key.objectid = inode->i_ino;
  1388. key.offset = (u64)-1;
  1389. key.type = (u8)-1;
  1390. btrfs_init_path(path);
  1391. ret = drop_csum_leaves(trans, root, path, inode, new_size);
  1392. BUG_ON(ret);
  1393. search_again:
  1394. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1395. if (ret < 0) {
  1396. goto error;
  1397. }
  1398. if (ret > 0) {
  1399. /* there are no items in the tree for us to truncate, we're
  1400. * done
  1401. */
  1402. if (path->slots[0] == 0) {
  1403. ret = 0;
  1404. goto error;
  1405. }
  1406. path->slots[0]--;
  1407. }
  1408. while(1) {
  1409. fi = NULL;
  1410. leaf = path->nodes[0];
  1411. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1412. found_type = btrfs_key_type(&found_key);
  1413. if (found_key.objectid != inode->i_ino)
  1414. break;
  1415. if (found_type < min_type)
  1416. break;
  1417. item_end = found_key.offset;
  1418. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  1419. fi = btrfs_item_ptr(leaf, path->slots[0],
  1420. struct btrfs_file_extent_item);
  1421. extent_type = btrfs_file_extent_type(leaf, fi);
  1422. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  1423. item_end +=
  1424. btrfs_file_extent_num_bytes(leaf, fi);
  1425. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1426. struct btrfs_item *item = btrfs_item_nr(leaf,
  1427. path->slots[0]);
  1428. item_end += btrfs_file_extent_inline_len(leaf,
  1429. item);
  1430. }
  1431. item_end--;
  1432. }
  1433. if (found_type == BTRFS_CSUM_ITEM_KEY) {
  1434. ret = btrfs_csum_truncate(trans, root, path,
  1435. new_size);
  1436. BUG_ON(ret);
  1437. }
  1438. if (item_end < new_size) {
  1439. if (found_type == BTRFS_DIR_ITEM_KEY) {
  1440. found_type = BTRFS_INODE_ITEM_KEY;
  1441. } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
  1442. found_type = BTRFS_CSUM_ITEM_KEY;
  1443. } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
  1444. found_type = BTRFS_XATTR_ITEM_KEY;
  1445. } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
  1446. found_type = BTRFS_INODE_REF_KEY;
  1447. } else if (found_type) {
  1448. found_type--;
  1449. } else {
  1450. break;
  1451. }
  1452. btrfs_set_key_type(&key, found_type);
  1453. goto next;
  1454. }
  1455. if (found_key.offset >= new_size)
  1456. del_item = 1;
  1457. else
  1458. del_item = 0;
  1459. found_extent = 0;
  1460. /* FIXME, shrink the extent if the ref count is only 1 */
  1461. if (found_type != BTRFS_EXTENT_DATA_KEY)
  1462. goto delete;
  1463. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  1464. u64 num_dec;
  1465. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  1466. if (!del_item) {
  1467. u64 orig_num_bytes =
  1468. btrfs_file_extent_num_bytes(leaf, fi);
  1469. extent_num_bytes = new_size -
  1470. found_key.offset + root->sectorsize - 1;
  1471. extent_num_bytes = extent_num_bytes &
  1472. ~((u64)root->sectorsize - 1);
  1473. btrfs_set_file_extent_num_bytes(leaf, fi,
  1474. extent_num_bytes);
  1475. num_dec = (orig_num_bytes -
  1476. extent_num_bytes);
  1477. if (root->ref_cows && extent_start != 0)
  1478. inode_sub_bytes(inode, num_dec);
  1479. btrfs_mark_buffer_dirty(leaf);
  1480. } else {
  1481. extent_num_bytes =
  1482. btrfs_file_extent_disk_num_bytes(leaf,
  1483. fi);
  1484. /* FIXME blocksize != 4096 */
  1485. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  1486. if (extent_start != 0) {
  1487. found_extent = 1;
  1488. if (root->ref_cows)
  1489. inode_sub_bytes(inode, num_dec);
  1490. }
  1491. root_gen = btrfs_header_generation(leaf);
  1492. root_owner = btrfs_header_owner(leaf);
  1493. }
  1494. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  1495. if (!del_item) {
  1496. u32 size = new_size - found_key.offset;
  1497. if (root->ref_cows) {
  1498. inode_sub_bytes(inode, item_end + 1 -
  1499. new_size);
  1500. }
  1501. size =
  1502. btrfs_file_extent_calc_inline_size(size);
  1503. ret = btrfs_truncate_item(trans, root, path,
  1504. size, 1);
  1505. BUG_ON(ret);
  1506. } else if (root->ref_cows) {
  1507. inode_sub_bytes(inode, item_end + 1 -
  1508. found_key.offset);
  1509. }
  1510. }
  1511. delete:
  1512. if (del_item) {
  1513. if (!pending_del_nr) {
  1514. /* no pending yet, add ourselves */
  1515. pending_del_slot = path->slots[0];
  1516. pending_del_nr = 1;
  1517. } else if (pending_del_nr &&
  1518. path->slots[0] + 1 == pending_del_slot) {
  1519. /* hop on the pending chunk */
  1520. pending_del_nr++;
  1521. pending_del_slot = path->slots[0];
  1522. } else {
  1523. printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
  1524. }
  1525. } else {
  1526. break;
  1527. }
  1528. if (found_extent) {
  1529. ret = btrfs_free_extent(trans, root, extent_start,
  1530. extent_num_bytes,
  1531. leaf->start, root_owner,
  1532. root_gen, inode->i_ino, 0);
  1533. BUG_ON(ret);
  1534. }
  1535. next:
  1536. if (path->slots[0] == 0) {
  1537. if (pending_del_nr)
  1538. goto del_pending;
  1539. btrfs_release_path(root, path);
  1540. goto search_again;
  1541. }
  1542. path->slots[0]--;
  1543. if (pending_del_nr &&
  1544. path->slots[0] + 1 != pending_del_slot) {
  1545. struct btrfs_key debug;
  1546. del_pending:
  1547. btrfs_item_key_to_cpu(path->nodes[0], &debug,
  1548. pending_del_slot);
  1549. ret = btrfs_del_items(trans, root, path,
  1550. pending_del_slot,
  1551. pending_del_nr);
  1552. BUG_ON(ret);
  1553. pending_del_nr = 0;
  1554. btrfs_release_path(root, path);
  1555. goto search_again;
  1556. }
  1557. }
  1558. ret = 0;
  1559. error:
  1560. if (pending_del_nr) {
  1561. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  1562. pending_del_nr);
  1563. }
  1564. btrfs_free_path(path);
  1565. inode->i_sb->s_dirt = 1;
  1566. return ret;
  1567. }
  1568. /*
  1569. * taken from block_truncate_page, but does cow as it zeros out
  1570. * any bytes left in the last page in the file.
  1571. */
  1572. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  1573. {
  1574. struct inode *inode = mapping->host;
  1575. struct btrfs_root *root = BTRFS_I(inode)->root;
  1576. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1577. struct btrfs_ordered_extent *ordered;
  1578. char *kaddr;
  1579. u32 blocksize = root->sectorsize;
  1580. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  1581. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  1582. struct page *page;
  1583. int ret = 0;
  1584. u64 page_start;
  1585. u64 page_end;
  1586. if ((offset & (blocksize - 1)) == 0)
  1587. goto out;
  1588. ret = -ENOMEM;
  1589. again:
  1590. page = grab_cache_page(mapping, index);
  1591. if (!page)
  1592. goto out;
  1593. page_start = page_offset(page);
  1594. page_end = page_start + PAGE_CACHE_SIZE - 1;
  1595. if (!PageUptodate(page)) {
  1596. ret = btrfs_readpage(NULL, page);
  1597. lock_page(page);
  1598. if (page->mapping != mapping) {
  1599. unlock_page(page);
  1600. page_cache_release(page);
  1601. goto again;
  1602. }
  1603. if (!PageUptodate(page)) {
  1604. ret = -EIO;
  1605. goto out_unlock;
  1606. }
  1607. }
  1608. wait_on_page_writeback(page);
  1609. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  1610. set_page_extent_mapped(page);
  1611. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1612. if (ordered) {
  1613. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  1614. unlock_page(page);
  1615. page_cache_release(page);
  1616. btrfs_start_ordered_extent(inode, ordered, 1);
  1617. btrfs_put_ordered_extent(ordered);
  1618. goto again;
  1619. }
  1620. btrfs_set_extent_delalloc(inode, page_start, page_end);
  1621. ret = 0;
  1622. if (offset != PAGE_CACHE_SIZE) {
  1623. kaddr = kmap(page);
  1624. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  1625. flush_dcache_page(page);
  1626. kunmap(page);
  1627. }
  1628. ClearPageChecked(page);
  1629. set_page_dirty(page);
  1630. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  1631. out_unlock:
  1632. unlock_page(page);
  1633. page_cache_release(page);
  1634. out:
  1635. return ret;
  1636. }
  1637. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  1638. {
  1639. struct inode *inode = dentry->d_inode;
  1640. int err;
  1641. err = inode_change_ok(inode, attr);
  1642. if (err)
  1643. return err;
  1644. if (S_ISREG(inode->i_mode) &&
  1645. attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
  1646. struct btrfs_trans_handle *trans;
  1647. struct btrfs_root *root = BTRFS_I(inode)->root;
  1648. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1649. u64 mask = root->sectorsize - 1;
  1650. u64 hole_start = (inode->i_size + mask) & ~mask;
  1651. u64 block_end = (attr->ia_size + mask) & ~mask;
  1652. u64 hole_size;
  1653. u64 alloc_hint = 0;
  1654. if (attr->ia_size <= hole_start)
  1655. goto out;
  1656. err = btrfs_check_free_space(root, 1, 0);
  1657. if (err)
  1658. goto fail;
  1659. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  1660. hole_size = block_end - hole_start;
  1661. while(1) {
  1662. struct btrfs_ordered_extent *ordered;
  1663. btrfs_wait_ordered_range(inode, hole_start, hole_size);
  1664. lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  1665. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  1666. if (ordered) {
  1667. unlock_extent(io_tree, hole_start,
  1668. block_end - 1, GFP_NOFS);
  1669. btrfs_put_ordered_extent(ordered);
  1670. } else {
  1671. break;
  1672. }
  1673. }
  1674. trans = btrfs_start_transaction(root, 1);
  1675. btrfs_set_trans_block_group(trans, inode);
  1676. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  1677. err = btrfs_drop_extents(trans, root, inode,
  1678. hole_start, block_end, hole_start,
  1679. &alloc_hint);
  1680. if (alloc_hint != EXTENT_MAP_INLINE) {
  1681. err = btrfs_insert_file_extent(trans, root,
  1682. inode->i_ino,
  1683. hole_start, 0, 0,
  1684. hole_size, 0);
  1685. btrfs_drop_extent_cache(inode, hole_start,
  1686. (u64)-1, 0);
  1687. btrfs_check_file(root, inode);
  1688. }
  1689. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  1690. btrfs_end_transaction(trans, root);
  1691. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  1692. if (err)
  1693. return err;
  1694. }
  1695. out:
  1696. err = inode_setattr(inode, attr);
  1697. if (!err && ((attr->ia_valid & ATTR_MODE)))
  1698. err = btrfs_acl_chmod(inode);
  1699. fail:
  1700. return err;
  1701. }
  1702. void btrfs_delete_inode(struct inode *inode)
  1703. {
  1704. struct btrfs_trans_handle *trans;
  1705. struct btrfs_root *root = BTRFS_I(inode)->root;
  1706. unsigned long nr;
  1707. int ret;
  1708. truncate_inode_pages(&inode->i_data, 0);
  1709. if (is_bad_inode(inode)) {
  1710. btrfs_orphan_del(NULL, inode);
  1711. goto no_delete;
  1712. }
  1713. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  1714. btrfs_i_size_write(inode, 0);
  1715. trans = btrfs_start_transaction(root, 1);
  1716. btrfs_set_trans_block_group(trans, inode);
  1717. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
  1718. if (ret) {
  1719. btrfs_orphan_del(NULL, inode);
  1720. goto no_delete_lock;
  1721. }
  1722. btrfs_orphan_del(trans, inode);
  1723. nr = trans->blocks_used;
  1724. clear_inode(inode);
  1725. btrfs_end_transaction(trans, root);
  1726. btrfs_btree_balance_dirty(root, nr);
  1727. return;
  1728. no_delete_lock:
  1729. nr = trans->blocks_used;
  1730. btrfs_end_transaction(trans, root);
  1731. btrfs_btree_balance_dirty(root, nr);
  1732. no_delete:
  1733. clear_inode(inode);
  1734. }
  1735. /*
  1736. * this returns the key found in the dir entry in the location pointer.
  1737. * If no dir entries were found, location->objectid is 0.
  1738. */
  1739. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  1740. struct btrfs_key *location)
  1741. {
  1742. const char *name = dentry->d_name.name;
  1743. int namelen = dentry->d_name.len;
  1744. struct btrfs_dir_item *di;
  1745. struct btrfs_path *path;
  1746. struct btrfs_root *root = BTRFS_I(dir)->root;
  1747. int ret = 0;
  1748. path = btrfs_alloc_path();
  1749. BUG_ON(!path);
  1750. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  1751. namelen, 0);
  1752. if (IS_ERR(di))
  1753. ret = PTR_ERR(di);
  1754. if (!di || IS_ERR(di)) {
  1755. goto out_err;
  1756. }
  1757. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  1758. out:
  1759. btrfs_free_path(path);
  1760. return ret;
  1761. out_err:
  1762. location->objectid = 0;
  1763. goto out;
  1764. }
  1765. /*
  1766. * when we hit a tree root in a directory, the btrfs part of the inode
  1767. * needs to be changed to reflect the root directory of the tree root. This
  1768. * is kind of like crossing a mount point.
  1769. */
  1770. static int fixup_tree_root_location(struct btrfs_root *root,
  1771. struct btrfs_key *location,
  1772. struct btrfs_root **sub_root,
  1773. struct dentry *dentry)
  1774. {
  1775. struct btrfs_root_item *ri;
  1776. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  1777. return 0;
  1778. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  1779. return 0;
  1780. *sub_root = btrfs_read_fs_root(root->fs_info, location,
  1781. dentry->d_name.name,
  1782. dentry->d_name.len);
  1783. if (IS_ERR(*sub_root))
  1784. return PTR_ERR(*sub_root);
  1785. ri = &(*sub_root)->root_item;
  1786. location->objectid = btrfs_root_dirid(ri);
  1787. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  1788. location->offset = 0;
  1789. return 0;
  1790. }
  1791. static noinline void init_btrfs_i(struct inode *inode)
  1792. {
  1793. struct btrfs_inode *bi = BTRFS_I(inode);
  1794. bi->i_acl = NULL;
  1795. bi->i_default_acl = NULL;
  1796. bi->generation = 0;
  1797. bi->last_trans = 0;
  1798. bi->logged_trans = 0;
  1799. bi->delalloc_bytes = 0;
  1800. bi->disk_i_size = 0;
  1801. bi->flags = 0;
  1802. bi->index_cnt = (u64)-1;
  1803. bi->log_dirty_trans = 0;
  1804. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  1805. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  1806. inode->i_mapping, GFP_NOFS);
  1807. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  1808. inode->i_mapping, GFP_NOFS);
  1809. INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
  1810. btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
  1811. mutex_init(&BTRFS_I(inode)->csum_mutex);
  1812. mutex_init(&BTRFS_I(inode)->extent_mutex);
  1813. mutex_init(&BTRFS_I(inode)->log_mutex);
  1814. }
  1815. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  1816. {
  1817. struct btrfs_iget_args *args = p;
  1818. inode->i_ino = args->ino;
  1819. init_btrfs_i(inode);
  1820. BTRFS_I(inode)->root = args->root;
  1821. return 0;
  1822. }
  1823. static int btrfs_find_actor(struct inode *inode, void *opaque)
  1824. {
  1825. struct btrfs_iget_args *args = opaque;
  1826. return (args->ino == inode->i_ino &&
  1827. args->root == BTRFS_I(inode)->root);
  1828. }
  1829. struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
  1830. struct btrfs_root *root, int wait)
  1831. {
  1832. struct inode *inode;
  1833. struct btrfs_iget_args args;
  1834. args.ino = objectid;
  1835. args.root = root;
  1836. if (wait) {
  1837. inode = ilookup5(s, objectid, btrfs_find_actor,
  1838. (void *)&args);
  1839. } else {
  1840. inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
  1841. (void *)&args);
  1842. }
  1843. return inode;
  1844. }
  1845. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  1846. struct btrfs_root *root)
  1847. {
  1848. struct inode *inode;
  1849. struct btrfs_iget_args args;
  1850. args.ino = objectid;
  1851. args.root = root;
  1852. inode = iget5_locked(s, objectid, btrfs_find_actor,
  1853. btrfs_init_locked_inode,
  1854. (void *)&args);
  1855. return inode;
  1856. }
  1857. /* Get an inode object given its location and corresponding root.
  1858. * Returns in *is_new if the inode was read from disk
  1859. */
  1860. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  1861. struct btrfs_root *root, int *is_new)
  1862. {
  1863. struct inode *inode;
  1864. inode = btrfs_iget_locked(s, location->objectid, root);
  1865. if (!inode)
  1866. return ERR_PTR(-EACCES);
  1867. if (inode->i_state & I_NEW) {
  1868. BTRFS_I(inode)->root = root;
  1869. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  1870. btrfs_read_locked_inode(inode);
  1871. unlock_new_inode(inode);
  1872. if (is_new)
  1873. *is_new = 1;
  1874. } else {
  1875. if (is_new)
  1876. *is_new = 0;
  1877. }
  1878. return inode;
  1879. }
  1880. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  1881. struct nameidata *nd)
  1882. {
  1883. struct inode * inode;
  1884. struct btrfs_inode *bi = BTRFS_I(dir);
  1885. struct btrfs_root *root = bi->root;
  1886. struct btrfs_root *sub_root = root;
  1887. struct btrfs_key location;
  1888. int ret, new, do_orphan = 0;
  1889. if (dentry->d_name.len > BTRFS_NAME_LEN)
  1890. return ERR_PTR(-ENAMETOOLONG);
  1891. ret = btrfs_inode_by_name(dir, dentry, &location);
  1892. if (ret < 0)
  1893. return ERR_PTR(ret);
  1894. inode = NULL;
  1895. if (location.objectid) {
  1896. ret = fixup_tree_root_location(root, &location, &sub_root,
  1897. dentry);
  1898. if (ret < 0)
  1899. return ERR_PTR(ret);
  1900. if (ret > 0)
  1901. return ERR_PTR(-ENOENT);
  1902. inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
  1903. if (IS_ERR(inode))
  1904. return ERR_CAST(inode);
  1905. /* the inode and parent dir are two different roots */
  1906. if (new && root != sub_root) {
  1907. igrab(inode);
  1908. sub_root->inode = inode;
  1909. do_orphan = 1;
  1910. }
  1911. }
  1912. if (unlikely(do_orphan))
  1913. btrfs_orphan_cleanup(sub_root);
  1914. return d_splice_alias(inode, dentry);
  1915. }
  1916. static unsigned char btrfs_filetype_table[] = {
  1917. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  1918. };
  1919. static int btrfs_real_readdir(struct file *filp, void *dirent,
  1920. filldir_t filldir)
  1921. {
  1922. struct inode *inode = filp->f_dentry->d_inode;
  1923. struct btrfs_root *root = BTRFS_I(inode)->root;
  1924. struct btrfs_item *item;
  1925. struct btrfs_dir_item *di;
  1926. struct btrfs_key key;
  1927. struct btrfs_key found_key;
  1928. struct btrfs_path *path;
  1929. int ret;
  1930. u32 nritems;
  1931. struct extent_buffer *leaf;
  1932. int slot;
  1933. int advance;
  1934. unsigned char d_type;
  1935. int over = 0;
  1936. u32 di_cur;
  1937. u32 di_total;
  1938. u32 di_len;
  1939. int key_type = BTRFS_DIR_INDEX_KEY;
  1940. char tmp_name[32];
  1941. char *name_ptr;
  1942. int name_len;
  1943. /* FIXME, use a real flag for deciding about the key type */
  1944. if (root->fs_info->tree_root == root)
  1945. key_type = BTRFS_DIR_ITEM_KEY;
  1946. /* special case for "." */
  1947. if (filp->f_pos == 0) {
  1948. over = filldir(dirent, ".", 1,
  1949. 1, inode->i_ino,
  1950. DT_DIR);
  1951. if (over)
  1952. return 0;
  1953. filp->f_pos = 1;
  1954. }
  1955. /* special case for .., just use the back ref */
  1956. if (filp->f_pos == 1) {
  1957. u64 pino = parent_ino(filp->f_path.dentry);
  1958. over = filldir(dirent, "..", 2,
  1959. 2, pino, DT_DIR);
  1960. if (over)
  1961. return 0;
  1962. filp->f_pos = 2;
  1963. }
  1964. path = btrfs_alloc_path();
  1965. path->reada = 2;
  1966. btrfs_set_key_type(&key, key_type);
  1967. key.offset = filp->f_pos;
  1968. key.objectid = inode->i_ino;
  1969. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1970. if (ret < 0)
  1971. goto err;
  1972. advance = 0;
  1973. while (1) {
  1974. leaf = path->nodes[0];
  1975. nritems = btrfs_header_nritems(leaf);
  1976. slot = path->slots[0];
  1977. if (advance || slot >= nritems) {
  1978. if (slot >= nritems - 1) {
  1979. ret = btrfs_next_leaf(root, path);
  1980. if (ret)
  1981. break;
  1982. leaf = path->nodes[0];
  1983. nritems = btrfs_header_nritems(leaf);
  1984. slot = path->slots[0];
  1985. } else {
  1986. slot++;
  1987. path->slots[0]++;
  1988. }
  1989. }
  1990. advance = 1;
  1991. item = btrfs_item_nr(leaf, slot);
  1992. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1993. if (found_key.objectid != key.objectid)
  1994. break;
  1995. if (btrfs_key_type(&found_key) != key_type)
  1996. break;
  1997. if (found_key.offset < filp->f_pos)
  1998. continue;
  1999. filp->f_pos = found_key.offset;
  2000. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  2001. di_cur = 0;
  2002. di_total = btrfs_item_size(leaf, item);
  2003. while (di_cur < di_total) {
  2004. struct btrfs_key location;
  2005. name_len = btrfs_dir_name_len(leaf, di);
  2006. if (name_len <= sizeof(tmp_name)) {
  2007. name_ptr = tmp_name;
  2008. } else {
  2009. name_ptr = kmalloc(name_len, GFP_NOFS);
  2010. if (!name_ptr) {
  2011. ret = -ENOMEM;
  2012. goto err;
  2013. }
  2014. }
  2015. read_extent_buffer(leaf, name_ptr,
  2016. (unsigned long)(di + 1), name_len);
  2017. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  2018. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  2019. over = filldir(dirent, name_ptr, name_len,
  2020. found_key.offset, location.objectid,
  2021. d_type);
  2022. if (name_ptr != tmp_name)
  2023. kfree(name_ptr);
  2024. if (over)
  2025. goto nopos;
  2026. di_len = btrfs_dir_name_len(leaf, di) +
  2027. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  2028. di_cur += di_len;
  2029. di = (struct btrfs_dir_item *)((char *)di + di_len);
  2030. }
  2031. }
  2032. /* Reached end of directory/root. Bump pos past the last item. */
  2033. if (key_type == BTRFS_DIR_INDEX_KEY)
  2034. filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
  2035. else
  2036. filp->f_pos++;
  2037. nopos:
  2038. ret = 0;
  2039. err:
  2040. btrfs_free_path(path);
  2041. return ret;
  2042. }
  2043. int btrfs_write_inode(struct inode *inode, int wait)
  2044. {
  2045. struct btrfs_root *root = BTRFS_I(inode)->root;
  2046. struct btrfs_trans_handle *trans;
  2047. int ret = 0;
  2048. if (root->fs_info->closing > 1)
  2049. return 0;
  2050. if (wait) {
  2051. trans = btrfs_join_transaction(root, 1);
  2052. btrfs_set_trans_block_group(trans, inode);
  2053. ret = btrfs_commit_transaction(trans, root);
  2054. }
  2055. return ret;
  2056. }
  2057. /*
  2058. * This is somewhat expensive, updating the tree every time the
  2059. * inode changes. But, it is most likely to find the inode in cache.
  2060. * FIXME, needs more benchmarking...there are no reasons other than performance
  2061. * to keep or drop this code.
  2062. */
  2063. void btrfs_dirty_inode(struct inode *inode)
  2064. {
  2065. struct btrfs_root *root = BTRFS_I(inode)->root;
  2066. struct btrfs_trans_handle *trans;
  2067. trans = btrfs_join_transaction(root, 1);
  2068. btrfs_set_trans_block_group(trans, inode);
  2069. btrfs_update_inode(trans, root, inode);
  2070. btrfs_end_transaction(trans, root);
  2071. }
  2072. /*
  2073. * find the highest existing sequence number in a directory
  2074. * and then set the in-memory index_cnt variable to reflect
  2075. * free sequence numbers
  2076. */
  2077. static int btrfs_set_inode_index_count(struct inode *inode)
  2078. {
  2079. struct btrfs_root *root = BTRFS_I(inode)->root;
  2080. struct btrfs_key key, found_key;
  2081. struct btrfs_path *path;
  2082. struct extent_buffer *leaf;
  2083. int ret;
  2084. key.objectid = inode->i_ino;
  2085. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  2086. key.offset = (u64)-1;
  2087. path = btrfs_alloc_path();
  2088. if (!path)
  2089. return -ENOMEM;
  2090. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2091. if (ret < 0)
  2092. goto out;
  2093. /* FIXME: we should be able to handle this */
  2094. if (ret == 0)
  2095. goto out;
  2096. ret = 0;
  2097. /*
  2098. * MAGIC NUMBER EXPLANATION:
  2099. * since we search a directory based on f_pos we have to start at 2
  2100. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  2101. * else has to start at 2
  2102. */
  2103. if (path->slots[0] == 0) {
  2104. BTRFS_I(inode)->index_cnt = 2;
  2105. goto out;
  2106. }
  2107. path->slots[0]--;
  2108. leaf = path->nodes[0];
  2109. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2110. if (found_key.objectid != inode->i_ino ||
  2111. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  2112. BTRFS_I(inode)->index_cnt = 2;
  2113. goto out;
  2114. }
  2115. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  2116. out:
  2117. btrfs_free_path(path);
  2118. return ret;
  2119. }
  2120. /*
  2121. * helper to find a free sequence number in a given directory. This current
  2122. * code is very simple, later versions will do smarter things in the btree
  2123. */
  2124. static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
  2125. u64 *index)
  2126. {
  2127. int ret = 0;
  2128. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  2129. ret = btrfs_set_inode_index_count(dir);
  2130. if (ret) {
  2131. return ret;
  2132. }
  2133. }
  2134. *index = BTRFS_I(dir)->index_cnt;
  2135. BTRFS_I(dir)->index_cnt++;
  2136. return ret;
  2137. }
  2138. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  2139. struct btrfs_root *root,
  2140. struct inode *dir,
  2141. const char *name, int name_len,
  2142. u64 ref_objectid,
  2143. u64 objectid,
  2144. struct btrfs_block_group_cache *group,
  2145. int mode, u64 *index)
  2146. {
  2147. struct inode *inode;
  2148. struct btrfs_inode_item *inode_item;
  2149. struct btrfs_block_group_cache *new_inode_group;
  2150. struct btrfs_key *location;
  2151. struct btrfs_path *path;
  2152. struct btrfs_inode_ref *ref;
  2153. struct btrfs_key key[2];
  2154. u32 sizes[2];
  2155. unsigned long ptr;
  2156. int ret;
  2157. int owner;
  2158. path = btrfs_alloc_path();
  2159. BUG_ON(!path);
  2160. inode = new_inode(root->fs_info->sb);
  2161. if (!inode)
  2162. return ERR_PTR(-ENOMEM);
  2163. if (dir) {
  2164. ret = btrfs_set_inode_index(dir, inode, index);
  2165. if (ret)
  2166. return ERR_PTR(ret);
  2167. }
  2168. /*
  2169. * index_cnt is ignored for everything but a dir,
  2170. * btrfs_get_inode_index_count has an explanation for the magic
  2171. * number
  2172. */
  2173. init_btrfs_i(inode);
  2174. BTRFS_I(inode)->index_cnt = 2;
  2175. BTRFS_I(inode)->root = root;
  2176. BTRFS_I(inode)->generation = trans->transid;
  2177. if (mode & S_IFDIR)
  2178. owner = 0;
  2179. else
  2180. owner = 1;
  2181. new_inode_group = btrfs_find_block_group(root, group, 0,
  2182. BTRFS_BLOCK_GROUP_METADATA, owner);
  2183. if (!new_inode_group) {
  2184. printk("find_block group failed\n");
  2185. new_inode_group = group;
  2186. }
  2187. BTRFS_I(inode)->block_group = new_inode_group;
  2188. key[0].objectid = objectid;
  2189. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  2190. key[0].offset = 0;
  2191. key[1].objectid = objectid;
  2192. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  2193. key[1].offset = ref_objectid;
  2194. sizes[0] = sizeof(struct btrfs_inode_item);
  2195. sizes[1] = name_len + sizeof(*ref);
  2196. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  2197. if (ret != 0)
  2198. goto fail;
  2199. if (objectid > root->highest_inode)
  2200. root->highest_inode = objectid;
  2201. inode->i_uid = current->fsuid;
  2202. inode->i_gid = current->fsgid;
  2203. inode->i_mode = mode;
  2204. inode->i_ino = objectid;
  2205. inode_set_bytes(inode, 0);
  2206. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  2207. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2208. struct btrfs_inode_item);
  2209. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  2210. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  2211. struct btrfs_inode_ref);
  2212. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  2213. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  2214. ptr = (unsigned long)(ref + 1);
  2215. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  2216. btrfs_mark_buffer_dirty(path->nodes[0]);
  2217. btrfs_free_path(path);
  2218. location = &BTRFS_I(inode)->location;
  2219. location->objectid = objectid;
  2220. location->offset = 0;
  2221. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  2222. insert_inode_hash(inode);
  2223. return inode;
  2224. fail:
  2225. if (dir)
  2226. BTRFS_I(dir)->index_cnt--;
  2227. btrfs_free_path(path);
  2228. return ERR_PTR(ret);
  2229. }
  2230. static inline u8 btrfs_inode_type(struct inode *inode)
  2231. {
  2232. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  2233. }
  2234. /*
  2235. * utility function to add 'inode' into 'parent_inode' with
  2236. * a give name and a given sequence number.
  2237. * if 'add_backref' is true, also insert a backref from the
  2238. * inode to the parent directory.
  2239. */
  2240. int btrfs_add_link(struct btrfs_trans_handle *trans,
  2241. struct inode *parent_inode, struct inode *inode,
  2242. const char *name, int name_len, int add_backref, u64 index)
  2243. {
  2244. int ret;
  2245. struct btrfs_key key;
  2246. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  2247. key.objectid = inode->i_ino;
  2248. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  2249. key.offset = 0;
  2250. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  2251. parent_inode->i_ino,
  2252. &key, btrfs_inode_type(inode),
  2253. index);
  2254. if (ret == 0) {
  2255. if (add_backref) {
  2256. ret = btrfs_insert_inode_ref(trans, root,
  2257. name, name_len,
  2258. inode->i_ino,
  2259. parent_inode->i_ino,
  2260. index);
  2261. }
  2262. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  2263. name_len * 2);
  2264. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  2265. ret = btrfs_update_inode(trans, root, parent_inode);
  2266. }
  2267. return ret;
  2268. }
  2269. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  2270. struct dentry *dentry, struct inode *inode,
  2271. int backref, u64 index)
  2272. {
  2273. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  2274. inode, dentry->d_name.name,
  2275. dentry->d_name.len, backref, index);
  2276. if (!err) {
  2277. d_instantiate(dentry, inode);
  2278. return 0;
  2279. }
  2280. if (err > 0)
  2281. err = -EEXIST;
  2282. return err;
  2283. }
  2284. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  2285. int mode, dev_t rdev)
  2286. {
  2287. struct btrfs_trans_handle *trans;
  2288. struct btrfs_root *root = BTRFS_I(dir)->root;
  2289. struct inode *inode = NULL;
  2290. int err;
  2291. int drop_inode = 0;
  2292. u64 objectid;
  2293. unsigned long nr = 0;
  2294. u64 index = 0;
  2295. if (!new_valid_dev(rdev))
  2296. return -EINVAL;
  2297. err = btrfs_check_free_space(root, 1, 0);
  2298. if (err)
  2299. goto fail;
  2300. trans = btrfs_start_transaction(root, 1);
  2301. btrfs_set_trans_block_group(trans, dir);
  2302. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  2303. if (err) {
  2304. err = -ENOSPC;
  2305. goto out_unlock;
  2306. }
  2307. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  2308. dentry->d_name.len,
  2309. dentry->d_parent->d_inode->i_ino, objectid,
  2310. BTRFS_I(dir)->block_group, mode, &index);
  2311. err = PTR_ERR(inode);
  2312. if (IS_ERR(inode))
  2313. goto out_unlock;
  2314. err = btrfs_init_acl(inode, dir);
  2315. if (err) {
  2316. drop_inode = 1;
  2317. goto out_unlock;
  2318. }
  2319. btrfs_set_trans_block_group(trans, inode);
  2320. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  2321. if (err)
  2322. drop_inode = 1;
  2323. else {
  2324. inode->i_op = &btrfs_special_inode_operations;
  2325. init_special_inode(inode, inode->i_mode, rdev);
  2326. btrfs_update_inode(trans, root, inode);
  2327. }
  2328. dir->i_sb->s_dirt = 1;
  2329. btrfs_update_inode_block_group(trans, inode);
  2330. btrfs_update_inode_block_group(trans, dir);
  2331. out_unlock:
  2332. nr = trans->blocks_used;
  2333. btrfs_end_transaction_throttle(trans, root);
  2334. fail:
  2335. if (drop_inode) {
  2336. inode_dec_link_count(inode);
  2337. iput(inode);
  2338. }
  2339. btrfs_btree_balance_dirty(root, nr);
  2340. return err;
  2341. }
  2342. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  2343. int mode, struct nameidata *nd)
  2344. {
  2345. struct btrfs_trans_handle *trans;
  2346. struct btrfs_root *root = BTRFS_I(dir)->root;
  2347. struct inode *inode = NULL;
  2348. int err;
  2349. int drop_inode = 0;
  2350. unsigned long nr = 0;
  2351. u64 objectid;
  2352. u64 index = 0;
  2353. err = btrfs_check_free_space(root, 1, 0);
  2354. if (err)
  2355. goto fail;
  2356. trans = btrfs_start_transaction(root, 1);
  2357. btrfs_set_trans_block_group(trans, dir);
  2358. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  2359. if (err) {
  2360. err = -ENOSPC;
  2361. goto out_unlock;
  2362. }
  2363. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  2364. dentry->d_name.len,
  2365. dentry->d_parent->d_inode->i_ino,
  2366. objectid, BTRFS_I(dir)->block_group, mode,
  2367. &index);
  2368. err = PTR_ERR(inode);
  2369. if (IS_ERR(inode))
  2370. goto out_unlock;
  2371. err = btrfs_init_acl(inode, dir);
  2372. if (err) {
  2373. drop_inode = 1;
  2374. goto out_unlock;
  2375. }
  2376. btrfs_set_trans_block_group(trans, inode);
  2377. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  2378. if (err)
  2379. drop_inode = 1;
  2380. else {
  2381. inode->i_mapping->a_ops = &btrfs_aops;
  2382. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  2383. inode->i_fop = &btrfs_file_operations;
  2384. inode->i_op = &btrfs_file_inode_operations;
  2385. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  2386. }
  2387. dir->i_sb->s_dirt = 1;
  2388. btrfs_update_inode_block_group(trans, inode);
  2389. btrfs_update_inode_block_group(trans, dir);
  2390. out_unlock:
  2391. nr = trans->blocks_used;
  2392. btrfs_end_transaction_throttle(trans, root);
  2393. fail:
  2394. if (drop_inode) {
  2395. inode_dec_link_count(inode);
  2396. iput(inode);
  2397. }
  2398. btrfs_btree_balance_dirty(root, nr);
  2399. return err;
  2400. }
  2401. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  2402. struct dentry *dentry)
  2403. {
  2404. struct btrfs_trans_handle *trans;
  2405. struct btrfs_root *root = BTRFS_I(dir)->root;
  2406. struct inode *inode = old_dentry->d_inode;
  2407. u64 index;
  2408. unsigned long nr = 0;
  2409. int err;
  2410. int drop_inode = 0;
  2411. if (inode->i_nlink == 0)
  2412. return -ENOENT;
  2413. btrfs_inc_nlink(inode);
  2414. err = btrfs_check_free_space(root, 1, 0);
  2415. if (err)
  2416. goto fail;
  2417. err = btrfs_set_inode_index(dir, inode, &index);
  2418. if (err)
  2419. goto fail;
  2420. trans = btrfs_start_transaction(root, 1);
  2421. btrfs_set_trans_block_group(trans, dir);
  2422. atomic_inc(&inode->i_count);
  2423. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  2424. if (err)
  2425. drop_inode = 1;
  2426. dir->i_sb->s_dirt = 1;
  2427. btrfs_update_inode_block_group(trans, dir);
  2428. err = btrfs_update_inode(trans, root, inode);
  2429. if (err)
  2430. drop_inode = 1;
  2431. nr = trans->blocks_used;
  2432. btrfs_end_transaction_throttle(trans, root);
  2433. fail:
  2434. if (drop_inode) {
  2435. inode_dec_link_count(inode);
  2436. iput(inode);
  2437. }
  2438. btrfs_btree_balance_dirty(root, nr);
  2439. return err;
  2440. }
  2441. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2442. {
  2443. struct inode *inode = NULL;
  2444. struct btrfs_trans_handle *trans;
  2445. struct btrfs_root *root = BTRFS_I(dir)->root;
  2446. int err = 0;
  2447. int drop_on_err = 0;
  2448. u64 objectid = 0;
  2449. u64 index = 0;
  2450. unsigned long nr = 1;
  2451. err = btrfs_check_free_space(root, 1, 0);
  2452. if (err)
  2453. goto out_unlock;
  2454. trans = btrfs_start_transaction(root, 1);
  2455. btrfs_set_trans_block_group(trans, dir);
  2456. if (IS_ERR(trans)) {
  2457. err = PTR_ERR(trans);
  2458. goto out_unlock;
  2459. }
  2460. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  2461. if (err) {
  2462. err = -ENOSPC;
  2463. goto out_unlock;
  2464. }
  2465. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  2466. dentry->d_name.len,
  2467. dentry->d_parent->d_inode->i_ino, objectid,
  2468. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  2469. &index);
  2470. if (IS_ERR(inode)) {
  2471. err = PTR_ERR(inode);
  2472. goto out_fail;
  2473. }
  2474. drop_on_err = 1;
  2475. err = btrfs_init_acl(inode, dir);
  2476. if (err)
  2477. goto out_fail;
  2478. inode->i_op = &btrfs_dir_inode_operations;
  2479. inode->i_fop = &btrfs_dir_file_operations;
  2480. btrfs_set_trans_block_group(trans, inode);
  2481. btrfs_i_size_write(inode, 0);
  2482. err = btrfs_update_inode(trans, root, inode);
  2483. if (err)
  2484. goto out_fail;
  2485. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  2486. inode, dentry->d_name.name,
  2487. dentry->d_name.len, 0, index);
  2488. if (err)
  2489. goto out_fail;
  2490. d_instantiate(dentry, inode);
  2491. drop_on_err = 0;
  2492. dir->i_sb->s_dirt = 1;
  2493. btrfs_update_inode_block_group(trans, inode);
  2494. btrfs_update_inode_block_group(trans, dir);
  2495. out_fail:
  2496. nr = trans->blocks_used;
  2497. btrfs_end_transaction_throttle(trans, root);
  2498. out_unlock:
  2499. if (drop_on_err)
  2500. iput(inode);
  2501. btrfs_btree_balance_dirty(root, nr);
  2502. return err;
  2503. }
  2504. /* helper for btfs_get_extent. Given an existing extent in the tree,
  2505. * and an extent that you want to insert, deal with overlap and insert
  2506. * the new extent into the tree.
  2507. */
  2508. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  2509. struct extent_map *existing,
  2510. struct extent_map *em,
  2511. u64 map_start, u64 map_len)
  2512. {
  2513. u64 start_diff;
  2514. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  2515. start_diff = map_start - em->start;
  2516. em->start = map_start;
  2517. em->len = map_len;
  2518. if (em->block_start < EXTENT_MAP_LAST_BYTE)
  2519. em->block_start += start_diff;
  2520. return add_extent_mapping(em_tree, em);
  2521. }
  2522. /*
  2523. * a bit scary, this does extent mapping from logical file offset to the disk.
  2524. * the ugly parts come from merging extents from the disk with the
  2525. * in-ram representation. This gets more complex because of the data=ordered code,
  2526. * where the in-ram extents might be locked pending data=ordered completion.
  2527. *
  2528. * This also copies inline extents directly into the page.
  2529. */
  2530. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  2531. size_t pg_offset, u64 start, u64 len,
  2532. int create)
  2533. {
  2534. int ret;
  2535. int err = 0;
  2536. u64 bytenr;
  2537. u64 extent_start = 0;
  2538. u64 extent_end = 0;
  2539. u64 objectid = inode->i_ino;
  2540. u32 found_type;
  2541. struct btrfs_path *path = NULL;
  2542. struct btrfs_root *root = BTRFS_I(inode)->root;
  2543. struct btrfs_file_extent_item *item;
  2544. struct extent_buffer *leaf;
  2545. struct btrfs_key found_key;
  2546. struct extent_map *em = NULL;
  2547. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  2548. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2549. struct btrfs_trans_handle *trans = NULL;
  2550. again:
  2551. spin_lock(&em_tree->lock);
  2552. em = lookup_extent_mapping(em_tree, start, len);
  2553. if (em)
  2554. em->bdev = root->fs_info->fs_devices->latest_bdev;
  2555. spin_unlock(&em_tree->lock);
  2556. if (em) {
  2557. if (em->start > start || em->start + em->len <= start)
  2558. free_extent_map(em);
  2559. else if (em->block_start == EXTENT_MAP_INLINE && page)
  2560. free_extent_map(em);
  2561. else
  2562. goto out;
  2563. }
  2564. em = alloc_extent_map(GFP_NOFS);
  2565. if (!em) {
  2566. err = -ENOMEM;
  2567. goto out;
  2568. }
  2569. em->bdev = root->fs_info->fs_devices->latest_bdev;
  2570. em->start = EXTENT_MAP_HOLE;
  2571. em->len = (u64)-1;
  2572. if (!path) {
  2573. path = btrfs_alloc_path();
  2574. BUG_ON(!path);
  2575. }
  2576. ret = btrfs_lookup_file_extent(trans, root, path,
  2577. objectid, start, trans != NULL);
  2578. if (ret < 0) {
  2579. err = ret;
  2580. goto out;
  2581. }
  2582. if (ret != 0) {
  2583. if (path->slots[0] == 0)
  2584. goto not_found;
  2585. path->slots[0]--;
  2586. }
  2587. leaf = path->nodes[0];
  2588. item = btrfs_item_ptr(leaf, path->slots[0],
  2589. struct btrfs_file_extent_item);
  2590. /* are we inside the extent that was found? */
  2591. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2592. found_type = btrfs_key_type(&found_key);
  2593. if (found_key.objectid != objectid ||
  2594. found_type != BTRFS_EXTENT_DATA_KEY) {
  2595. goto not_found;
  2596. }
  2597. found_type = btrfs_file_extent_type(leaf, item);
  2598. extent_start = found_key.offset;
  2599. if (found_type == BTRFS_FILE_EXTENT_REG) {
  2600. extent_end = extent_start +
  2601. btrfs_file_extent_num_bytes(leaf, item);
  2602. err = 0;
  2603. if (start < extent_start || start >= extent_end) {
  2604. em->start = start;
  2605. if (start < extent_start) {
  2606. if (start + len <= extent_start)
  2607. goto not_found;
  2608. em->len = extent_end - extent_start;
  2609. } else {
  2610. em->len = len;
  2611. }
  2612. goto not_found_em;
  2613. }
  2614. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  2615. if (bytenr == 0) {
  2616. em->start = extent_start;
  2617. em->len = extent_end - extent_start;
  2618. em->block_start = EXTENT_MAP_HOLE;
  2619. goto insert;
  2620. }
  2621. bytenr += btrfs_file_extent_offset(leaf, item);
  2622. em->block_start = bytenr;
  2623. em->start = extent_start;
  2624. em->len = extent_end - extent_start;
  2625. goto insert;
  2626. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  2627. u64 page_start;
  2628. unsigned long ptr;
  2629. char *map;
  2630. size_t size;
  2631. size_t extent_offset;
  2632. size_t copy_size;
  2633. size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
  2634. path->slots[0]));
  2635. extent_end = (extent_start + size + root->sectorsize - 1) &
  2636. ~((u64)root->sectorsize - 1);
  2637. if (start < extent_start || start >= extent_end) {
  2638. em->start = start;
  2639. if (start < extent_start) {
  2640. if (start + len <= extent_start)
  2641. goto not_found;
  2642. em->len = extent_end - extent_start;
  2643. } else {
  2644. em->len = len;
  2645. }
  2646. goto not_found_em;
  2647. }
  2648. em->block_start = EXTENT_MAP_INLINE;
  2649. if (!page) {
  2650. em->start = extent_start;
  2651. em->len = size;
  2652. goto out;
  2653. }
  2654. page_start = page_offset(page) + pg_offset;
  2655. extent_offset = page_start - extent_start;
  2656. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  2657. size - extent_offset);
  2658. em->start = extent_start + extent_offset;
  2659. em->len = (copy_size + root->sectorsize - 1) &
  2660. ~((u64)root->sectorsize - 1);
  2661. map = kmap(page);
  2662. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  2663. if (create == 0 && !PageUptodate(page)) {
  2664. read_extent_buffer(leaf, map + pg_offset, ptr,
  2665. copy_size);
  2666. flush_dcache_page(page);
  2667. } else if (create && PageUptodate(page)) {
  2668. if (!trans) {
  2669. kunmap(page);
  2670. free_extent_map(em);
  2671. em = NULL;
  2672. btrfs_release_path(root, path);
  2673. trans = btrfs_join_transaction(root, 1);
  2674. goto again;
  2675. }
  2676. write_extent_buffer(leaf, map + pg_offset, ptr,
  2677. copy_size);
  2678. btrfs_mark_buffer_dirty(leaf);
  2679. }
  2680. kunmap(page);
  2681. set_extent_uptodate(io_tree, em->start,
  2682. extent_map_end(em) - 1, GFP_NOFS);
  2683. goto insert;
  2684. } else {
  2685. printk("unkknown found_type %d\n", found_type);
  2686. WARN_ON(1);
  2687. }
  2688. not_found:
  2689. em->start = start;
  2690. em->len = len;
  2691. not_found_em:
  2692. em->block_start = EXTENT_MAP_HOLE;
  2693. insert:
  2694. btrfs_release_path(root, path);
  2695. if (em->start > start || extent_map_end(em) <= start) {
  2696. printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
  2697. err = -EIO;
  2698. goto out;
  2699. }
  2700. err = 0;
  2701. spin_lock(&em_tree->lock);
  2702. ret = add_extent_mapping(em_tree, em);
  2703. /* it is possible that someone inserted the extent into the tree
  2704. * while we had the lock dropped. It is also possible that
  2705. * an overlapping map exists in the tree
  2706. */
  2707. if (ret == -EEXIST) {
  2708. struct extent_map *existing;
  2709. ret = 0;
  2710. existing = lookup_extent_mapping(em_tree, start, len);
  2711. if (existing && (existing->start > start ||
  2712. existing->start + existing->len <= start)) {
  2713. free_extent_map(existing);
  2714. existing = NULL;
  2715. }
  2716. if (!existing) {
  2717. existing = lookup_extent_mapping(em_tree, em->start,
  2718. em->len);
  2719. if (existing) {
  2720. err = merge_extent_mapping(em_tree, existing,
  2721. em, start,
  2722. root->sectorsize);
  2723. free_extent_map(existing);
  2724. if (err) {
  2725. free_extent_map(em);
  2726. em = NULL;
  2727. }
  2728. } else {
  2729. err = -EIO;
  2730. printk("failing to insert %Lu %Lu\n",
  2731. start, len);
  2732. free_extent_map(em);
  2733. em = NULL;
  2734. }
  2735. } else {
  2736. free_extent_map(em);
  2737. em = existing;
  2738. err = 0;
  2739. }
  2740. }
  2741. spin_unlock(&em_tree->lock);
  2742. out:
  2743. if (path)
  2744. btrfs_free_path(path);
  2745. if (trans) {
  2746. ret = btrfs_end_transaction(trans, root);
  2747. if (!err) {
  2748. err = ret;
  2749. }
  2750. }
  2751. if (err) {
  2752. free_extent_map(em);
  2753. WARN_ON(1);
  2754. return ERR_PTR(err);
  2755. }
  2756. return em;
  2757. }
  2758. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  2759. const struct iovec *iov, loff_t offset,
  2760. unsigned long nr_segs)
  2761. {
  2762. return -EINVAL;
  2763. }
  2764. static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
  2765. {
  2766. return extent_bmap(mapping, iblock, btrfs_get_extent);
  2767. }
  2768. int btrfs_readpage(struct file *file, struct page *page)
  2769. {
  2770. struct extent_io_tree *tree;
  2771. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2772. return extent_read_full_page(tree, page, btrfs_get_extent);
  2773. }
  2774. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  2775. {
  2776. struct extent_io_tree *tree;
  2777. if (current->flags & PF_MEMALLOC) {
  2778. redirty_page_for_writepage(wbc, page);
  2779. unlock_page(page);
  2780. return 0;
  2781. }
  2782. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2783. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  2784. }
  2785. int btrfs_writepages(struct address_space *mapping,
  2786. struct writeback_control *wbc)
  2787. {
  2788. struct extent_io_tree *tree;
  2789. tree = &BTRFS_I(mapping->host)->io_tree;
  2790. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  2791. }
  2792. static int
  2793. btrfs_readpages(struct file *file, struct address_space *mapping,
  2794. struct list_head *pages, unsigned nr_pages)
  2795. {
  2796. struct extent_io_tree *tree;
  2797. tree = &BTRFS_I(mapping->host)->io_tree;
  2798. return extent_readpages(tree, mapping, pages, nr_pages,
  2799. btrfs_get_extent);
  2800. }
  2801. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  2802. {
  2803. struct extent_io_tree *tree;
  2804. struct extent_map_tree *map;
  2805. int ret;
  2806. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2807. map = &BTRFS_I(page->mapping->host)->extent_tree;
  2808. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  2809. if (ret == 1) {
  2810. ClearPagePrivate(page);
  2811. set_page_private(page, 0);
  2812. page_cache_release(page);
  2813. }
  2814. return ret;
  2815. }
  2816. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  2817. {
  2818. if (PageWriteback(page) || PageDirty(page))
  2819. return 0;
  2820. return __btrfs_releasepage(page, gfp_flags);
  2821. }
  2822. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  2823. {
  2824. struct extent_io_tree *tree;
  2825. struct btrfs_ordered_extent *ordered;
  2826. u64 page_start = page_offset(page);
  2827. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  2828. wait_on_page_writeback(page);
  2829. tree = &BTRFS_I(page->mapping->host)->io_tree;
  2830. if (offset) {
  2831. btrfs_releasepage(page, GFP_NOFS);
  2832. return;
  2833. }
  2834. lock_extent(tree, page_start, page_end, GFP_NOFS);
  2835. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  2836. page_offset(page));
  2837. if (ordered) {
  2838. /*
  2839. * IO on this page will never be started, so we need
  2840. * to account for any ordered extents now
  2841. */
  2842. clear_extent_bit(tree, page_start, page_end,
  2843. EXTENT_DIRTY | EXTENT_DELALLOC |
  2844. EXTENT_LOCKED, 1, 0, GFP_NOFS);
  2845. btrfs_finish_ordered_io(page->mapping->host,
  2846. page_start, page_end);
  2847. btrfs_put_ordered_extent(ordered);
  2848. lock_extent(tree, page_start, page_end, GFP_NOFS);
  2849. }
  2850. clear_extent_bit(tree, page_start, page_end,
  2851. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  2852. EXTENT_ORDERED,
  2853. 1, 1, GFP_NOFS);
  2854. __btrfs_releasepage(page, GFP_NOFS);
  2855. ClearPageChecked(page);
  2856. if (PagePrivate(page)) {
  2857. ClearPagePrivate(page);
  2858. set_page_private(page, 0);
  2859. page_cache_release(page);
  2860. }
  2861. }
  2862. /*
  2863. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  2864. * called from a page fault handler when a page is first dirtied. Hence we must
  2865. * be careful to check for EOF conditions here. We set the page up correctly
  2866. * for a written page which means we get ENOSPC checking when writing into
  2867. * holes and correct delalloc and unwritten extent mapping on filesystems that
  2868. * support these features.
  2869. *
  2870. * We are not allowed to take the i_mutex here so we have to play games to
  2871. * protect against truncate races as the page could now be beyond EOF. Because
  2872. * vmtruncate() writes the inode size before removing pages, once we have the
  2873. * page lock we can determine safely if the page is beyond EOF. If it is not
  2874. * beyond EOF, then the page is guaranteed safe against truncation until we
  2875. * unlock the page.
  2876. */
  2877. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  2878. {
  2879. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  2880. struct btrfs_root *root = BTRFS_I(inode)->root;
  2881. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2882. struct btrfs_ordered_extent *ordered;
  2883. char *kaddr;
  2884. unsigned long zero_start;
  2885. loff_t size;
  2886. int ret;
  2887. u64 page_start;
  2888. u64 page_end;
  2889. ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
  2890. if (ret)
  2891. goto out;
  2892. ret = -EINVAL;
  2893. again:
  2894. lock_page(page);
  2895. size = i_size_read(inode);
  2896. page_start = page_offset(page);
  2897. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2898. if ((page->mapping != inode->i_mapping) ||
  2899. (page_start >= size)) {
  2900. /* page got truncated out from underneath us */
  2901. goto out_unlock;
  2902. }
  2903. wait_on_page_writeback(page);
  2904. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2905. set_page_extent_mapped(page);
  2906. /*
  2907. * we can't set the delalloc bits if there are pending ordered
  2908. * extents. Drop our locks and wait for them to finish
  2909. */
  2910. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2911. if (ordered) {
  2912. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2913. unlock_page(page);
  2914. btrfs_start_ordered_extent(inode, ordered, 1);
  2915. btrfs_put_ordered_extent(ordered);
  2916. goto again;
  2917. }
  2918. btrfs_set_extent_delalloc(inode, page_start, page_end);
  2919. ret = 0;
  2920. /* page is wholly or partially inside EOF */
  2921. if (page_start + PAGE_CACHE_SIZE > size)
  2922. zero_start = size & ~PAGE_CACHE_MASK;
  2923. else
  2924. zero_start = PAGE_CACHE_SIZE;
  2925. if (zero_start != PAGE_CACHE_SIZE) {
  2926. kaddr = kmap(page);
  2927. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  2928. flush_dcache_page(page);
  2929. kunmap(page);
  2930. }
  2931. ClearPageChecked(page);
  2932. set_page_dirty(page);
  2933. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2934. out_unlock:
  2935. unlock_page(page);
  2936. out:
  2937. return ret;
  2938. }
  2939. static void btrfs_truncate(struct inode *inode)
  2940. {
  2941. struct btrfs_root *root = BTRFS_I(inode)->root;
  2942. int ret;
  2943. struct btrfs_trans_handle *trans;
  2944. unsigned long nr;
  2945. u64 mask = root->sectorsize - 1;
  2946. if (!S_ISREG(inode->i_mode))
  2947. return;
  2948. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2949. return;
  2950. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  2951. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  2952. trans = btrfs_start_transaction(root, 1);
  2953. btrfs_set_trans_block_group(trans, inode);
  2954. btrfs_i_size_write(inode, inode->i_size);
  2955. ret = btrfs_orphan_add(trans, inode);
  2956. if (ret)
  2957. goto out;
  2958. /* FIXME, add redo link to tree so we don't leak on crash */
  2959. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
  2960. BTRFS_EXTENT_DATA_KEY);
  2961. btrfs_update_inode(trans, root, inode);
  2962. ret = btrfs_orphan_del(trans, inode);
  2963. BUG_ON(ret);
  2964. out:
  2965. nr = trans->blocks_used;
  2966. ret = btrfs_end_transaction_throttle(trans, root);
  2967. BUG_ON(ret);
  2968. btrfs_btree_balance_dirty(root, nr);
  2969. }
  2970. /*
  2971. * Invalidate a single dcache entry at the root of the filesystem.
  2972. * Needed after creation of snapshot or subvolume.
  2973. */
  2974. void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
  2975. int namelen)
  2976. {
  2977. struct dentry *alias, *entry;
  2978. struct qstr qstr;
  2979. alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
  2980. if (alias) {
  2981. qstr.name = name;
  2982. qstr.len = namelen;
  2983. /* change me if btrfs ever gets a d_hash operation */
  2984. qstr.hash = full_name_hash(qstr.name, qstr.len);
  2985. entry = d_lookup(alias, &qstr);
  2986. dput(alias);
  2987. if (entry) {
  2988. d_invalidate(entry);
  2989. dput(entry);
  2990. }
  2991. }
  2992. }
  2993. /*
  2994. * create a new subvolume directory/inode (helper for the ioctl).
  2995. */
  2996. int btrfs_create_subvol_root(struct btrfs_root *new_root,
  2997. struct btrfs_trans_handle *trans, u64 new_dirid,
  2998. struct btrfs_block_group_cache *block_group)
  2999. {
  3000. struct inode *inode;
  3001. u64 index = 0;
  3002. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  3003. new_dirid, block_group, S_IFDIR | 0700, &index);
  3004. if (IS_ERR(inode))
  3005. return PTR_ERR(inode);
  3006. inode->i_op = &btrfs_dir_inode_operations;
  3007. inode->i_fop = &btrfs_dir_file_operations;
  3008. new_root->inode = inode;
  3009. inode->i_nlink = 1;
  3010. btrfs_i_size_write(inode, 0);
  3011. return btrfs_update_inode(trans, new_root, inode);
  3012. }
  3013. /* helper function for file defrag and space balancing. This
  3014. * forces readahead on a given range of bytes in an inode
  3015. */
  3016. unsigned long btrfs_force_ra(struct address_space *mapping,
  3017. struct file_ra_state *ra, struct file *file,
  3018. pgoff_t offset, pgoff_t last_index)
  3019. {
  3020. pgoff_t req_size = last_index - offset + 1;
  3021. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  3022. return offset + req_size;
  3023. }
  3024. struct inode *btrfs_alloc_inode(struct super_block *sb)
  3025. {
  3026. struct btrfs_inode *ei;
  3027. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  3028. if (!ei)
  3029. return NULL;
  3030. ei->last_trans = 0;
  3031. ei->logged_trans = 0;
  3032. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  3033. ei->i_acl = BTRFS_ACL_NOT_CACHED;
  3034. ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
  3035. INIT_LIST_HEAD(&ei->i_orphan);
  3036. return &ei->vfs_inode;
  3037. }
  3038. void btrfs_destroy_inode(struct inode *inode)
  3039. {
  3040. struct btrfs_ordered_extent *ordered;
  3041. WARN_ON(!list_empty(&inode->i_dentry));
  3042. WARN_ON(inode->i_data.nrpages);
  3043. if (BTRFS_I(inode)->i_acl &&
  3044. BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
  3045. posix_acl_release(BTRFS_I(inode)->i_acl);
  3046. if (BTRFS_I(inode)->i_default_acl &&
  3047. BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
  3048. posix_acl_release(BTRFS_I(inode)->i_default_acl);
  3049. spin_lock(&BTRFS_I(inode)->root->list_lock);
  3050. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  3051. printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
  3052. " list\n", inode->i_ino);
  3053. dump_stack();
  3054. }
  3055. spin_unlock(&BTRFS_I(inode)->root->list_lock);
  3056. while(1) {
  3057. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  3058. if (!ordered)
  3059. break;
  3060. else {
  3061. printk("found ordered extent %Lu %Lu\n",
  3062. ordered->file_offset, ordered->len);
  3063. btrfs_remove_ordered_extent(inode, ordered);
  3064. btrfs_put_ordered_extent(ordered);
  3065. btrfs_put_ordered_extent(ordered);
  3066. }
  3067. }
  3068. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  3069. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  3070. }
  3071. static void init_once(void *foo)
  3072. {
  3073. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  3074. inode_init_once(&ei->vfs_inode);
  3075. }
  3076. void btrfs_destroy_cachep(void)
  3077. {
  3078. if (btrfs_inode_cachep)
  3079. kmem_cache_destroy(btrfs_inode_cachep);
  3080. if (btrfs_trans_handle_cachep)
  3081. kmem_cache_destroy(btrfs_trans_handle_cachep);
  3082. if (btrfs_transaction_cachep)
  3083. kmem_cache_destroy(btrfs_transaction_cachep);
  3084. if (btrfs_bit_radix_cachep)
  3085. kmem_cache_destroy(btrfs_bit_radix_cachep);
  3086. if (btrfs_path_cachep)
  3087. kmem_cache_destroy(btrfs_path_cachep);
  3088. }
  3089. struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
  3090. unsigned long extra_flags,
  3091. void (*ctor)(void *))
  3092. {
  3093. return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
  3094. SLAB_MEM_SPREAD | extra_flags), ctor);
  3095. }
  3096. int btrfs_init_cachep(void)
  3097. {
  3098. btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
  3099. sizeof(struct btrfs_inode),
  3100. 0, init_once);
  3101. if (!btrfs_inode_cachep)
  3102. goto fail;
  3103. btrfs_trans_handle_cachep =
  3104. btrfs_cache_create("btrfs_trans_handle_cache",
  3105. sizeof(struct btrfs_trans_handle),
  3106. 0, NULL);
  3107. if (!btrfs_trans_handle_cachep)
  3108. goto fail;
  3109. btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
  3110. sizeof(struct btrfs_transaction),
  3111. 0, NULL);
  3112. if (!btrfs_transaction_cachep)
  3113. goto fail;
  3114. btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
  3115. sizeof(struct btrfs_path),
  3116. 0, NULL);
  3117. if (!btrfs_path_cachep)
  3118. goto fail;
  3119. btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
  3120. SLAB_DESTROY_BY_RCU, NULL);
  3121. if (!btrfs_bit_radix_cachep)
  3122. goto fail;
  3123. return 0;
  3124. fail:
  3125. btrfs_destroy_cachep();
  3126. return -ENOMEM;
  3127. }
  3128. static int btrfs_getattr(struct vfsmount *mnt,
  3129. struct dentry *dentry, struct kstat *stat)
  3130. {
  3131. struct inode *inode = dentry->d_inode;
  3132. generic_fillattr(inode, stat);
  3133. stat->blksize = PAGE_CACHE_SIZE;
  3134. stat->blocks = (inode_get_bytes(inode) +
  3135. BTRFS_I(inode)->delalloc_bytes) >> 9;
  3136. return 0;
  3137. }
  3138. static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
  3139. struct inode * new_dir,struct dentry *new_dentry)
  3140. {
  3141. struct btrfs_trans_handle *trans;
  3142. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  3143. struct inode *new_inode = new_dentry->d_inode;
  3144. struct inode *old_inode = old_dentry->d_inode;
  3145. struct timespec ctime = CURRENT_TIME;
  3146. u64 index = 0;
  3147. int ret;
  3148. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  3149. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
  3150. return -ENOTEMPTY;
  3151. }
  3152. ret = btrfs_check_free_space(root, 1, 0);
  3153. if (ret)
  3154. goto out_unlock;
  3155. trans = btrfs_start_transaction(root, 1);
  3156. btrfs_set_trans_block_group(trans, new_dir);
  3157. btrfs_inc_nlink(old_dentry->d_inode);
  3158. old_dir->i_ctime = old_dir->i_mtime = ctime;
  3159. new_dir->i_ctime = new_dir->i_mtime = ctime;
  3160. old_inode->i_ctime = ctime;
  3161. ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
  3162. old_dentry->d_name.name,
  3163. old_dentry->d_name.len);
  3164. if (ret)
  3165. goto out_fail;
  3166. if (new_inode) {
  3167. new_inode->i_ctime = CURRENT_TIME;
  3168. ret = btrfs_unlink_inode(trans, root, new_dir,
  3169. new_dentry->d_inode,
  3170. new_dentry->d_name.name,
  3171. new_dentry->d_name.len);
  3172. if (ret)
  3173. goto out_fail;
  3174. if (new_inode->i_nlink == 0) {
  3175. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  3176. if (ret)
  3177. goto out_fail;
  3178. }
  3179. }
  3180. ret = btrfs_set_inode_index(new_dir, old_inode, &index);
  3181. if (ret)
  3182. goto out_fail;
  3183. ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
  3184. old_inode, new_dentry->d_name.name,
  3185. new_dentry->d_name.len, 1, index);
  3186. if (ret)
  3187. goto out_fail;
  3188. out_fail:
  3189. btrfs_end_transaction_throttle(trans, root);
  3190. out_unlock:
  3191. return ret;
  3192. }
  3193. /*
  3194. * some fairly slow code that needs optimization. This walks the list
  3195. * of all the inodes with pending delalloc and forces them to disk.
  3196. */
  3197. int btrfs_start_delalloc_inodes(struct btrfs_root *root)
  3198. {
  3199. struct list_head *head = &root->fs_info->delalloc_inodes;
  3200. struct btrfs_inode *binode;
  3201. struct inode *inode;
  3202. unsigned long flags;
  3203. spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
  3204. while(!list_empty(head)) {
  3205. binode = list_entry(head->next, struct btrfs_inode,
  3206. delalloc_inodes);
  3207. inode = igrab(&binode->vfs_inode);
  3208. if (!inode)
  3209. list_del_init(&binode->delalloc_inodes);
  3210. spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
  3211. if (inode) {
  3212. filemap_flush(inode->i_mapping);
  3213. iput(inode);
  3214. }
  3215. cond_resched();
  3216. spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
  3217. }
  3218. spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
  3219. /* the filemap_flush will queue IO into the worker threads, but
  3220. * we have to make sure the IO is actually started and that
  3221. * ordered extents get created before we return
  3222. */
  3223. atomic_inc(&root->fs_info->async_submit_draining);
  3224. while(atomic_read(&root->fs_info->nr_async_submits)) {
  3225. wait_event(root->fs_info->async_submit_wait,
  3226. (atomic_read(&root->fs_info->nr_async_submits) == 0));
  3227. }
  3228. atomic_dec(&root->fs_info->async_submit_draining);
  3229. return 0;
  3230. }
  3231. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  3232. const char *symname)
  3233. {
  3234. struct btrfs_trans_handle *trans;
  3235. struct btrfs_root *root = BTRFS_I(dir)->root;
  3236. struct btrfs_path *path;
  3237. struct btrfs_key key;
  3238. struct inode *inode = NULL;
  3239. int err;
  3240. int drop_inode = 0;
  3241. u64 objectid;
  3242. u64 index = 0 ;
  3243. int name_len;
  3244. int datasize;
  3245. unsigned long ptr;
  3246. struct btrfs_file_extent_item *ei;
  3247. struct extent_buffer *leaf;
  3248. unsigned long nr = 0;
  3249. name_len = strlen(symname) + 1;
  3250. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  3251. return -ENAMETOOLONG;
  3252. err = btrfs_check_free_space(root, 1, 0);
  3253. if (err)
  3254. goto out_fail;
  3255. trans = btrfs_start_transaction(root, 1);
  3256. btrfs_set_trans_block_group(trans, dir);
  3257. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3258. if (err) {
  3259. err = -ENOSPC;
  3260. goto out_unlock;
  3261. }
  3262. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3263. dentry->d_name.len,
  3264. dentry->d_parent->d_inode->i_ino, objectid,
  3265. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  3266. &index);
  3267. err = PTR_ERR(inode);
  3268. if (IS_ERR(inode))
  3269. goto out_unlock;
  3270. err = btrfs_init_acl(inode, dir);
  3271. if (err) {
  3272. drop_inode = 1;
  3273. goto out_unlock;
  3274. }
  3275. btrfs_set_trans_block_group(trans, inode);
  3276. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3277. if (err)
  3278. drop_inode = 1;
  3279. else {
  3280. inode->i_mapping->a_ops = &btrfs_aops;
  3281. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3282. inode->i_fop = &btrfs_file_operations;
  3283. inode->i_op = &btrfs_file_inode_operations;
  3284. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3285. }
  3286. dir->i_sb->s_dirt = 1;
  3287. btrfs_update_inode_block_group(trans, inode);
  3288. btrfs_update_inode_block_group(trans, dir);
  3289. if (drop_inode)
  3290. goto out_unlock;
  3291. path = btrfs_alloc_path();
  3292. BUG_ON(!path);
  3293. key.objectid = inode->i_ino;
  3294. key.offset = 0;
  3295. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  3296. datasize = btrfs_file_extent_calc_inline_size(name_len);
  3297. err = btrfs_insert_empty_item(trans, root, path, &key,
  3298. datasize);
  3299. if (err) {
  3300. drop_inode = 1;
  3301. goto out_unlock;
  3302. }
  3303. leaf = path->nodes[0];
  3304. ei = btrfs_item_ptr(leaf, path->slots[0],
  3305. struct btrfs_file_extent_item);
  3306. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  3307. btrfs_set_file_extent_type(leaf, ei,
  3308. BTRFS_FILE_EXTENT_INLINE);
  3309. ptr = btrfs_file_extent_inline_start(ei);
  3310. write_extent_buffer(leaf, symname, ptr, name_len);
  3311. btrfs_mark_buffer_dirty(leaf);
  3312. btrfs_free_path(path);
  3313. inode->i_op = &btrfs_symlink_inode_operations;
  3314. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  3315. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3316. btrfs_i_size_write(inode, name_len - 1);
  3317. err = btrfs_update_inode(trans, root, inode);
  3318. if (err)
  3319. drop_inode = 1;
  3320. out_unlock:
  3321. nr = trans->blocks_used;
  3322. btrfs_end_transaction_throttle(trans, root);
  3323. out_fail:
  3324. if (drop_inode) {
  3325. inode_dec_link_count(inode);
  3326. iput(inode);
  3327. }
  3328. btrfs_btree_balance_dirty(root, nr);
  3329. return err;
  3330. }
  3331. static int btrfs_set_page_dirty(struct page *page)
  3332. {
  3333. return __set_page_dirty_nobuffers(page);
  3334. }
  3335. static int btrfs_permission(struct inode *inode, int mask)
  3336. {
  3337. if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
  3338. return -EACCES;
  3339. return generic_permission(inode, mask, btrfs_check_acl);
  3340. }
  3341. static struct inode_operations btrfs_dir_inode_operations = {
  3342. .lookup = btrfs_lookup,
  3343. .create = btrfs_create,
  3344. .unlink = btrfs_unlink,
  3345. .link = btrfs_link,
  3346. .mkdir = btrfs_mkdir,
  3347. .rmdir = btrfs_rmdir,
  3348. .rename = btrfs_rename,
  3349. .symlink = btrfs_symlink,
  3350. .setattr = btrfs_setattr,
  3351. .mknod = btrfs_mknod,
  3352. .setxattr = btrfs_setxattr,
  3353. .getxattr = btrfs_getxattr,
  3354. .listxattr = btrfs_listxattr,
  3355. .removexattr = btrfs_removexattr,
  3356. .permission = btrfs_permission,
  3357. };
  3358. static struct inode_operations btrfs_dir_ro_inode_operations = {
  3359. .lookup = btrfs_lookup,
  3360. .permission = btrfs_permission,
  3361. };
  3362. static struct file_operations btrfs_dir_file_operations = {
  3363. .llseek = generic_file_llseek,
  3364. .read = generic_read_dir,
  3365. .readdir = btrfs_real_readdir,
  3366. .unlocked_ioctl = btrfs_ioctl,
  3367. #ifdef CONFIG_COMPAT
  3368. .compat_ioctl = btrfs_ioctl,
  3369. #endif
  3370. .release = btrfs_release_file,
  3371. .fsync = btrfs_sync_file,
  3372. };
  3373. static struct extent_io_ops btrfs_extent_io_ops = {
  3374. .fill_delalloc = run_delalloc_range,
  3375. .submit_bio_hook = btrfs_submit_bio_hook,
  3376. .merge_bio_hook = btrfs_merge_bio_hook,
  3377. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  3378. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  3379. .writepage_start_hook = btrfs_writepage_start_hook,
  3380. .readpage_io_failed_hook = btrfs_io_failed_hook,
  3381. .set_bit_hook = btrfs_set_bit_hook,
  3382. .clear_bit_hook = btrfs_clear_bit_hook,
  3383. };
  3384. static struct address_space_operations btrfs_aops = {
  3385. .readpage = btrfs_readpage,
  3386. .writepage = btrfs_writepage,
  3387. .writepages = btrfs_writepages,
  3388. .readpages = btrfs_readpages,
  3389. .sync_page = block_sync_page,
  3390. .bmap = btrfs_bmap,
  3391. .direct_IO = btrfs_direct_IO,
  3392. .invalidatepage = btrfs_invalidatepage,
  3393. .releasepage = btrfs_releasepage,
  3394. .set_page_dirty = btrfs_set_page_dirty,
  3395. };
  3396. static struct address_space_operations btrfs_symlink_aops = {
  3397. .readpage = btrfs_readpage,
  3398. .writepage = btrfs_writepage,
  3399. .invalidatepage = btrfs_invalidatepage,
  3400. .releasepage = btrfs_releasepage,
  3401. };
  3402. static struct inode_operations btrfs_file_inode_operations = {
  3403. .truncate = btrfs_truncate,
  3404. .getattr = btrfs_getattr,
  3405. .setattr = btrfs_setattr,
  3406. .setxattr = btrfs_setxattr,
  3407. .getxattr = btrfs_getxattr,
  3408. .listxattr = btrfs_listxattr,
  3409. .removexattr = btrfs_removexattr,
  3410. .permission = btrfs_permission,
  3411. };
  3412. static struct inode_operations btrfs_special_inode_operations = {
  3413. .getattr = btrfs_getattr,
  3414. .setattr = btrfs_setattr,
  3415. .permission = btrfs_permission,
  3416. .setxattr = btrfs_setxattr,
  3417. .getxattr = btrfs_getxattr,
  3418. .listxattr = btrfs_listxattr,
  3419. .removexattr = btrfs_removexattr,
  3420. };
  3421. static struct inode_operations btrfs_symlink_inode_operations = {
  3422. .readlink = generic_readlink,
  3423. .follow_link = page_follow_link_light,
  3424. .put_link = page_put_link,
  3425. .permission = btrfs_permission,
  3426. };