inode.c 90 KB

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