inode.c 75 KB

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