inode.c 75 KB

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