inode.c 79 KB

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