inode.c 78 KB

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