inode.c 76 KB

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