inode.c 78 KB

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