inode.c 96 KB

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