inode.c 79 KB

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