inode.c 85 KB

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