inode.c 94 KB

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