inode.c 88 KB

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