inode.c 97 KB

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