inode.c 96 KB

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