inode.c 88 KB

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