inode.c 86 KB

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