inode.c 94 KB

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