inode.c 97 KB

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