inode.c 84 KB

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