inode.c 78 KB

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