inode.c 86 KB

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