inode.c 95 KB

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