inode.c 78 KB

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