inode.c 85 KB

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