inode.c 78 KB

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