inode.c 76 KB

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