inode.c 85 KB

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