inode.c 85 KB

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