inode.c 97 KB

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