inode.c 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002
  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/kernel.h>
  19. #include <linux/bio.h>
  20. #include <linux/buffer_head.h>
  21. #include <linux/file.h>
  22. #include <linux/fs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/highmem.h>
  25. #include <linux/time.h>
  26. #include <linux/init.h>
  27. #include <linux/string.h>
  28. #include <linux/smp_lock.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/mpage.h>
  31. #include <linux/swap.h>
  32. #include <linux/writeback.h>
  33. #include <linux/statfs.h>
  34. #include <linux/compat.h>
  35. #include <linux/bit_spinlock.h>
  36. #include <linux/version.h>
  37. #include <linux/xattr.h>
  38. #include <linux/posix_acl.h>
  39. #include <linux/falloc.h>
  40. #include "compat.h"
  41. #include "ctree.h"
  42. #include "disk-io.h"
  43. #include "transaction.h"
  44. #include "btrfs_inode.h"
  45. #include "ioctl.h"
  46. #include "print-tree.h"
  47. #include "volumes.h"
  48. #include "ordered-data.h"
  49. #include "xattr.h"
  50. #include "tree-log.h"
  51. #include "ref-cache.h"
  52. #include "compression.h"
  53. struct btrfs_iget_args {
  54. u64 ino;
  55. struct btrfs_root *root;
  56. };
  57. static struct inode_operations btrfs_dir_inode_operations;
  58. static struct inode_operations btrfs_symlink_inode_operations;
  59. static struct inode_operations btrfs_dir_ro_inode_operations;
  60. static struct inode_operations btrfs_special_inode_operations;
  61. static struct inode_operations btrfs_file_inode_operations;
  62. static struct address_space_operations btrfs_aops;
  63. static struct address_space_operations btrfs_symlink_aops;
  64. static struct file_operations btrfs_dir_file_operations;
  65. static struct extent_io_ops btrfs_extent_io_ops;
  66. static struct kmem_cache *btrfs_inode_cachep;
  67. struct kmem_cache *btrfs_trans_handle_cachep;
  68. struct kmem_cache *btrfs_transaction_cachep;
  69. struct kmem_cache *btrfs_bit_radix_cachep;
  70. struct kmem_cache *btrfs_path_cachep;
  71. #define S_SHIFT 12
  72. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  73. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  74. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  75. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  76. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  77. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  78. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  79. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  80. };
  81. static void btrfs_truncate(struct inode *inode);
  82. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
  83. static noinline int cow_file_range(struct inode *inode,
  84. struct page *locked_page,
  85. u64 start, u64 end, int *page_started,
  86. unsigned long *nr_written, int unlock);
  87. /*
  88. * a very lame attempt at stopping writes when the FS is 85% full. There
  89. * are countless ways this is incorrect, but it is better than nothing.
  90. */
  91. int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
  92. int for_del)
  93. {
  94. u64 total;
  95. u64 used;
  96. u64 thresh;
  97. unsigned long flags;
  98. int ret = 0;
  99. spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
  100. total = btrfs_super_total_bytes(&root->fs_info->super_copy);
  101. used = btrfs_super_bytes_used(&root->fs_info->super_copy);
  102. if (for_del)
  103. thresh = total * 90;
  104. else
  105. thresh = total * 85;
  106. do_div(thresh, 100);
  107. if (used + root->fs_info->delalloc_bytes + num_required > thresh)
  108. ret = -ENOSPC;
  109. spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
  110. return ret;
  111. }
  112. /*
  113. * this does all the hard work for inserting an inline extent into
  114. * the btree. The caller should have done a btrfs_drop_extents so that
  115. * no overlapping inline items exist in the btree
  116. */
  117. static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
  118. struct btrfs_root *root, struct inode *inode,
  119. u64 start, size_t size, size_t compressed_size,
  120. struct page **compressed_pages)
  121. {
  122. struct btrfs_key key;
  123. struct btrfs_path *path;
  124. struct extent_buffer *leaf;
  125. struct page *page = NULL;
  126. char *kaddr;
  127. unsigned long ptr;
  128. struct btrfs_file_extent_item *ei;
  129. int err = 0;
  130. int ret;
  131. size_t cur_size = size;
  132. size_t datasize;
  133. unsigned long offset;
  134. int use_compress = 0;
  135. if (compressed_size && compressed_pages) {
  136. use_compress = 1;
  137. cur_size = compressed_size;
  138. }
  139. path = btrfs_alloc_path(); if (!path)
  140. return -ENOMEM;
  141. btrfs_set_trans_block_group(trans, inode);
  142. key.objectid = inode->i_ino;
  143. key.offset = start;
  144. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  145. inode_add_bytes(inode, size);
  146. datasize = btrfs_file_extent_calc_inline_size(cur_size);
  147. inode_add_bytes(inode, size);
  148. ret = btrfs_insert_empty_item(trans, root, path, &key,
  149. datasize);
  150. BUG_ON(ret);
  151. if (ret) {
  152. err = ret;
  153. printk("got bad ret %d\n", ret);
  154. goto fail;
  155. }
  156. leaf = path->nodes[0];
  157. ei = btrfs_item_ptr(leaf, path->slots[0],
  158. struct btrfs_file_extent_item);
  159. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  160. btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
  161. btrfs_set_file_extent_encryption(leaf, ei, 0);
  162. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  163. btrfs_set_file_extent_ram_bytes(leaf, ei, size);
  164. ptr = btrfs_file_extent_inline_start(ei);
  165. if (use_compress) {
  166. struct page *cpage;
  167. int i = 0;
  168. while(compressed_size > 0) {
  169. cpage = compressed_pages[i];
  170. cur_size = min_t(unsigned long, compressed_size,
  171. PAGE_CACHE_SIZE);
  172. kaddr = kmap(cpage);
  173. write_extent_buffer(leaf, kaddr, ptr, cur_size);
  174. kunmap(cpage);
  175. i++;
  176. ptr += cur_size;
  177. compressed_size -= cur_size;
  178. }
  179. btrfs_set_file_extent_compression(leaf, ei,
  180. BTRFS_COMPRESS_ZLIB);
  181. } else {
  182. page = find_get_page(inode->i_mapping,
  183. start >> PAGE_CACHE_SHIFT);
  184. btrfs_set_file_extent_compression(leaf, ei, 0);
  185. kaddr = kmap_atomic(page, KM_USER0);
  186. offset = start & (PAGE_CACHE_SIZE - 1);
  187. write_extent_buffer(leaf, kaddr + offset, ptr, size);
  188. kunmap_atomic(kaddr, KM_USER0);
  189. page_cache_release(page);
  190. }
  191. btrfs_mark_buffer_dirty(leaf);
  192. btrfs_free_path(path);
  193. BTRFS_I(inode)->disk_i_size = inode->i_size;
  194. btrfs_update_inode(trans, root, inode);
  195. return 0;
  196. fail:
  197. btrfs_free_path(path);
  198. return err;
  199. }
  200. /*
  201. * conditionally insert an inline extent into the file. This
  202. * does the checks required to make sure the data is small enough
  203. * to fit as an inline extent.
  204. */
  205. static int cow_file_range_inline(struct btrfs_trans_handle *trans,
  206. struct btrfs_root *root,
  207. struct inode *inode, u64 start, u64 end,
  208. size_t compressed_size,
  209. struct page **compressed_pages)
  210. {
  211. u64 isize = i_size_read(inode);
  212. u64 actual_end = min(end + 1, isize);
  213. u64 inline_len = actual_end - start;
  214. u64 aligned_end = (end + root->sectorsize - 1) &
  215. ~((u64)root->sectorsize - 1);
  216. u64 hint_byte;
  217. u64 data_len = inline_len;
  218. int ret;
  219. if (compressed_size)
  220. data_len = compressed_size;
  221. if (start > 0 ||
  222. actual_end >= PAGE_CACHE_SIZE ||
  223. data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
  224. (!compressed_size &&
  225. (actual_end & (root->sectorsize - 1)) == 0) ||
  226. end + 1 < isize ||
  227. data_len > root->fs_info->max_inline) {
  228. return 1;
  229. }
  230. ret = btrfs_drop_extents(trans, root, inode, start,
  231. aligned_end, start, &hint_byte);
  232. BUG_ON(ret);
  233. if (isize > actual_end)
  234. inline_len = min_t(u64, isize, actual_end);
  235. ret = insert_inline_extent(trans, root, inode, start,
  236. inline_len, compressed_size,
  237. compressed_pages);
  238. BUG_ON(ret);
  239. btrfs_drop_extent_cache(inode, start, aligned_end, 0);
  240. return 0;
  241. }
  242. struct async_extent {
  243. u64 start;
  244. u64 ram_size;
  245. u64 compressed_size;
  246. struct page **pages;
  247. unsigned long nr_pages;
  248. struct list_head list;
  249. };
  250. struct async_cow {
  251. struct inode *inode;
  252. struct btrfs_root *root;
  253. struct page *locked_page;
  254. u64 start;
  255. u64 end;
  256. struct list_head extents;
  257. struct btrfs_work work;
  258. };
  259. static noinline int add_async_extent(struct async_cow *cow,
  260. u64 start, u64 ram_size,
  261. u64 compressed_size,
  262. struct page **pages,
  263. unsigned long nr_pages)
  264. {
  265. struct async_extent *async_extent;
  266. async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
  267. async_extent->start = start;
  268. async_extent->ram_size = ram_size;
  269. async_extent->compressed_size = compressed_size;
  270. async_extent->pages = pages;
  271. async_extent->nr_pages = nr_pages;
  272. list_add_tail(&async_extent->list, &cow->extents);
  273. return 0;
  274. }
  275. /*
  276. * we create compressed extents in two phases. The first
  277. * phase compresses a range of pages that have already been
  278. * locked (both pages and state bits are locked).
  279. *
  280. * This is done inside an ordered work queue, and the compression
  281. * is spread across many cpus. The actual IO submission is step
  282. * two, and the ordered work queue takes care of making sure that
  283. * happens in the same order things were put onto the queue by
  284. * writepages and friends.
  285. *
  286. * If this code finds it can't get good compression, it puts an
  287. * entry onto the work queue to write the uncompressed bytes. This
  288. * makes sure that both compressed inodes and uncompressed inodes
  289. * are written in the same order that pdflush sent them down.
  290. */
  291. static noinline int compress_file_range(struct inode *inode,
  292. struct page *locked_page,
  293. u64 start, u64 end,
  294. struct async_cow *async_cow,
  295. int *num_added)
  296. {
  297. struct btrfs_root *root = BTRFS_I(inode)->root;
  298. struct btrfs_trans_handle *trans;
  299. u64 num_bytes;
  300. u64 orig_start;
  301. u64 disk_num_bytes;
  302. u64 blocksize = root->sectorsize;
  303. u64 actual_end;
  304. int ret = 0;
  305. struct page **pages = NULL;
  306. unsigned long nr_pages;
  307. unsigned long nr_pages_ret = 0;
  308. unsigned long total_compressed = 0;
  309. unsigned long total_in = 0;
  310. unsigned long max_compressed = 128 * 1024;
  311. unsigned long max_uncompressed = 128 * 1024;
  312. int i;
  313. int will_compress;
  314. orig_start = start;
  315. again:
  316. will_compress = 0;
  317. nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
  318. nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
  319. actual_end = min_t(u64, i_size_read(inode), end + 1);
  320. total_compressed = actual_end - start;
  321. /* we want to make sure that amount of ram required to uncompress
  322. * an extent is reasonable, so we limit the total size in ram
  323. * of a compressed extent to 128k. This is a crucial number
  324. * because it also controls how easily we can spread reads across
  325. * cpus for decompression.
  326. *
  327. * We also want to make sure the amount of IO required to do
  328. * a random read is reasonably small, so we limit the size of
  329. * a compressed extent to 128k.
  330. */
  331. total_compressed = min(total_compressed, max_uncompressed);
  332. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  333. num_bytes = max(blocksize, num_bytes);
  334. disk_num_bytes = num_bytes;
  335. total_in = 0;
  336. ret = 0;
  337. /*
  338. * we do compression for mount -o compress and when the
  339. * inode has not been flagged as nocompress. This flag can
  340. * change at any time if we discover bad compression ratios.
  341. */
  342. if (!btrfs_test_flag(inode, NOCOMPRESS) &&
  343. btrfs_test_opt(root, COMPRESS)) {
  344. WARN_ON(pages);
  345. pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
  346. ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
  347. total_compressed, pages,
  348. nr_pages, &nr_pages_ret,
  349. &total_in,
  350. &total_compressed,
  351. max_compressed);
  352. if (!ret) {
  353. unsigned long offset = total_compressed &
  354. (PAGE_CACHE_SIZE - 1);
  355. struct page *page = pages[nr_pages_ret - 1];
  356. char *kaddr;
  357. /* zero the tail end of the last page, we might be
  358. * sending it down to disk
  359. */
  360. if (offset) {
  361. kaddr = kmap_atomic(page, KM_USER0);
  362. memset(kaddr + offset, 0,
  363. PAGE_CACHE_SIZE - offset);
  364. kunmap_atomic(kaddr, KM_USER0);
  365. }
  366. will_compress = 1;
  367. }
  368. }
  369. if (start == 0) {
  370. trans = btrfs_join_transaction(root, 1);
  371. BUG_ON(!trans);
  372. btrfs_set_trans_block_group(trans, inode);
  373. /* lets try to make an inline extent */
  374. if (ret || total_in < (actual_end - start)) {
  375. /* we didn't compress the entire range, try
  376. * to make an uncompressed inline extent.
  377. */
  378. ret = cow_file_range_inline(trans, root, inode,
  379. start, end, 0, NULL);
  380. } else {
  381. /* try making a compressed inline extent */
  382. ret = cow_file_range_inline(trans, root, inode,
  383. start, end,
  384. total_compressed, pages);
  385. }
  386. btrfs_end_transaction(trans, root);
  387. if (ret == 0) {
  388. /*
  389. * inline extent creation worked, we don't need
  390. * to create any more async work items. Unlock
  391. * and free up our temp pages.
  392. */
  393. extent_clear_unlock_delalloc(inode,
  394. &BTRFS_I(inode)->io_tree,
  395. start, end, NULL, 1, 0,
  396. 0, 1, 1, 1);
  397. ret = 0;
  398. goto free_pages_out;
  399. }
  400. }
  401. if (will_compress) {
  402. /*
  403. * we aren't doing an inline extent round the compressed size
  404. * up to a block size boundary so the allocator does sane
  405. * things
  406. */
  407. total_compressed = (total_compressed + blocksize - 1) &
  408. ~(blocksize - 1);
  409. /*
  410. * one last check to make sure the compression is really a
  411. * win, compare the page count read with the blocks on disk
  412. */
  413. total_in = (total_in + PAGE_CACHE_SIZE - 1) &
  414. ~(PAGE_CACHE_SIZE - 1);
  415. if (total_compressed >= total_in) {
  416. will_compress = 0;
  417. } else {
  418. disk_num_bytes = total_compressed;
  419. num_bytes = total_in;
  420. }
  421. }
  422. if (!will_compress && pages) {
  423. /*
  424. * the compression code ran but failed to make things smaller,
  425. * free any pages it allocated and our page pointer array
  426. */
  427. for (i = 0; i < nr_pages_ret; i++) {
  428. WARN_ON(pages[i]->mapping);
  429. page_cache_release(pages[i]);
  430. }
  431. kfree(pages);
  432. pages = NULL;
  433. total_compressed = 0;
  434. nr_pages_ret = 0;
  435. /* flag the file so we don't compress in the future */
  436. btrfs_set_flag(inode, NOCOMPRESS);
  437. }
  438. if (will_compress) {
  439. *num_added += 1;
  440. /* the async work queues will take care of doing actual
  441. * allocation on disk for these compressed pages,
  442. * and will submit them to the elevator.
  443. */
  444. add_async_extent(async_cow, start, num_bytes,
  445. total_compressed, pages, nr_pages_ret);
  446. if (start + num_bytes < end) {
  447. start += num_bytes;
  448. pages = NULL;
  449. cond_resched();
  450. goto again;
  451. }
  452. } else {
  453. /*
  454. * No compression, but we still need to write the pages in
  455. * the file we've been given so far. redirty the locked
  456. * page if it corresponds to our extent and set things up
  457. * for the async work queue to run cow_file_range to do
  458. * the normal delalloc dance
  459. */
  460. if (page_offset(locked_page) >= start &&
  461. page_offset(locked_page) <= end) {
  462. __set_page_dirty_nobuffers(locked_page);
  463. /* unlocked later on in the async handlers */
  464. }
  465. add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
  466. *num_added += 1;
  467. }
  468. out:
  469. return 0;
  470. free_pages_out:
  471. for (i = 0; i < nr_pages_ret; i++) {
  472. WARN_ON(pages[i]->mapping);
  473. page_cache_release(pages[i]);
  474. }
  475. if (pages)
  476. kfree(pages);
  477. goto out;
  478. }
  479. /*
  480. * phase two of compressed writeback. This is the ordered portion
  481. * of the code, which only gets called in the order the work was
  482. * queued. We walk all the async extents created by compress_file_range
  483. * and send them down to the disk.
  484. */
  485. static noinline int submit_compressed_extents(struct inode *inode,
  486. struct async_cow *async_cow)
  487. {
  488. struct async_extent *async_extent;
  489. u64 alloc_hint = 0;
  490. struct btrfs_trans_handle *trans;
  491. struct btrfs_key ins;
  492. struct extent_map *em;
  493. struct btrfs_root *root = BTRFS_I(inode)->root;
  494. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  495. struct extent_io_tree *io_tree;
  496. int ret;
  497. if (list_empty(&async_cow->extents))
  498. return 0;
  499. trans = btrfs_join_transaction(root, 1);
  500. while(!list_empty(&async_cow->extents)) {
  501. async_extent = list_entry(async_cow->extents.next,
  502. struct async_extent, list);
  503. list_del(&async_extent->list);
  504. io_tree = &BTRFS_I(inode)->io_tree;
  505. /* did the compression code fall back to uncompressed IO? */
  506. if (!async_extent->pages) {
  507. int page_started = 0;
  508. unsigned long nr_written = 0;
  509. lock_extent(io_tree, async_extent->start,
  510. async_extent->start + async_extent->ram_size - 1,
  511. GFP_NOFS);
  512. /* allocate blocks */
  513. cow_file_range(inode, async_cow->locked_page,
  514. async_extent->start,
  515. async_extent->start +
  516. async_extent->ram_size - 1,
  517. &page_started, &nr_written, 0);
  518. /*
  519. * if page_started, cow_file_range inserted an
  520. * inline extent and took care of all the unlocking
  521. * and IO for us. Otherwise, we need to submit
  522. * all those pages down to the drive.
  523. */
  524. if (!page_started)
  525. extent_write_locked_range(io_tree,
  526. inode, async_extent->start,
  527. async_extent->start +
  528. async_extent->ram_size - 1,
  529. btrfs_get_extent,
  530. WB_SYNC_ALL);
  531. kfree(async_extent);
  532. cond_resched();
  533. continue;
  534. }
  535. lock_extent(io_tree, async_extent->start,
  536. async_extent->start + async_extent->ram_size - 1,
  537. GFP_NOFS);
  538. /*
  539. * here we're doing allocation and writeback of the
  540. * compressed pages
  541. */
  542. btrfs_drop_extent_cache(inode, async_extent->start,
  543. async_extent->start +
  544. async_extent->ram_size - 1, 0);
  545. ret = btrfs_reserve_extent(trans, root,
  546. async_extent->compressed_size,
  547. async_extent->compressed_size,
  548. 0, alloc_hint,
  549. (u64)-1, &ins, 1);
  550. BUG_ON(ret);
  551. em = alloc_extent_map(GFP_NOFS);
  552. em->start = async_extent->start;
  553. em->len = async_extent->ram_size;
  554. em->orig_start = em->start;
  555. em->block_start = ins.objectid;
  556. em->block_len = ins.offset;
  557. em->bdev = root->fs_info->fs_devices->latest_bdev;
  558. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  559. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  560. while(1) {
  561. spin_lock(&em_tree->lock);
  562. ret = add_extent_mapping(em_tree, em);
  563. spin_unlock(&em_tree->lock);
  564. if (ret != -EEXIST) {
  565. free_extent_map(em);
  566. break;
  567. }
  568. btrfs_drop_extent_cache(inode, async_extent->start,
  569. async_extent->start +
  570. async_extent->ram_size - 1, 0);
  571. }
  572. ret = btrfs_add_ordered_extent(inode, async_extent->start,
  573. ins.objectid,
  574. async_extent->ram_size,
  575. ins.offset,
  576. BTRFS_ORDERED_COMPRESSED);
  577. BUG_ON(ret);
  578. btrfs_end_transaction(trans, root);
  579. /*
  580. * clear dirty, set writeback and unlock the pages.
  581. */
  582. extent_clear_unlock_delalloc(inode,
  583. &BTRFS_I(inode)->io_tree,
  584. async_extent->start,
  585. async_extent->start +
  586. async_extent->ram_size - 1,
  587. NULL, 1, 1, 0, 1, 1, 0);
  588. ret = btrfs_submit_compressed_write(inode,
  589. async_extent->start,
  590. async_extent->ram_size,
  591. ins.objectid,
  592. ins.offset, async_extent->pages,
  593. async_extent->nr_pages);
  594. BUG_ON(ret);
  595. trans = btrfs_join_transaction(root, 1);
  596. alloc_hint = ins.objectid + ins.offset;
  597. kfree(async_extent);
  598. cond_resched();
  599. }
  600. btrfs_end_transaction(trans, root);
  601. return 0;
  602. }
  603. /*
  604. * when extent_io.c finds a delayed allocation range in the file,
  605. * the call backs end up in this code. The basic idea is to
  606. * allocate extents on disk for the range, and create ordered data structs
  607. * in ram to track those extents.
  608. *
  609. * locked_page is the page that writepage had locked already. We use
  610. * it to make sure we don't do extra locks or unlocks.
  611. *
  612. * *page_started is set to one if we unlock locked_page and do everything
  613. * required to start IO on it. It may be clean and already done with
  614. * IO when we return.
  615. */
  616. static noinline int cow_file_range(struct inode *inode,
  617. struct page *locked_page,
  618. u64 start, u64 end, int *page_started,
  619. unsigned long *nr_written,
  620. int unlock)
  621. {
  622. struct btrfs_root *root = BTRFS_I(inode)->root;
  623. struct btrfs_trans_handle *trans;
  624. u64 alloc_hint = 0;
  625. u64 num_bytes;
  626. unsigned long ram_size;
  627. u64 disk_num_bytes;
  628. u64 cur_alloc_size;
  629. u64 blocksize = root->sectorsize;
  630. u64 actual_end;
  631. struct btrfs_key ins;
  632. struct extent_map *em;
  633. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  634. int ret = 0;
  635. trans = btrfs_join_transaction(root, 1);
  636. BUG_ON(!trans);
  637. btrfs_set_trans_block_group(trans, inode);
  638. actual_end = min_t(u64, i_size_read(inode), end + 1);
  639. num_bytes = (end - start + blocksize) & ~(blocksize - 1);
  640. num_bytes = max(blocksize, num_bytes);
  641. disk_num_bytes = num_bytes;
  642. ret = 0;
  643. if (start == 0) {
  644. /* lets try to make an inline extent */
  645. ret = cow_file_range_inline(trans, root, inode,
  646. start, end, 0, NULL);
  647. if (ret == 0) {
  648. extent_clear_unlock_delalloc(inode,
  649. &BTRFS_I(inode)->io_tree,
  650. start, end, NULL, 1, 1,
  651. 1, 1, 1, 1);
  652. *nr_written = *nr_written +
  653. (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
  654. *page_started = 1;
  655. ret = 0;
  656. goto out;
  657. }
  658. }
  659. BUG_ON(disk_num_bytes >
  660. btrfs_super_total_bytes(&root->fs_info->super_copy));
  661. btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
  662. while(disk_num_bytes > 0) {
  663. cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
  664. ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
  665. root->sectorsize, 0, alloc_hint,
  666. (u64)-1, &ins, 1);
  667. if (ret) {
  668. BUG();
  669. }
  670. em = alloc_extent_map(GFP_NOFS);
  671. em->start = start;
  672. em->orig_start = em->start;
  673. ram_size = ins.offset;
  674. em->len = ins.offset;
  675. em->block_start = ins.objectid;
  676. em->block_len = ins.offset;
  677. em->bdev = root->fs_info->fs_devices->latest_bdev;
  678. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  679. while(1) {
  680. spin_lock(&em_tree->lock);
  681. ret = add_extent_mapping(em_tree, em);
  682. spin_unlock(&em_tree->lock);
  683. if (ret != -EEXIST) {
  684. free_extent_map(em);
  685. break;
  686. }
  687. btrfs_drop_extent_cache(inode, start,
  688. start + ram_size - 1, 0);
  689. }
  690. cur_alloc_size = ins.offset;
  691. ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
  692. ram_size, cur_alloc_size, 0);
  693. BUG_ON(ret);
  694. if (disk_num_bytes < cur_alloc_size) {
  695. printk("num_bytes %Lu cur_alloc %Lu\n", disk_num_bytes,
  696. cur_alloc_size);
  697. break;
  698. }
  699. /* we're not doing compressed IO, don't unlock the first
  700. * page (which the caller expects to stay locked), don't
  701. * clear any dirty bits and don't set any writeback bits
  702. */
  703. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  704. start, start + ram_size - 1,
  705. locked_page, unlock, 1,
  706. 1, 0, 0, 0);
  707. disk_num_bytes -= cur_alloc_size;
  708. num_bytes -= cur_alloc_size;
  709. alloc_hint = ins.objectid + ins.offset;
  710. start += cur_alloc_size;
  711. }
  712. out:
  713. ret = 0;
  714. btrfs_end_transaction(trans, root);
  715. return ret;
  716. }
  717. /*
  718. * work queue call back to started compression on a file and pages
  719. */
  720. static noinline void async_cow_start(struct btrfs_work *work)
  721. {
  722. struct async_cow *async_cow;
  723. int num_added = 0;
  724. async_cow = container_of(work, struct async_cow, work);
  725. compress_file_range(async_cow->inode, async_cow->locked_page,
  726. async_cow->start, async_cow->end, async_cow,
  727. &num_added);
  728. if (num_added == 0)
  729. async_cow->inode = NULL;
  730. }
  731. /*
  732. * work queue call back to submit previously compressed pages
  733. */
  734. static noinline void async_cow_submit(struct btrfs_work *work)
  735. {
  736. struct async_cow *async_cow;
  737. struct btrfs_root *root;
  738. unsigned long nr_pages;
  739. async_cow = container_of(work, struct async_cow, work);
  740. root = async_cow->root;
  741. nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
  742. PAGE_CACHE_SHIFT;
  743. atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
  744. if (atomic_read(&root->fs_info->async_delalloc_pages) <
  745. 5 * 1042 * 1024 &&
  746. waitqueue_active(&root->fs_info->async_submit_wait))
  747. wake_up(&root->fs_info->async_submit_wait);
  748. if (async_cow->inode) {
  749. submit_compressed_extents(async_cow->inode, async_cow);
  750. }
  751. }
  752. static noinline void async_cow_free(struct btrfs_work *work)
  753. {
  754. struct async_cow *async_cow;
  755. async_cow = container_of(work, struct async_cow, work);
  756. kfree(async_cow);
  757. }
  758. static int cow_file_range_async(struct inode *inode, struct page *locked_page,
  759. u64 start, u64 end, int *page_started,
  760. unsigned long *nr_written)
  761. {
  762. struct async_cow *async_cow;
  763. struct btrfs_root *root = BTRFS_I(inode)->root;
  764. unsigned long nr_pages;
  765. u64 cur_end;
  766. int limit = 10 * 1024 * 1042;
  767. if (!btrfs_test_opt(root, COMPRESS)) {
  768. return cow_file_range(inode, locked_page, start, end,
  769. page_started, nr_written, 1);
  770. }
  771. clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
  772. EXTENT_DELALLOC, 1, 0, GFP_NOFS);
  773. while(start < end) {
  774. async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
  775. async_cow->inode = inode;
  776. async_cow->root = root;
  777. async_cow->locked_page = locked_page;
  778. async_cow->start = start;
  779. if (btrfs_test_flag(inode, NOCOMPRESS))
  780. cur_end = end;
  781. else
  782. cur_end = min(end, start + 512 * 1024 - 1);
  783. async_cow->end = cur_end;
  784. INIT_LIST_HEAD(&async_cow->extents);
  785. async_cow->work.func = async_cow_start;
  786. async_cow->work.ordered_func = async_cow_submit;
  787. async_cow->work.ordered_free = async_cow_free;
  788. async_cow->work.flags = 0;
  789. nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
  790. PAGE_CACHE_SHIFT;
  791. atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
  792. btrfs_queue_worker(&root->fs_info->delalloc_workers,
  793. &async_cow->work);
  794. if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
  795. wait_event(root->fs_info->async_submit_wait,
  796. (atomic_read(&root->fs_info->async_delalloc_pages) <
  797. limit));
  798. }
  799. while(atomic_read(&root->fs_info->async_submit_draining) &&
  800. atomic_read(&root->fs_info->async_delalloc_pages)) {
  801. wait_event(root->fs_info->async_submit_wait,
  802. (atomic_read(&root->fs_info->async_delalloc_pages) ==
  803. 0));
  804. }
  805. *nr_written += nr_pages;
  806. start = cur_end + 1;
  807. }
  808. *page_started = 1;
  809. return 0;
  810. }
  811. /*
  812. * when nowcow writeback call back. This checks for snapshots or COW copies
  813. * of the extents that exist in the file, and COWs the file as required.
  814. *
  815. * If no cow copies or snapshots exist, we write directly to the existing
  816. * blocks on disk
  817. */
  818. static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
  819. u64 start, u64 end, int *page_started, int force,
  820. unsigned long *nr_written)
  821. {
  822. struct btrfs_root *root = BTRFS_I(inode)->root;
  823. struct btrfs_trans_handle *trans;
  824. struct extent_buffer *leaf;
  825. struct btrfs_path *path;
  826. struct btrfs_file_extent_item *fi;
  827. struct btrfs_key found_key;
  828. u64 cow_start;
  829. u64 cur_offset;
  830. u64 extent_end;
  831. u64 disk_bytenr;
  832. u64 num_bytes;
  833. int extent_type;
  834. int ret;
  835. int type;
  836. int nocow;
  837. int check_prev = 1;
  838. path = btrfs_alloc_path();
  839. BUG_ON(!path);
  840. trans = btrfs_join_transaction(root, 1);
  841. BUG_ON(!trans);
  842. cow_start = (u64)-1;
  843. cur_offset = start;
  844. while (1) {
  845. ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
  846. cur_offset, 0);
  847. BUG_ON(ret < 0);
  848. if (ret > 0 && path->slots[0] > 0 && check_prev) {
  849. leaf = path->nodes[0];
  850. btrfs_item_key_to_cpu(leaf, &found_key,
  851. path->slots[0] - 1);
  852. if (found_key.objectid == inode->i_ino &&
  853. found_key.type == BTRFS_EXTENT_DATA_KEY)
  854. path->slots[0]--;
  855. }
  856. check_prev = 0;
  857. next_slot:
  858. leaf = path->nodes[0];
  859. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  860. ret = btrfs_next_leaf(root, path);
  861. if (ret < 0)
  862. BUG_ON(1);
  863. if (ret > 0)
  864. break;
  865. leaf = path->nodes[0];
  866. }
  867. nocow = 0;
  868. disk_bytenr = 0;
  869. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  870. if (found_key.objectid > inode->i_ino ||
  871. found_key.type > BTRFS_EXTENT_DATA_KEY ||
  872. found_key.offset > end)
  873. break;
  874. if (found_key.offset > cur_offset) {
  875. extent_end = found_key.offset;
  876. goto out_check;
  877. }
  878. fi = btrfs_item_ptr(leaf, path->slots[0],
  879. struct btrfs_file_extent_item);
  880. extent_type = btrfs_file_extent_type(leaf, fi);
  881. if (extent_type == BTRFS_FILE_EXTENT_REG ||
  882. extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  883. struct btrfs_block_group_cache *block_group;
  884. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  885. extent_end = found_key.offset +
  886. btrfs_file_extent_num_bytes(leaf, fi);
  887. if (extent_end <= start) {
  888. path->slots[0]++;
  889. goto next_slot;
  890. }
  891. if (btrfs_file_extent_compression(leaf, fi) ||
  892. btrfs_file_extent_encryption(leaf, fi) ||
  893. btrfs_file_extent_other_encoding(leaf, fi))
  894. goto out_check;
  895. if (disk_bytenr == 0)
  896. goto out_check;
  897. if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
  898. goto out_check;
  899. if (btrfs_cross_ref_exist(trans, root, disk_bytenr))
  900. goto out_check;
  901. block_group = btrfs_lookup_block_group(root->fs_info,
  902. disk_bytenr);
  903. if (!block_group || block_group->ro)
  904. goto out_check;
  905. disk_bytenr += btrfs_file_extent_offset(leaf, fi);
  906. nocow = 1;
  907. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  908. extent_end = found_key.offset +
  909. btrfs_file_extent_inline_len(leaf, fi);
  910. extent_end = ALIGN(extent_end, root->sectorsize);
  911. } else {
  912. BUG_ON(1);
  913. }
  914. out_check:
  915. if (extent_end <= start) {
  916. path->slots[0]++;
  917. goto next_slot;
  918. }
  919. if (!nocow) {
  920. if (cow_start == (u64)-1)
  921. cow_start = cur_offset;
  922. cur_offset = extent_end;
  923. if (cur_offset > end)
  924. break;
  925. path->slots[0]++;
  926. goto next_slot;
  927. }
  928. btrfs_release_path(root, path);
  929. if (cow_start != (u64)-1) {
  930. ret = cow_file_range(inode, locked_page, cow_start,
  931. found_key.offset - 1, page_started,
  932. nr_written, 1);
  933. BUG_ON(ret);
  934. cow_start = (u64)-1;
  935. }
  936. disk_bytenr += cur_offset - found_key.offset;
  937. num_bytes = min(end + 1, extent_end) - cur_offset;
  938. if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
  939. struct extent_map *em;
  940. struct extent_map_tree *em_tree;
  941. em_tree = &BTRFS_I(inode)->extent_tree;
  942. em = alloc_extent_map(GFP_NOFS);
  943. em->start = cur_offset;
  944. em->orig_start = em->start;
  945. em->len = num_bytes;
  946. em->block_len = num_bytes;
  947. em->block_start = disk_bytenr;
  948. em->bdev = root->fs_info->fs_devices->latest_bdev;
  949. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  950. while (1) {
  951. spin_lock(&em_tree->lock);
  952. ret = add_extent_mapping(em_tree, em);
  953. spin_unlock(&em_tree->lock);
  954. if (ret != -EEXIST) {
  955. free_extent_map(em);
  956. break;
  957. }
  958. btrfs_drop_extent_cache(inode, em->start,
  959. em->start + em->len - 1, 0);
  960. }
  961. type = BTRFS_ORDERED_PREALLOC;
  962. } else {
  963. type = BTRFS_ORDERED_NOCOW;
  964. }
  965. ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
  966. num_bytes, num_bytes, type);
  967. BUG_ON(ret);
  968. extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
  969. cur_offset, cur_offset + num_bytes - 1,
  970. locked_page, 1, 1, 1, 0, 0, 0);
  971. cur_offset = extent_end;
  972. if (cur_offset > end)
  973. break;
  974. }
  975. btrfs_release_path(root, path);
  976. if (cur_offset <= end && cow_start == (u64)-1)
  977. cow_start = cur_offset;
  978. if (cow_start != (u64)-1) {
  979. ret = cow_file_range(inode, locked_page, cow_start, end,
  980. page_started, nr_written, 1);
  981. BUG_ON(ret);
  982. }
  983. ret = btrfs_end_transaction(trans, root);
  984. BUG_ON(ret);
  985. btrfs_free_path(path);
  986. return 0;
  987. }
  988. /*
  989. * extent_io.c call back to do delayed allocation processing
  990. */
  991. static int run_delalloc_range(struct inode *inode, struct page *locked_page,
  992. u64 start, u64 end, int *page_started,
  993. unsigned long *nr_written)
  994. {
  995. struct btrfs_root *root = BTRFS_I(inode)->root;
  996. int ret;
  997. if (btrfs_test_opt(root, NODATACOW) ||
  998. btrfs_test_flag(inode, NODATACOW))
  999. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1000. page_started, 1, nr_written);
  1001. else if (btrfs_test_flag(inode, PREALLOC))
  1002. ret = run_delalloc_nocow(inode, locked_page, start, end,
  1003. page_started, 0, nr_written);
  1004. else
  1005. ret = cow_file_range_async(inode, locked_page, start, end,
  1006. page_started, nr_written);
  1007. return ret;
  1008. }
  1009. /*
  1010. * extent_io.c set_bit_hook, used to track delayed allocation
  1011. * bytes in this file, and to maintain the list of inodes that
  1012. * have pending delalloc work to be done.
  1013. */
  1014. static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
  1015. unsigned long old, unsigned long bits)
  1016. {
  1017. unsigned long flags;
  1018. if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1019. struct btrfs_root *root = BTRFS_I(inode)->root;
  1020. spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
  1021. BTRFS_I(inode)->delalloc_bytes += end - start + 1;
  1022. root->fs_info->delalloc_bytes += end - start + 1;
  1023. if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1024. list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
  1025. &root->fs_info->delalloc_inodes);
  1026. }
  1027. spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
  1028. }
  1029. return 0;
  1030. }
  1031. /*
  1032. * extent_io.c clear_bit_hook, see set_bit_hook for why
  1033. */
  1034. static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
  1035. unsigned long old, unsigned long bits)
  1036. {
  1037. if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
  1038. struct btrfs_root *root = BTRFS_I(inode)->root;
  1039. unsigned long flags;
  1040. spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
  1041. if (end - start + 1 > root->fs_info->delalloc_bytes) {
  1042. printk("warning: delalloc account %Lu %Lu\n",
  1043. end - start + 1, root->fs_info->delalloc_bytes);
  1044. root->fs_info->delalloc_bytes = 0;
  1045. BTRFS_I(inode)->delalloc_bytes = 0;
  1046. } else {
  1047. root->fs_info->delalloc_bytes -= end - start + 1;
  1048. BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
  1049. }
  1050. if (BTRFS_I(inode)->delalloc_bytes == 0 &&
  1051. !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
  1052. list_del_init(&BTRFS_I(inode)->delalloc_inodes);
  1053. }
  1054. spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
  1055. }
  1056. return 0;
  1057. }
  1058. /*
  1059. * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
  1060. * we don't create bios that span stripes or chunks
  1061. */
  1062. int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
  1063. size_t size, struct bio *bio,
  1064. unsigned long bio_flags)
  1065. {
  1066. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1067. struct btrfs_mapping_tree *map_tree;
  1068. u64 logical = (u64)bio->bi_sector << 9;
  1069. u64 length = 0;
  1070. u64 map_length;
  1071. int ret;
  1072. if (bio_flags & EXTENT_BIO_COMPRESSED)
  1073. return 0;
  1074. length = bio->bi_size;
  1075. map_tree = &root->fs_info->mapping_tree;
  1076. map_length = length;
  1077. ret = btrfs_map_block(map_tree, READ, logical,
  1078. &map_length, NULL, 0);
  1079. if (map_length < length + size) {
  1080. return 1;
  1081. }
  1082. return 0;
  1083. }
  1084. /*
  1085. * in order to insert checksums into the metadata in large chunks,
  1086. * we wait until bio submission time. All the pages in the bio are
  1087. * checksummed and sums are attached onto the ordered extent record.
  1088. *
  1089. * At IO completion time the cums attached on the ordered extent record
  1090. * are inserted into the btree
  1091. */
  1092. static int __btrfs_submit_bio_start(struct inode *inode, int rw, struct bio *bio,
  1093. int mirror_num, unsigned long bio_flags)
  1094. {
  1095. struct btrfs_root *root = BTRFS_I(inode)->root;
  1096. int ret = 0;
  1097. ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
  1098. BUG_ON(ret);
  1099. return 0;
  1100. }
  1101. /*
  1102. * in order to insert checksums into the metadata in large chunks,
  1103. * we wait until bio submission time. All the pages in the bio are
  1104. * checksummed and sums are attached onto the ordered extent record.
  1105. *
  1106. * At IO completion time the cums attached on the ordered extent record
  1107. * are inserted into the btree
  1108. */
  1109. static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
  1110. int mirror_num, unsigned long bio_flags)
  1111. {
  1112. struct btrfs_root *root = BTRFS_I(inode)->root;
  1113. return btrfs_map_bio(root, rw, bio, mirror_num, 1);
  1114. }
  1115. /*
  1116. * extent_io.c submission hook. This does the right thing for csum calculation on write,
  1117. * or reading the csums from the tree before a read
  1118. */
  1119. static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
  1120. int mirror_num, unsigned long bio_flags)
  1121. {
  1122. struct btrfs_root *root = BTRFS_I(inode)->root;
  1123. int ret = 0;
  1124. int skip_sum;
  1125. ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
  1126. BUG_ON(ret);
  1127. skip_sum = btrfs_test_opt(root, NODATASUM) ||
  1128. btrfs_test_flag(inode, NODATASUM);
  1129. if (!(rw & (1 << BIO_RW))) {
  1130. if (bio_flags & EXTENT_BIO_COMPRESSED) {
  1131. return btrfs_submit_compressed_read(inode, bio,
  1132. mirror_num, bio_flags);
  1133. } else if (!skip_sum)
  1134. btrfs_lookup_bio_sums(root, inode, bio, NULL);
  1135. goto mapit;
  1136. } else if (!skip_sum) {
  1137. /* we're doing a write, do the async checksumming */
  1138. return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
  1139. inode, rw, bio, mirror_num,
  1140. bio_flags, __btrfs_submit_bio_start,
  1141. __btrfs_submit_bio_done);
  1142. }
  1143. mapit:
  1144. return btrfs_map_bio(root, rw, bio, mirror_num, 0);
  1145. }
  1146. /*
  1147. * given a list of ordered sums record them in the inode. This happens
  1148. * at IO completion time based on sums calculated at bio submission time.
  1149. */
  1150. static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
  1151. struct inode *inode, u64 file_offset,
  1152. struct list_head *list)
  1153. {
  1154. struct list_head *cur;
  1155. struct btrfs_ordered_sum *sum;
  1156. btrfs_set_trans_block_group(trans, inode);
  1157. list_for_each(cur, list) {
  1158. sum = list_entry(cur, struct btrfs_ordered_sum, list);
  1159. btrfs_csum_file_blocks(trans,
  1160. BTRFS_I(inode)->root->fs_info->csum_root, sum);
  1161. }
  1162. return 0;
  1163. }
  1164. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
  1165. {
  1166. if ((end & (PAGE_CACHE_SIZE - 1)) == 0) {
  1167. WARN_ON(1);
  1168. }
  1169. return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
  1170. GFP_NOFS);
  1171. }
  1172. /* see btrfs_writepage_start_hook for details on why this is required */
  1173. struct btrfs_writepage_fixup {
  1174. struct page *page;
  1175. struct btrfs_work work;
  1176. };
  1177. static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
  1178. {
  1179. struct btrfs_writepage_fixup *fixup;
  1180. struct btrfs_ordered_extent *ordered;
  1181. struct page *page;
  1182. struct inode *inode;
  1183. u64 page_start;
  1184. u64 page_end;
  1185. fixup = container_of(work, struct btrfs_writepage_fixup, work);
  1186. page = fixup->page;
  1187. again:
  1188. lock_page(page);
  1189. if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
  1190. ClearPageChecked(page);
  1191. goto out_page;
  1192. }
  1193. inode = page->mapping->host;
  1194. page_start = page_offset(page);
  1195. page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
  1196. lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1197. /* already ordered? We're done */
  1198. if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
  1199. EXTENT_ORDERED, 0)) {
  1200. goto out;
  1201. }
  1202. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  1203. if (ordered) {
  1204. unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
  1205. page_end, GFP_NOFS);
  1206. unlock_page(page);
  1207. btrfs_start_ordered_extent(inode, ordered, 1);
  1208. goto again;
  1209. }
  1210. btrfs_set_extent_delalloc(inode, page_start, page_end);
  1211. ClearPageChecked(page);
  1212. out:
  1213. unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
  1214. out_page:
  1215. unlock_page(page);
  1216. page_cache_release(page);
  1217. }
  1218. /*
  1219. * There are a few paths in the higher layers of the kernel that directly
  1220. * set the page dirty bit without asking the filesystem if it is a
  1221. * good idea. This causes problems because we want to make sure COW
  1222. * properly happens and the data=ordered rules are followed.
  1223. *
  1224. * In our case any range that doesn't have the ORDERED bit set
  1225. * hasn't been properly setup for IO. We kick off an async process
  1226. * to fix it up. The async helper will wait for ordered extents, set
  1227. * the delalloc bit and make it safe to write the page.
  1228. */
  1229. static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
  1230. {
  1231. struct inode *inode = page->mapping->host;
  1232. struct btrfs_writepage_fixup *fixup;
  1233. struct btrfs_root *root = BTRFS_I(inode)->root;
  1234. int ret;
  1235. ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
  1236. EXTENT_ORDERED, 0);
  1237. if (ret)
  1238. return 0;
  1239. if (PageChecked(page))
  1240. return -EAGAIN;
  1241. fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
  1242. if (!fixup)
  1243. return -EAGAIN;
  1244. SetPageChecked(page);
  1245. page_cache_get(page);
  1246. fixup->work.func = btrfs_writepage_fixup_worker;
  1247. fixup->page = page;
  1248. btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
  1249. return -EAGAIN;
  1250. }
  1251. static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
  1252. struct inode *inode, u64 file_pos,
  1253. u64 disk_bytenr, u64 disk_num_bytes,
  1254. u64 num_bytes, u64 ram_bytes,
  1255. u8 compression, u8 encryption,
  1256. u16 other_encoding, int extent_type)
  1257. {
  1258. struct btrfs_root *root = BTRFS_I(inode)->root;
  1259. struct btrfs_file_extent_item *fi;
  1260. struct btrfs_path *path;
  1261. struct extent_buffer *leaf;
  1262. struct btrfs_key ins;
  1263. u64 hint;
  1264. int ret;
  1265. path = btrfs_alloc_path();
  1266. BUG_ON(!path);
  1267. ret = btrfs_drop_extents(trans, root, inode, file_pos,
  1268. file_pos + num_bytes, file_pos, &hint);
  1269. BUG_ON(ret);
  1270. ins.objectid = inode->i_ino;
  1271. ins.offset = file_pos;
  1272. ins.type = BTRFS_EXTENT_DATA_KEY;
  1273. ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
  1274. BUG_ON(ret);
  1275. leaf = path->nodes[0];
  1276. fi = btrfs_item_ptr(leaf, path->slots[0],
  1277. struct btrfs_file_extent_item);
  1278. btrfs_set_file_extent_generation(leaf, fi, trans->transid);
  1279. btrfs_set_file_extent_type(leaf, fi, extent_type);
  1280. btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
  1281. btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
  1282. btrfs_set_file_extent_offset(leaf, fi, 0);
  1283. btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
  1284. btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
  1285. btrfs_set_file_extent_compression(leaf, fi, compression);
  1286. btrfs_set_file_extent_encryption(leaf, fi, encryption);
  1287. btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
  1288. btrfs_mark_buffer_dirty(leaf);
  1289. inode_add_bytes(inode, num_bytes);
  1290. btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
  1291. ins.objectid = disk_bytenr;
  1292. ins.offset = disk_num_bytes;
  1293. ins.type = BTRFS_EXTENT_ITEM_KEY;
  1294. ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
  1295. root->root_key.objectid,
  1296. trans->transid, inode->i_ino, &ins);
  1297. BUG_ON(ret);
  1298. btrfs_free_path(path);
  1299. return 0;
  1300. }
  1301. /* as ordered data IO finishes, this gets called so we can finish
  1302. * an ordered extent if the range of bytes in the file it covers are
  1303. * fully written.
  1304. */
  1305. static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
  1306. {
  1307. struct btrfs_root *root = BTRFS_I(inode)->root;
  1308. struct btrfs_trans_handle *trans;
  1309. struct btrfs_ordered_extent *ordered_extent;
  1310. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1311. int compressed = 0;
  1312. int ret;
  1313. ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
  1314. if (!ret)
  1315. return 0;
  1316. trans = btrfs_join_transaction(root, 1);
  1317. ordered_extent = btrfs_lookup_ordered_extent(inode, start);
  1318. BUG_ON(!ordered_extent);
  1319. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
  1320. goto nocow;
  1321. lock_extent(io_tree, ordered_extent->file_offset,
  1322. ordered_extent->file_offset + ordered_extent->len - 1,
  1323. GFP_NOFS);
  1324. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1325. compressed = 1;
  1326. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1327. BUG_ON(compressed);
  1328. ret = btrfs_mark_extent_written(trans, root, inode,
  1329. ordered_extent->file_offset,
  1330. ordered_extent->file_offset +
  1331. ordered_extent->len);
  1332. BUG_ON(ret);
  1333. } else {
  1334. ret = insert_reserved_file_extent(trans, inode,
  1335. ordered_extent->file_offset,
  1336. ordered_extent->start,
  1337. ordered_extent->disk_len,
  1338. ordered_extent->len,
  1339. ordered_extent->len,
  1340. compressed, 0, 0,
  1341. BTRFS_FILE_EXTENT_REG);
  1342. BUG_ON(ret);
  1343. }
  1344. unlock_extent(io_tree, ordered_extent->file_offset,
  1345. ordered_extent->file_offset + ordered_extent->len - 1,
  1346. GFP_NOFS);
  1347. nocow:
  1348. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1349. &ordered_extent->list);
  1350. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  1351. btrfs_ordered_update_i_size(inode, ordered_extent);
  1352. btrfs_update_inode(trans, root, inode);
  1353. btrfs_remove_ordered_extent(inode, ordered_extent);
  1354. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  1355. /* once for us */
  1356. btrfs_put_ordered_extent(ordered_extent);
  1357. /* once for the tree */
  1358. btrfs_put_ordered_extent(ordered_extent);
  1359. btrfs_end_transaction(trans, root);
  1360. return 0;
  1361. }
  1362. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1363. struct extent_state *state, int uptodate)
  1364. {
  1365. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1366. }
  1367. /*
  1368. * When IO fails, either with EIO or csum verification fails, we
  1369. * try other mirrors that might have a good copy of the data. This
  1370. * io_failure_record is used to record state as we go through all the
  1371. * mirrors. If another mirror has good data, the page is set up to date
  1372. * and things continue. If a good mirror can't be found, the original
  1373. * bio end_io callback is called to indicate things have failed.
  1374. */
  1375. struct io_failure_record {
  1376. struct page *page;
  1377. u64 start;
  1378. u64 len;
  1379. u64 logical;
  1380. unsigned long bio_flags;
  1381. int last_mirror;
  1382. };
  1383. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1384. struct page *page, u64 start, u64 end,
  1385. struct extent_state *state)
  1386. {
  1387. struct io_failure_record *failrec = NULL;
  1388. u64 private;
  1389. struct extent_map *em;
  1390. struct inode *inode = page->mapping->host;
  1391. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1392. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1393. struct bio *bio;
  1394. int num_copies;
  1395. int ret;
  1396. int rw;
  1397. u64 logical;
  1398. ret = get_state_private(failure_tree, start, &private);
  1399. if (ret) {
  1400. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1401. if (!failrec)
  1402. return -ENOMEM;
  1403. failrec->start = start;
  1404. failrec->len = end - start + 1;
  1405. failrec->last_mirror = 0;
  1406. failrec->bio_flags = 0;
  1407. spin_lock(&em_tree->lock);
  1408. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1409. if (em->start > start || em->start + em->len < start) {
  1410. free_extent_map(em);
  1411. em = NULL;
  1412. }
  1413. spin_unlock(&em_tree->lock);
  1414. if (!em || IS_ERR(em)) {
  1415. kfree(failrec);
  1416. return -EIO;
  1417. }
  1418. logical = start - em->start;
  1419. logical = em->block_start + logical;
  1420. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1421. logical = em->block_start;
  1422. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1423. }
  1424. failrec->logical = logical;
  1425. free_extent_map(em);
  1426. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1427. EXTENT_DIRTY, GFP_NOFS);
  1428. set_state_private(failure_tree, start,
  1429. (u64)(unsigned long)failrec);
  1430. } else {
  1431. failrec = (struct io_failure_record *)(unsigned long)private;
  1432. }
  1433. num_copies = btrfs_num_copies(
  1434. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1435. failrec->logical, failrec->len);
  1436. failrec->last_mirror++;
  1437. if (!state) {
  1438. spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
  1439. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1440. failrec->start,
  1441. EXTENT_LOCKED);
  1442. if (state && state->start != failrec->start)
  1443. state = NULL;
  1444. spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
  1445. }
  1446. if (!state || failrec->last_mirror > num_copies) {
  1447. set_state_private(failure_tree, failrec->start, 0);
  1448. clear_extent_bits(failure_tree, failrec->start,
  1449. failrec->start + failrec->len - 1,
  1450. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1451. kfree(failrec);
  1452. return -EIO;
  1453. }
  1454. bio = bio_alloc(GFP_NOFS, 1);
  1455. bio->bi_private = state;
  1456. bio->bi_end_io = failed_bio->bi_end_io;
  1457. bio->bi_sector = failrec->logical >> 9;
  1458. bio->bi_bdev = failed_bio->bi_bdev;
  1459. bio->bi_size = 0;
  1460. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1461. if (failed_bio->bi_rw & (1 << BIO_RW))
  1462. rw = WRITE;
  1463. else
  1464. rw = READ;
  1465. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1466. failrec->last_mirror,
  1467. failrec->bio_flags);
  1468. return 0;
  1469. }
  1470. /*
  1471. * each time an IO finishes, we do a fast check in the IO failure tree
  1472. * to see if we need to process or clean up an io_failure_record
  1473. */
  1474. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1475. {
  1476. u64 private;
  1477. u64 private_failure;
  1478. struct io_failure_record *failure;
  1479. int ret;
  1480. private = 0;
  1481. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1482. (u64)-1, 1, EXTENT_DIRTY)) {
  1483. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1484. start, &private_failure);
  1485. if (ret == 0) {
  1486. failure = (struct io_failure_record *)(unsigned long)
  1487. private_failure;
  1488. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1489. failure->start, 0);
  1490. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1491. failure->start,
  1492. failure->start + failure->len - 1,
  1493. EXTENT_DIRTY | EXTENT_LOCKED,
  1494. GFP_NOFS);
  1495. kfree(failure);
  1496. }
  1497. }
  1498. return 0;
  1499. }
  1500. /*
  1501. * when reads are done, we need to check csums to verify the data is correct
  1502. * if there's a match, we allow the bio to finish. If not, we go through
  1503. * the io_failure_record routines to find good copies
  1504. */
  1505. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1506. struct extent_state *state)
  1507. {
  1508. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1509. struct inode *inode = page->mapping->host;
  1510. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1511. char *kaddr;
  1512. u64 private = ~(u32)0;
  1513. int ret;
  1514. struct btrfs_root *root = BTRFS_I(inode)->root;
  1515. u32 csum = ~(u32)0;
  1516. unsigned long flags;
  1517. if (PageChecked(page)) {
  1518. ClearPageChecked(page);
  1519. goto good;
  1520. }
  1521. if (btrfs_test_opt(root, NODATASUM) ||
  1522. btrfs_test_flag(inode, NODATASUM))
  1523. return 0;
  1524. if (state && state->start == start) {
  1525. private = state->private;
  1526. ret = 0;
  1527. } else {
  1528. ret = get_state_private(io_tree, start, &private);
  1529. }
  1530. local_irq_save(flags);
  1531. kaddr = kmap_atomic(page, KM_IRQ0);
  1532. if (ret) {
  1533. goto zeroit;
  1534. }
  1535. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1536. btrfs_csum_final(csum, (char *)&csum);
  1537. if (csum != private) {
  1538. goto zeroit;
  1539. }
  1540. kunmap_atomic(kaddr, KM_IRQ0);
  1541. local_irq_restore(flags);
  1542. good:
  1543. /* if the io failure tree for this inode is non-empty,
  1544. * check to see if we've recovered from a failed IO
  1545. */
  1546. btrfs_clean_io_failures(inode, start);
  1547. return 0;
  1548. zeroit:
  1549. printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
  1550. page->mapping->host->i_ino, (unsigned long long)start, csum,
  1551. private);
  1552. memset(kaddr + offset, 1, end - start + 1);
  1553. flush_dcache_page(page);
  1554. kunmap_atomic(kaddr, KM_IRQ0);
  1555. local_irq_restore(flags);
  1556. if (private == 0)
  1557. return 0;
  1558. return -EIO;
  1559. }
  1560. /*
  1561. * This creates an orphan entry for the given inode in case something goes
  1562. * wrong in the middle of an unlink/truncate.
  1563. */
  1564. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1565. {
  1566. struct btrfs_root *root = BTRFS_I(inode)->root;
  1567. int ret = 0;
  1568. spin_lock(&root->list_lock);
  1569. /* already on the orphan list, we're good */
  1570. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1571. spin_unlock(&root->list_lock);
  1572. return 0;
  1573. }
  1574. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1575. spin_unlock(&root->list_lock);
  1576. /*
  1577. * insert an orphan item to track this unlinked/truncated file
  1578. */
  1579. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1580. return ret;
  1581. }
  1582. /*
  1583. * We have done the truncate/delete so we can go ahead and remove the orphan
  1584. * item for this particular inode.
  1585. */
  1586. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1587. {
  1588. struct btrfs_root *root = BTRFS_I(inode)->root;
  1589. int ret = 0;
  1590. spin_lock(&root->list_lock);
  1591. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1592. spin_unlock(&root->list_lock);
  1593. return 0;
  1594. }
  1595. list_del_init(&BTRFS_I(inode)->i_orphan);
  1596. if (!trans) {
  1597. spin_unlock(&root->list_lock);
  1598. return 0;
  1599. }
  1600. spin_unlock(&root->list_lock);
  1601. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  1602. return ret;
  1603. }
  1604. /*
  1605. * this cleans up any orphans that may be left on the list from the last use
  1606. * of this root.
  1607. */
  1608. void btrfs_orphan_cleanup(struct btrfs_root *root)
  1609. {
  1610. struct btrfs_path *path;
  1611. struct extent_buffer *leaf;
  1612. struct btrfs_item *item;
  1613. struct btrfs_key key, found_key;
  1614. struct btrfs_trans_handle *trans;
  1615. struct inode *inode;
  1616. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  1617. path = btrfs_alloc_path();
  1618. if (!path)
  1619. return;
  1620. path->reada = -1;
  1621. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1622. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  1623. key.offset = (u64)-1;
  1624. while (1) {
  1625. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1626. if (ret < 0) {
  1627. printk(KERN_ERR "Error searching slot for orphan: %d"
  1628. "\n", ret);
  1629. break;
  1630. }
  1631. /*
  1632. * if ret == 0 means we found what we were searching for, which
  1633. * is weird, but possible, so only screw with path if we didnt
  1634. * find the key and see if we have stuff that matches
  1635. */
  1636. if (ret > 0) {
  1637. if (path->slots[0] == 0)
  1638. break;
  1639. path->slots[0]--;
  1640. }
  1641. /* pull out the item */
  1642. leaf = path->nodes[0];
  1643. item = btrfs_item_nr(leaf, path->slots[0]);
  1644. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1645. /* make sure the item matches what we want */
  1646. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  1647. break;
  1648. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  1649. break;
  1650. /* release the path since we're done with it */
  1651. btrfs_release_path(root, path);
  1652. /*
  1653. * this is where we are basically btrfs_lookup, without the
  1654. * crossing root thing. we store the inode number in the
  1655. * offset of the orphan item.
  1656. */
  1657. inode = btrfs_iget_locked(root->fs_info->sb,
  1658. found_key.offset, root);
  1659. if (!inode)
  1660. break;
  1661. if (inode->i_state & I_NEW) {
  1662. BTRFS_I(inode)->root = root;
  1663. /* have to set the location manually */
  1664. BTRFS_I(inode)->location.objectid = inode->i_ino;
  1665. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  1666. BTRFS_I(inode)->location.offset = 0;
  1667. btrfs_read_locked_inode(inode);
  1668. unlock_new_inode(inode);
  1669. }
  1670. /*
  1671. * add this inode to the orphan list so btrfs_orphan_del does
  1672. * the proper thing when we hit it
  1673. */
  1674. spin_lock(&root->list_lock);
  1675. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1676. spin_unlock(&root->list_lock);
  1677. /*
  1678. * if this is a bad inode, means we actually succeeded in
  1679. * removing the inode, but not the orphan record, which means
  1680. * we need to manually delete the orphan since iput will just
  1681. * do a destroy_inode
  1682. */
  1683. if (is_bad_inode(inode)) {
  1684. trans = btrfs_start_transaction(root, 1);
  1685. btrfs_orphan_del(trans, inode);
  1686. btrfs_end_transaction(trans, root);
  1687. iput(inode);
  1688. continue;
  1689. }
  1690. /* if we have links, this was a truncate, lets do that */
  1691. if (inode->i_nlink) {
  1692. nr_truncate++;
  1693. btrfs_truncate(inode);
  1694. } else {
  1695. nr_unlink++;
  1696. }
  1697. /* this will do delete_inode and everything for us */
  1698. iput(inode);
  1699. }
  1700. if (nr_unlink)
  1701. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  1702. if (nr_truncate)
  1703. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  1704. btrfs_free_path(path);
  1705. }
  1706. /*
  1707. * read an inode from the btree into the in-memory inode
  1708. */
  1709. void btrfs_read_locked_inode(struct inode *inode)
  1710. {
  1711. struct btrfs_path *path;
  1712. struct extent_buffer *leaf;
  1713. struct btrfs_inode_item *inode_item;
  1714. struct btrfs_timespec *tspec;
  1715. struct btrfs_root *root = BTRFS_I(inode)->root;
  1716. struct btrfs_key location;
  1717. u64 alloc_group_block;
  1718. u32 rdev;
  1719. int ret;
  1720. path = btrfs_alloc_path();
  1721. BUG_ON(!path);
  1722. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  1723. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  1724. if (ret)
  1725. goto make_bad;
  1726. leaf = path->nodes[0];
  1727. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1728. struct btrfs_inode_item);
  1729. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  1730. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  1731. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  1732. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  1733. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  1734. tspec = btrfs_inode_atime(inode_item);
  1735. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1736. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1737. tspec = btrfs_inode_mtime(inode_item);
  1738. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1739. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1740. tspec = btrfs_inode_ctime(inode_item);
  1741. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1742. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1743. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  1744. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  1745. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  1746. inode->i_generation = BTRFS_I(inode)->generation;
  1747. inode->i_rdev = 0;
  1748. rdev = btrfs_inode_rdev(leaf, inode_item);
  1749. BTRFS_I(inode)->index_cnt = (u64)-1;
  1750. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  1751. BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
  1752. alloc_group_block);
  1753. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  1754. if (!BTRFS_I(inode)->block_group) {
  1755. BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
  1756. NULL, 0,
  1757. BTRFS_BLOCK_GROUP_METADATA, 0);
  1758. }
  1759. btrfs_free_path(path);
  1760. inode_item = NULL;
  1761. switch (inode->i_mode & S_IFMT) {
  1762. case S_IFREG:
  1763. inode->i_mapping->a_ops = &btrfs_aops;
  1764. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1765. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  1766. inode->i_fop = &btrfs_file_operations;
  1767. inode->i_op = &btrfs_file_inode_operations;
  1768. break;
  1769. case S_IFDIR:
  1770. inode->i_fop = &btrfs_dir_file_operations;
  1771. if (root == root->fs_info->tree_root)
  1772. inode->i_op = &btrfs_dir_ro_inode_operations;
  1773. else
  1774. inode->i_op = &btrfs_dir_inode_operations;
  1775. break;
  1776. case S_IFLNK:
  1777. inode->i_op = &btrfs_symlink_inode_operations;
  1778. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  1779. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1780. break;
  1781. default:
  1782. init_special_inode(inode, inode->i_mode, rdev);
  1783. break;
  1784. }
  1785. return;
  1786. make_bad:
  1787. btrfs_free_path(path);
  1788. make_bad_inode(inode);
  1789. }
  1790. /*
  1791. * given a leaf and an inode, copy the inode fields into the leaf
  1792. */
  1793. static void fill_inode_item(struct btrfs_trans_handle *trans,
  1794. struct extent_buffer *leaf,
  1795. struct btrfs_inode_item *item,
  1796. struct inode *inode)
  1797. {
  1798. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  1799. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  1800. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  1801. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  1802. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  1803. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  1804. inode->i_atime.tv_sec);
  1805. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  1806. inode->i_atime.tv_nsec);
  1807. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  1808. inode->i_mtime.tv_sec);
  1809. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  1810. inode->i_mtime.tv_nsec);
  1811. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  1812. inode->i_ctime.tv_sec);
  1813. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  1814. inode->i_ctime.tv_nsec);
  1815. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  1816. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  1817. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  1818. btrfs_set_inode_transid(leaf, item, trans->transid);
  1819. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  1820. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  1821. btrfs_set_inode_block_group(leaf, item,
  1822. BTRFS_I(inode)->block_group->key.objectid);
  1823. }
  1824. /*
  1825. * copy everything in the in-memory inode into the btree.
  1826. */
  1827. int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
  1828. struct btrfs_root *root,
  1829. struct inode *inode)
  1830. {
  1831. struct btrfs_inode_item *inode_item;
  1832. struct btrfs_path *path;
  1833. struct extent_buffer *leaf;
  1834. int ret;
  1835. path = btrfs_alloc_path();
  1836. BUG_ON(!path);
  1837. ret = btrfs_lookup_inode(trans, root, path,
  1838. &BTRFS_I(inode)->location, 1);
  1839. if (ret) {
  1840. if (ret > 0)
  1841. ret = -ENOENT;
  1842. goto failed;
  1843. }
  1844. leaf = path->nodes[0];
  1845. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1846. struct btrfs_inode_item);
  1847. fill_inode_item(trans, leaf, inode_item, inode);
  1848. btrfs_mark_buffer_dirty(leaf);
  1849. btrfs_set_inode_last_trans(trans, inode);
  1850. ret = 0;
  1851. failed:
  1852. btrfs_free_path(path);
  1853. return ret;
  1854. }
  1855. /*
  1856. * unlink helper that gets used here in inode.c and in the tree logging
  1857. * recovery code. It remove a link in a directory with a given name, and
  1858. * also drops the back refs in the inode to the directory
  1859. */
  1860. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  1861. struct btrfs_root *root,
  1862. struct inode *dir, struct inode *inode,
  1863. const char *name, int name_len)
  1864. {
  1865. struct btrfs_path *path;
  1866. int ret = 0;
  1867. struct extent_buffer *leaf;
  1868. struct btrfs_dir_item *di;
  1869. struct btrfs_key key;
  1870. u64 index;
  1871. path = btrfs_alloc_path();
  1872. if (!path) {
  1873. ret = -ENOMEM;
  1874. goto err;
  1875. }
  1876. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  1877. name, name_len, -1);
  1878. if (IS_ERR(di)) {
  1879. ret = PTR_ERR(di);
  1880. goto err;
  1881. }
  1882. if (!di) {
  1883. ret = -ENOENT;
  1884. goto err;
  1885. }
  1886. leaf = path->nodes[0];
  1887. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  1888. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  1889. if (ret)
  1890. goto err;
  1891. btrfs_release_path(root, path);
  1892. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  1893. inode->i_ino,
  1894. dir->i_ino, &index);
  1895. if (ret) {
  1896. printk("failed to delete reference to %.*s, "
  1897. "inode %lu parent %lu\n", name_len, name,
  1898. inode->i_ino, dir->i_ino);
  1899. goto err;
  1900. }
  1901. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  1902. index, name, name_len, -1);
  1903. if (IS_ERR(di)) {
  1904. ret = PTR_ERR(di);
  1905. goto err;
  1906. }
  1907. if (!di) {
  1908. ret = -ENOENT;
  1909. goto err;
  1910. }
  1911. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  1912. btrfs_release_path(root, path);
  1913. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  1914. inode, dir->i_ino);
  1915. BUG_ON(ret != 0 && ret != -ENOENT);
  1916. if (ret != -ENOENT)
  1917. BTRFS_I(dir)->log_dirty_trans = trans->transid;
  1918. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  1919. dir, index);
  1920. BUG_ON(ret);
  1921. err:
  1922. btrfs_free_path(path);
  1923. if (ret)
  1924. goto out;
  1925. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  1926. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  1927. btrfs_update_inode(trans, root, dir);
  1928. btrfs_drop_nlink(inode);
  1929. ret = btrfs_update_inode(trans, root, inode);
  1930. dir->i_sb->s_dirt = 1;
  1931. out:
  1932. return ret;
  1933. }
  1934. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  1935. {
  1936. struct btrfs_root *root;
  1937. struct btrfs_trans_handle *trans;
  1938. struct inode *inode = dentry->d_inode;
  1939. int ret;
  1940. unsigned long nr = 0;
  1941. root = BTRFS_I(dir)->root;
  1942. ret = btrfs_check_free_space(root, 1, 1);
  1943. if (ret)
  1944. goto fail;
  1945. trans = btrfs_start_transaction(root, 1);
  1946. btrfs_set_trans_block_group(trans, dir);
  1947. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  1948. dentry->d_name.name, dentry->d_name.len);
  1949. if (inode->i_nlink == 0)
  1950. ret = btrfs_orphan_add(trans, inode);
  1951. nr = trans->blocks_used;
  1952. btrfs_end_transaction_throttle(trans, root);
  1953. fail:
  1954. btrfs_btree_balance_dirty(root, nr);
  1955. return ret;
  1956. }
  1957. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  1958. {
  1959. struct inode *inode = dentry->d_inode;
  1960. int err = 0;
  1961. int ret;
  1962. struct btrfs_root *root = BTRFS_I(dir)->root;
  1963. struct btrfs_trans_handle *trans;
  1964. unsigned long nr = 0;
  1965. /*
  1966. * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
  1967. * the root of a subvolume or snapshot
  1968. */
  1969. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  1970. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  1971. return -ENOTEMPTY;
  1972. }
  1973. ret = btrfs_check_free_space(root, 1, 1);
  1974. if (ret)
  1975. goto fail;
  1976. trans = btrfs_start_transaction(root, 1);
  1977. btrfs_set_trans_block_group(trans, dir);
  1978. err = btrfs_orphan_add(trans, inode);
  1979. if (err)
  1980. goto fail_trans;
  1981. /* now the directory is empty */
  1982. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  1983. dentry->d_name.name, dentry->d_name.len);
  1984. if (!err) {
  1985. btrfs_i_size_write(inode, 0);
  1986. }
  1987. fail_trans:
  1988. nr = trans->blocks_used;
  1989. ret = btrfs_end_transaction_throttle(trans, root);
  1990. fail:
  1991. btrfs_btree_balance_dirty(root, nr);
  1992. if (ret && !err)
  1993. err = ret;
  1994. return err;
  1995. }
  1996. #if 0
  1997. /*
  1998. * when truncating bytes in a file, it is possible to avoid reading
  1999. * the leaves that contain only checksum items. This can be the
  2000. * majority of the IO required to delete a large file, but it must
  2001. * be done carefully.
  2002. *
  2003. * The keys in the level just above the leaves are checked to make sure
  2004. * the lowest key in a given leaf is a csum key, and starts at an offset
  2005. * after the new size.
  2006. *
  2007. * Then the key for the next leaf is checked to make sure it also has
  2008. * a checksum item for the same file. If it does, we know our target leaf
  2009. * contains only checksum items, and it can be safely freed without reading
  2010. * it.
  2011. *
  2012. * This is just an optimization targeted at large files. It may do
  2013. * nothing. It will return 0 unless things went badly.
  2014. */
  2015. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2016. struct btrfs_root *root,
  2017. struct btrfs_path *path,
  2018. struct inode *inode, u64 new_size)
  2019. {
  2020. struct btrfs_key key;
  2021. int ret;
  2022. int nritems;
  2023. struct btrfs_key found_key;
  2024. struct btrfs_key other_key;
  2025. struct btrfs_leaf_ref *ref;
  2026. u64 leaf_gen;
  2027. u64 leaf_start;
  2028. path->lowest_level = 1;
  2029. key.objectid = inode->i_ino;
  2030. key.type = BTRFS_CSUM_ITEM_KEY;
  2031. key.offset = new_size;
  2032. again:
  2033. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2034. if (ret < 0)
  2035. goto out;
  2036. if (path->nodes[1] == NULL) {
  2037. ret = 0;
  2038. goto out;
  2039. }
  2040. ret = 0;
  2041. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2042. nritems = btrfs_header_nritems(path->nodes[1]);
  2043. if (!nritems)
  2044. goto out;
  2045. if (path->slots[1] >= nritems)
  2046. goto next_node;
  2047. /* did we find a key greater than anything we want to delete? */
  2048. if (found_key.objectid > inode->i_ino ||
  2049. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2050. goto out;
  2051. /* we check the next key in the node to make sure the leave contains
  2052. * only checksum items. This comparison doesn't work if our
  2053. * leaf is the last one in the node
  2054. */
  2055. if (path->slots[1] + 1 >= nritems) {
  2056. next_node:
  2057. /* search forward from the last key in the node, this
  2058. * will bring us into the next node in the tree
  2059. */
  2060. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2061. /* unlikely, but we inc below, so check to be safe */
  2062. if (found_key.offset == (u64)-1)
  2063. goto out;
  2064. /* search_forward needs a path with locks held, do the
  2065. * search again for the original key. It is possible
  2066. * this will race with a balance and return a path that
  2067. * we could modify, but this drop is just an optimization
  2068. * and is allowed to miss some leaves.
  2069. */
  2070. btrfs_release_path(root, path);
  2071. found_key.offset++;
  2072. /* setup a max key for search_forward */
  2073. other_key.offset = (u64)-1;
  2074. other_key.type = key.type;
  2075. other_key.objectid = key.objectid;
  2076. path->keep_locks = 1;
  2077. ret = btrfs_search_forward(root, &found_key, &other_key,
  2078. path, 0, 0);
  2079. path->keep_locks = 0;
  2080. if (ret || found_key.objectid != key.objectid ||
  2081. found_key.type != key.type) {
  2082. ret = 0;
  2083. goto out;
  2084. }
  2085. key.offset = found_key.offset;
  2086. btrfs_release_path(root, path);
  2087. cond_resched();
  2088. goto again;
  2089. }
  2090. /* we know there's one more slot after us in the tree,
  2091. * read that key so we can verify it is also a checksum item
  2092. */
  2093. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2094. if (found_key.objectid < inode->i_ino)
  2095. goto next_key;
  2096. if (found_key.type != key.type || found_key.offset < new_size)
  2097. goto next_key;
  2098. /*
  2099. * if the key for the next leaf isn't a csum key from this objectid,
  2100. * we can't be sure there aren't good items inside this leaf.
  2101. * Bail out
  2102. */
  2103. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2104. goto out;
  2105. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2106. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2107. /*
  2108. * it is safe to delete this leaf, it contains only
  2109. * csum items from this inode at an offset >= new_size
  2110. */
  2111. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2112. BUG_ON(ret);
  2113. if (root->ref_cows && leaf_gen < trans->transid) {
  2114. ref = btrfs_alloc_leaf_ref(root, 0);
  2115. if (ref) {
  2116. ref->root_gen = root->root_key.offset;
  2117. ref->bytenr = leaf_start;
  2118. ref->owner = 0;
  2119. ref->generation = leaf_gen;
  2120. ref->nritems = 0;
  2121. ret = btrfs_add_leaf_ref(root, ref, 0);
  2122. WARN_ON(ret);
  2123. btrfs_free_leaf_ref(root, ref);
  2124. } else {
  2125. WARN_ON(1);
  2126. }
  2127. }
  2128. next_key:
  2129. btrfs_release_path(root, path);
  2130. if (other_key.objectid == inode->i_ino &&
  2131. other_key.type == key.type && other_key.offset > key.offset) {
  2132. key.offset = other_key.offset;
  2133. cond_resched();
  2134. goto again;
  2135. }
  2136. ret = 0;
  2137. out:
  2138. /* fixup any changes we've made to the path */
  2139. path->lowest_level = 0;
  2140. path->keep_locks = 0;
  2141. btrfs_release_path(root, path);
  2142. return ret;
  2143. }
  2144. #endif
  2145. /*
  2146. * this can truncate away extent items, csum items and directory items.
  2147. * It starts at a high offset and removes keys until it can't find
  2148. * any higher than new_size
  2149. *
  2150. * csum items that cross the new i_size are truncated to the new size
  2151. * as well.
  2152. *
  2153. * min_type is the minimum key type to truncate down to. If set to 0, this
  2154. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2155. */
  2156. noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2157. struct btrfs_root *root,
  2158. struct inode *inode,
  2159. u64 new_size, u32 min_type)
  2160. {
  2161. int ret;
  2162. struct btrfs_path *path;
  2163. struct btrfs_key key;
  2164. struct btrfs_key found_key;
  2165. u32 found_type;
  2166. struct extent_buffer *leaf;
  2167. struct btrfs_file_extent_item *fi;
  2168. u64 extent_start = 0;
  2169. u64 extent_num_bytes = 0;
  2170. u64 item_end = 0;
  2171. u64 root_gen = 0;
  2172. u64 root_owner = 0;
  2173. int found_extent;
  2174. int del_item;
  2175. int pending_del_nr = 0;
  2176. int pending_del_slot = 0;
  2177. int extent_type = -1;
  2178. int encoding;
  2179. u64 mask = root->sectorsize - 1;
  2180. if (root->ref_cows)
  2181. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2182. path = btrfs_alloc_path();
  2183. path->reada = -1;
  2184. BUG_ON(!path);
  2185. /* FIXME, add redo link to tree so we don't leak on crash */
  2186. key.objectid = inode->i_ino;
  2187. key.offset = (u64)-1;
  2188. key.type = (u8)-1;
  2189. btrfs_init_path(path);
  2190. search_again:
  2191. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2192. if (ret < 0) {
  2193. goto error;
  2194. }
  2195. if (ret > 0) {
  2196. /* there are no items in the tree for us to truncate, we're
  2197. * done
  2198. */
  2199. if (path->slots[0] == 0) {
  2200. ret = 0;
  2201. goto error;
  2202. }
  2203. path->slots[0]--;
  2204. }
  2205. while(1) {
  2206. fi = NULL;
  2207. leaf = path->nodes[0];
  2208. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2209. found_type = btrfs_key_type(&found_key);
  2210. encoding = 0;
  2211. if (found_key.objectid != inode->i_ino)
  2212. break;
  2213. if (found_type < min_type)
  2214. break;
  2215. item_end = found_key.offset;
  2216. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2217. fi = btrfs_item_ptr(leaf, path->slots[0],
  2218. struct btrfs_file_extent_item);
  2219. extent_type = btrfs_file_extent_type(leaf, fi);
  2220. encoding = btrfs_file_extent_compression(leaf, fi);
  2221. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2222. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2223. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2224. item_end +=
  2225. btrfs_file_extent_num_bytes(leaf, fi);
  2226. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2227. item_end += btrfs_file_extent_inline_len(leaf,
  2228. fi);
  2229. }
  2230. item_end--;
  2231. }
  2232. if (item_end < new_size) {
  2233. if (found_type == BTRFS_DIR_ITEM_KEY) {
  2234. found_type = BTRFS_INODE_ITEM_KEY;
  2235. } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
  2236. found_type = BTRFS_EXTENT_DATA_KEY;
  2237. } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2238. found_type = BTRFS_XATTR_ITEM_KEY;
  2239. } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
  2240. found_type = BTRFS_INODE_REF_KEY;
  2241. } else if (found_type) {
  2242. found_type--;
  2243. } else {
  2244. break;
  2245. }
  2246. btrfs_set_key_type(&key, found_type);
  2247. goto next;
  2248. }
  2249. if (found_key.offset >= new_size)
  2250. del_item = 1;
  2251. else
  2252. del_item = 0;
  2253. found_extent = 0;
  2254. /* FIXME, shrink the extent if the ref count is only 1 */
  2255. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2256. goto delete;
  2257. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2258. u64 num_dec;
  2259. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2260. if (!del_item && !encoding) {
  2261. u64 orig_num_bytes =
  2262. btrfs_file_extent_num_bytes(leaf, fi);
  2263. extent_num_bytes = new_size -
  2264. found_key.offset + root->sectorsize - 1;
  2265. extent_num_bytes = extent_num_bytes &
  2266. ~((u64)root->sectorsize - 1);
  2267. btrfs_set_file_extent_num_bytes(leaf, fi,
  2268. extent_num_bytes);
  2269. num_dec = (orig_num_bytes -
  2270. extent_num_bytes);
  2271. if (root->ref_cows && extent_start != 0)
  2272. inode_sub_bytes(inode, num_dec);
  2273. btrfs_mark_buffer_dirty(leaf);
  2274. } else {
  2275. extent_num_bytes =
  2276. btrfs_file_extent_disk_num_bytes(leaf,
  2277. fi);
  2278. /* FIXME blocksize != 4096 */
  2279. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2280. if (extent_start != 0) {
  2281. found_extent = 1;
  2282. if (root->ref_cows)
  2283. inode_sub_bytes(inode, num_dec);
  2284. }
  2285. root_gen = btrfs_header_generation(leaf);
  2286. root_owner = btrfs_header_owner(leaf);
  2287. }
  2288. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2289. /*
  2290. * we can't truncate inline items that have had
  2291. * special encodings
  2292. */
  2293. if (!del_item &&
  2294. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2295. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2296. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2297. u32 size = new_size - found_key.offset;
  2298. if (root->ref_cows) {
  2299. inode_sub_bytes(inode, item_end + 1 -
  2300. new_size);
  2301. }
  2302. size =
  2303. btrfs_file_extent_calc_inline_size(size);
  2304. ret = btrfs_truncate_item(trans, root, path,
  2305. size, 1);
  2306. BUG_ON(ret);
  2307. } else if (root->ref_cows) {
  2308. inode_sub_bytes(inode, item_end + 1 -
  2309. found_key.offset);
  2310. }
  2311. }
  2312. delete:
  2313. if (del_item) {
  2314. if (!pending_del_nr) {
  2315. /* no pending yet, add ourselves */
  2316. pending_del_slot = path->slots[0];
  2317. pending_del_nr = 1;
  2318. } else if (pending_del_nr &&
  2319. path->slots[0] + 1 == pending_del_slot) {
  2320. /* hop on the pending chunk */
  2321. pending_del_nr++;
  2322. pending_del_slot = path->slots[0];
  2323. } else {
  2324. printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
  2325. }
  2326. } else {
  2327. break;
  2328. }
  2329. if (found_extent) {
  2330. ret = btrfs_free_extent(trans, root, extent_start,
  2331. extent_num_bytes,
  2332. leaf->start, root_owner,
  2333. root_gen, inode->i_ino, 0);
  2334. BUG_ON(ret);
  2335. }
  2336. next:
  2337. if (path->slots[0] == 0) {
  2338. if (pending_del_nr)
  2339. goto del_pending;
  2340. btrfs_release_path(root, path);
  2341. goto search_again;
  2342. }
  2343. path->slots[0]--;
  2344. if (pending_del_nr &&
  2345. path->slots[0] + 1 != pending_del_slot) {
  2346. struct btrfs_key debug;
  2347. del_pending:
  2348. btrfs_item_key_to_cpu(path->nodes[0], &debug,
  2349. pending_del_slot);
  2350. ret = btrfs_del_items(trans, root, path,
  2351. pending_del_slot,
  2352. pending_del_nr);
  2353. BUG_ON(ret);
  2354. pending_del_nr = 0;
  2355. btrfs_release_path(root, path);
  2356. goto search_again;
  2357. }
  2358. }
  2359. ret = 0;
  2360. error:
  2361. if (pending_del_nr) {
  2362. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  2363. pending_del_nr);
  2364. }
  2365. btrfs_free_path(path);
  2366. inode->i_sb->s_dirt = 1;
  2367. return ret;
  2368. }
  2369. /*
  2370. * taken from block_truncate_page, but does cow as it zeros out
  2371. * any bytes left in the last page in the file.
  2372. */
  2373. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  2374. {
  2375. struct inode *inode = mapping->host;
  2376. struct btrfs_root *root = BTRFS_I(inode)->root;
  2377. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2378. struct btrfs_ordered_extent *ordered;
  2379. char *kaddr;
  2380. u32 blocksize = root->sectorsize;
  2381. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2382. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2383. struct page *page;
  2384. int ret = 0;
  2385. u64 page_start;
  2386. u64 page_end;
  2387. if ((offset & (blocksize - 1)) == 0)
  2388. goto out;
  2389. ret = -ENOMEM;
  2390. again:
  2391. page = grab_cache_page(mapping, index);
  2392. if (!page)
  2393. goto out;
  2394. page_start = page_offset(page);
  2395. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2396. if (!PageUptodate(page)) {
  2397. ret = btrfs_readpage(NULL, page);
  2398. lock_page(page);
  2399. if (page->mapping != mapping) {
  2400. unlock_page(page);
  2401. page_cache_release(page);
  2402. goto again;
  2403. }
  2404. if (!PageUptodate(page)) {
  2405. ret = -EIO;
  2406. goto out_unlock;
  2407. }
  2408. }
  2409. wait_on_page_writeback(page);
  2410. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2411. set_page_extent_mapped(page);
  2412. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2413. if (ordered) {
  2414. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2415. unlock_page(page);
  2416. page_cache_release(page);
  2417. btrfs_start_ordered_extent(inode, ordered, 1);
  2418. btrfs_put_ordered_extent(ordered);
  2419. goto again;
  2420. }
  2421. btrfs_set_extent_delalloc(inode, page_start, page_end);
  2422. ret = 0;
  2423. if (offset != PAGE_CACHE_SIZE) {
  2424. kaddr = kmap(page);
  2425. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  2426. flush_dcache_page(page);
  2427. kunmap(page);
  2428. }
  2429. ClearPageChecked(page);
  2430. set_page_dirty(page);
  2431. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2432. out_unlock:
  2433. unlock_page(page);
  2434. page_cache_release(page);
  2435. out:
  2436. return ret;
  2437. }
  2438. int btrfs_cont_expand(struct inode *inode, loff_t size)
  2439. {
  2440. struct btrfs_trans_handle *trans;
  2441. struct btrfs_root *root = BTRFS_I(inode)->root;
  2442. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2443. struct extent_map *em;
  2444. u64 mask = root->sectorsize - 1;
  2445. u64 hole_start = (inode->i_size + mask) & ~mask;
  2446. u64 block_end = (size + mask) & ~mask;
  2447. u64 last_byte;
  2448. u64 cur_offset;
  2449. u64 hole_size;
  2450. int err;
  2451. if (size <= hole_start)
  2452. return 0;
  2453. err = btrfs_check_free_space(root, 1, 0);
  2454. if (err)
  2455. return err;
  2456. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  2457. while (1) {
  2458. struct btrfs_ordered_extent *ordered;
  2459. btrfs_wait_ordered_range(inode, hole_start,
  2460. block_end - hole_start);
  2461. lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2462. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  2463. if (!ordered)
  2464. break;
  2465. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2466. btrfs_put_ordered_extent(ordered);
  2467. }
  2468. trans = btrfs_start_transaction(root, 1);
  2469. btrfs_set_trans_block_group(trans, inode);
  2470. cur_offset = hole_start;
  2471. while (1) {
  2472. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  2473. block_end - cur_offset, 0);
  2474. BUG_ON(IS_ERR(em) || !em);
  2475. last_byte = min(extent_map_end(em), block_end);
  2476. last_byte = (last_byte + mask) & ~mask;
  2477. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  2478. u64 hint_byte = 0;
  2479. hole_size = last_byte - cur_offset;
  2480. err = btrfs_drop_extents(trans, root, inode,
  2481. cur_offset,
  2482. cur_offset + hole_size,
  2483. cur_offset, &hint_byte);
  2484. if (err)
  2485. break;
  2486. err = btrfs_insert_file_extent(trans, root,
  2487. inode->i_ino, cur_offset, 0,
  2488. 0, hole_size, 0, hole_size,
  2489. 0, 0, 0);
  2490. btrfs_drop_extent_cache(inode, hole_start,
  2491. last_byte - 1, 0);
  2492. }
  2493. free_extent_map(em);
  2494. cur_offset = last_byte;
  2495. if (err || cur_offset >= block_end)
  2496. break;
  2497. }
  2498. btrfs_end_transaction(trans, root);
  2499. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2500. return err;
  2501. }
  2502. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  2503. {
  2504. struct inode *inode = dentry->d_inode;
  2505. int err;
  2506. err = inode_change_ok(inode, attr);
  2507. if (err)
  2508. return err;
  2509. if (S_ISREG(inode->i_mode) &&
  2510. attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
  2511. err = btrfs_cont_expand(inode, attr->ia_size);
  2512. if (err)
  2513. return err;
  2514. }
  2515. err = inode_setattr(inode, attr);
  2516. if (!err && ((attr->ia_valid & ATTR_MODE)))
  2517. err = btrfs_acl_chmod(inode);
  2518. return err;
  2519. }
  2520. void btrfs_delete_inode(struct inode *inode)
  2521. {
  2522. struct btrfs_trans_handle *trans;
  2523. struct btrfs_root *root = BTRFS_I(inode)->root;
  2524. unsigned long nr;
  2525. int ret;
  2526. truncate_inode_pages(&inode->i_data, 0);
  2527. if (is_bad_inode(inode)) {
  2528. btrfs_orphan_del(NULL, inode);
  2529. goto no_delete;
  2530. }
  2531. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  2532. btrfs_i_size_write(inode, 0);
  2533. trans = btrfs_start_transaction(root, 1);
  2534. btrfs_set_trans_block_group(trans, inode);
  2535. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
  2536. if (ret) {
  2537. btrfs_orphan_del(NULL, inode);
  2538. goto no_delete_lock;
  2539. }
  2540. btrfs_orphan_del(trans, inode);
  2541. nr = trans->blocks_used;
  2542. clear_inode(inode);
  2543. btrfs_end_transaction(trans, root);
  2544. btrfs_btree_balance_dirty(root, nr);
  2545. return;
  2546. no_delete_lock:
  2547. nr = trans->blocks_used;
  2548. btrfs_end_transaction(trans, root);
  2549. btrfs_btree_balance_dirty(root, nr);
  2550. no_delete:
  2551. clear_inode(inode);
  2552. }
  2553. /*
  2554. * this returns the key found in the dir entry in the location pointer.
  2555. * If no dir entries were found, location->objectid is 0.
  2556. */
  2557. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  2558. struct btrfs_key *location)
  2559. {
  2560. const char *name = dentry->d_name.name;
  2561. int namelen = dentry->d_name.len;
  2562. struct btrfs_dir_item *di;
  2563. struct btrfs_path *path;
  2564. struct btrfs_root *root = BTRFS_I(dir)->root;
  2565. int ret = 0;
  2566. path = btrfs_alloc_path();
  2567. BUG_ON(!path);
  2568. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  2569. namelen, 0);
  2570. if (IS_ERR(di))
  2571. ret = PTR_ERR(di);
  2572. if (!di || IS_ERR(di)) {
  2573. goto out_err;
  2574. }
  2575. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  2576. out:
  2577. btrfs_free_path(path);
  2578. return ret;
  2579. out_err:
  2580. location->objectid = 0;
  2581. goto out;
  2582. }
  2583. /*
  2584. * when we hit a tree root in a directory, the btrfs part of the inode
  2585. * needs to be changed to reflect the root directory of the tree root. This
  2586. * is kind of like crossing a mount point.
  2587. */
  2588. static int fixup_tree_root_location(struct btrfs_root *root,
  2589. struct btrfs_key *location,
  2590. struct btrfs_root **sub_root,
  2591. struct dentry *dentry)
  2592. {
  2593. struct btrfs_root_item *ri;
  2594. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  2595. return 0;
  2596. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  2597. return 0;
  2598. *sub_root = btrfs_read_fs_root(root->fs_info, location,
  2599. dentry->d_name.name,
  2600. dentry->d_name.len);
  2601. if (IS_ERR(*sub_root))
  2602. return PTR_ERR(*sub_root);
  2603. ri = &(*sub_root)->root_item;
  2604. location->objectid = btrfs_root_dirid(ri);
  2605. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  2606. location->offset = 0;
  2607. return 0;
  2608. }
  2609. static noinline void init_btrfs_i(struct inode *inode)
  2610. {
  2611. struct btrfs_inode *bi = BTRFS_I(inode);
  2612. bi->i_acl = NULL;
  2613. bi->i_default_acl = NULL;
  2614. bi->generation = 0;
  2615. bi->sequence = 0;
  2616. bi->last_trans = 0;
  2617. bi->logged_trans = 0;
  2618. bi->delalloc_bytes = 0;
  2619. bi->disk_i_size = 0;
  2620. bi->flags = 0;
  2621. bi->index_cnt = (u64)-1;
  2622. bi->log_dirty_trans = 0;
  2623. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  2624. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  2625. inode->i_mapping, GFP_NOFS);
  2626. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  2627. inode->i_mapping, GFP_NOFS);
  2628. INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
  2629. btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
  2630. mutex_init(&BTRFS_I(inode)->extent_mutex);
  2631. mutex_init(&BTRFS_I(inode)->log_mutex);
  2632. }
  2633. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  2634. {
  2635. struct btrfs_iget_args *args = p;
  2636. inode->i_ino = args->ino;
  2637. init_btrfs_i(inode);
  2638. BTRFS_I(inode)->root = args->root;
  2639. return 0;
  2640. }
  2641. static int btrfs_find_actor(struct inode *inode, void *opaque)
  2642. {
  2643. struct btrfs_iget_args *args = opaque;
  2644. return (args->ino == inode->i_ino &&
  2645. args->root == BTRFS_I(inode)->root);
  2646. }
  2647. struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
  2648. struct btrfs_root *root, int wait)
  2649. {
  2650. struct inode *inode;
  2651. struct btrfs_iget_args args;
  2652. args.ino = objectid;
  2653. args.root = root;
  2654. if (wait) {
  2655. inode = ilookup5(s, objectid, btrfs_find_actor,
  2656. (void *)&args);
  2657. } else {
  2658. inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
  2659. (void *)&args);
  2660. }
  2661. return inode;
  2662. }
  2663. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  2664. struct btrfs_root *root)
  2665. {
  2666. struct inode *inode;
  2667. struct btrfs_iget_args args;
  2668. args.ino = objectid;
  2669. args.root = root;
  2670. inode = iget5_locked(s, objectid, btrfs_find_actor,
  2671. btrfs_init_locked_inode,
  2672. (void *)&args);
  2673. return inode;
  2674. }
  2675. /* Get an inode object given its location and corresponding root.
  2676. * Returns in *is_new if the inode was read from disk
  2677. */
  2678. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  2679. struct btrfs_root *root, int *is_new)
  2680. {
  2681. struct inode *inode;
  2682. inode = btrfs_iget_locked(s, location->objectid, root);
  2683. if (!inode)
  2684. return ERR_PTR(-EACCES);
  2685. if (inode->i_state & I_NEW) {
  2686. BTRFS_I(inode)->root = root;
  2687. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  2688. btrfs_read_locked_inode(inode);
  2689. unlock_new_inode(inode);
  2690. if (is_new)
  2691. *is_new = 1;
  2692. } else {
  2693. if (is_new)
  2694. *is_new = 0;
  2695. }
  2696. return inode;
  2697. }
  2698. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  2699. {
  2700. struct inode * inode;
  2701. struct btrfs_inode *bi = BTRFS_I(dir);
  2702. struct btrfs_root *root = bi->root;
  2703. struct btrfs_root *sub_root = root;
  2704. struct btrfs_key location;
  2705. int ret, new;
  2706. if (dentry->d_name.len > BTRFS_NAME_LEN)
  2707. return ERR_PTR(-ENAMETOOLONG);
  2708. ret = btrfs_inode_by_name(dir, dentry, &location);
  2709. if (ret < 0)
  2710. return ERR_PTR(ret);
  2711. inode = NULL;
  2712. if (location.objectid) {
  2713. ret = fixup_tree_root_location(root, &location, &sub_root,
  2714. dentry);
  2715. if (ret < 0)
  2716. return ERR_PTR(ret);
  2717. if (ret > 0)
  2718. return ERR_PTR(-ENOENT);
  2719. inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
  2720. if (IS_ERR(inode))
  2721. return ERR_CAST(inode);
  2722. }
  2723. return inode;
  2724. }
  2725. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  2726. struct nameidata *nd)
  2727. {
  2728. struct inode *inode;
  2729. if (dentry->d_name.len > BTRFS_NAME_LEN)
  2730. return ERR_PTR(-ENAMETOOLONG);
  2731. inode = btrfs_lookup_dentry(dir, dentry);
  2732. if (IS_ERR(inode))
  2733. return ERR_CAST(inode);
  2734. return d_splice_alias(inode, dentry);
  2735. }
  2736. static unsigned char btrfs_filetype_table[] = {
  2737. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  2738. };
  2739. static int btrfs_real_readdir(struct file *filp, void *dirent,
  2740. filldir_t filldir)
  2741. {
  2742. struct inode *inode = filp->f_dentry->d_inode;
  2743. struct btrfs_root *root = BTRFS_I(inode)->root;
  2744. struct btrfs_item *item;
  2745. struct btrfs_dir_item *di;
  2746. struct btrfs_key key;
  2747. struct btrfs_key found_key;
  2748. struct btrfs_path *path;
  2749. int ret;
  2750. u32 nritems;
  2751. struct extent_buffer *leaf;
  2752. int slot;
  2753. int advance;
  2754. unsigned char d_type;
  2755. int over = 0;
  2756. u32 di_cur;
  2757. u32 di_total;
  2758. u32 di_len;
  2759. int key_type = BTRFS_DIR_INDEX_KEY;
  2760. char tmp_name[32];
  2761. char *name_ptr;
  2762. int name_len;
  2763. /* FIXME, use a real flag for deciding about the key type */
  2764. if (root->fs_info->tree_root == root)
  2765. key_type = BTRFS_DIR_ITEM_KEY;
  2766. /* special case for "." */
  2767. if (filp->f_pos == 0) {
  2768. over = filldir(dirent, ".", 1,
  2769. 1, inode->i_ino,
  2770. DT_DIR);
  2771. if (over)
  2772. return 0;
  2773. filp->f_pos = 1;
  2774. }
  2775. /* special case for .., just use the back ref */
  2776. if (filp->f_pos == 1) {
  2777. u64 pino = parent_ino(filp->f_path.dentry);
  2778. over = filldir(dirent, "..", 2,
  2779. 2, pino, DT_DIR);
  2780. if (over)
  2781. return 0;
  2782. filp->f_pos = 2;
  2783. }
  2784. path = btrfs_alloc_path();
  2785. path->reada = 2;
  2786. btrfs_set_key_type(&key, key_type);
  2787. key.offset = filp->f_pos;
  2788. key.objectid = inode->i_ino;
  2789. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2790. if (ret < 0)
  2791. goto err;
  2792. advance = 0;
  2793. while (1) {
  2794. leaf = path->nodes[0];
  2795. nritems = btrfs_header_nritems(leaf);
  2796. slot = path->slots[0];
  2797. if (advance || slot >= nritems) {
  2798. if (slot >= nritems - 1) {
  2799. ret = btrfs_next_leaf(root, path);
  2800. if (ret)
  2801. break;
  2802. leaf = path->nodes[0];
  2803. nritems = btrfs_header_nritems(leaf);
  2804. slot = path->slots[0];
  2805. } else {
  2806. slot++;
  2807. path->slots[0]++;
  2808. }
  2809. }
  2810. advance = 1;
  2811. item = btrfs_item_nr(leaf, slot);
  2812. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2813. if (found_key.objectid != key.objectid)
  2814. break;
  2815. if (btrfs_key_type(&found_key) != key_type)
  2816. break;
  2817. if (found_key.offset < filp->f_pos)
  2818. continue;
  2819. filp->f_pos = found_key.offset;
  2820. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  2821. di_cur = 0;
  2822. di_total = btrfs_item_size(leaf, item);
  2823. while (di_cur < di_total) {
  2824. struct btrfs_key location;
  2825. name_len = btrfs_dir_name_len(leaf, di);
  2826. if (name_len <= sizeof(tmp_name)) {
  2827. name_ptr = tmp_name;
  2828. } else {
  2829. name_ptr = kmalloc(name_len, GFP_NOFS);
  2830. if (!name_ptr) {
  2831. ret = -ENOMEM;
  2832. goto err;
  2833. }
  2834. }
  2835. read_extent_buffer(leaf, name_ptr,
  2836. (unsigned long)(di + 1), name_len);
  2837. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  2838. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  2839. /* is this a reference to our own snapshot? If so
  2840. * skip it
  2841. */
  2842. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  2843. location.objectid == root->root_key.objectid) {
  2844. over = 0;
  2845. goto skip;
  2846. }
  2847. over = filldir(dirent, name_ptr, name_len,
  2848. found_key.offset, location.objectid,
  2849. d_type);
  2850. skip:
  2851. if (name_ptr != tmp_name)
  2852. kfree(name_ptr);
  2853. if (over)
  2854. goto nopos;
  2855. di_len = btrfs_dir_name_len(leaf, di) +
  2856. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  2857. di_cur += di_len;
  2858. di = (struct btrfs_dir_item *)((char *)di + di_len);
  2859. }
  2860. }
  2861. /* Reached end of directory/root. Bump pos past the last item. */
  2862. if (key_type == BTRFS_DIR_INDEX_KEY)
  2863. filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
  2864. else
  2865. filp->f_pos++;
  2866. nopos:
  2867. ret = 0;
  2868. err:
  2869. btrfs_free_path(path);
  2870. return ret;
  2871. }
  2872. int btrfs_write_inode(struct inode *inode, int wait)
  2873. {
  2874. struct btrfs_root *root = BTRFS_I(inode)->root;
  2875. struct btrfs_trans_handle *trans;
  2876. int ret = 0;
  2877. if (root->fs_info->btree_inode == inode)
  2878. return 0;
  2879. if (wait) {
  2880. trans = btrfs_join_transaction(root, 1);
  2881. btrfs_set_trans_block_group(trans, inode);
  2882. ret = btrfs_commit_transaction(trans, root);
  2883. }
  2884. return ret;
  2885. }
  2886. /*
  2887. * This is somewhat expensive, updating the tree every time the
  2888. * inode changes. But, it is most likely to find the inode in cache.
  2889. * FIXME, needs more benchmarking...there are no reasons other than performance
  2890. * to keep or drop this code.
  2891. */
  2892. void btrfs_dirty_inode(struct inode *inode)
  2893. {
  2894. struct btrfs_root *root = BTRFS_I(inode)->root;
  2895. struct btrfs_trans_handle *trans;
  2896. trans = btrfs_join_transaction(root, 1);
  2897. btrfs_set_trans_block_group(trans, inode);
  2898. btrfs_update_inode(trans, root, inode);
  2899. btrfs_end_transaction(trans, root);
  2900. }
  2901. /*
  2902. * find the highest existing sequence number in a directory
  2903. * and then set the in-memory index_cnt variable to reflect
  2904. * free sequence numbers
  2905. */
  2906. static int btrfs_set_inode_index_count(struct inode *inode)
  2907. {
  2908. struct btrfs_root *root = BTRFS_I(inode)->root;
  2909. struct btrfs_key key, found_key;
  2910. struct btrfs_path *path;
  2911. struct extent_buffer *leaf;
  2912. int ret;
  2913. key.objectid = inode->i_ino;
  2914. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  2915. key.offset = (u64)-1;
  2916. path = btrfs_alloc_path();
  2917. if (!path)
  2918. return -ENOMEM;
  2919. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2920. if (ret < 0)
  2921. goto out;
  2922. /* FIXME: we should be able to handle this */
  2923. if (ret == 0)
  2924. goto out;
  2925. ret = 0;
  2926. /*
  2927. * MAGIC NUMBER EXPLANATION:
  2928. * since we search a directory based on f_pos we have to start at 2
  2929. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  2930. * else has to start at 2
  2931. */
  2932. if (path->slots[0] == 0) {
  2933. BTRFS_I(inode)->index_cnt = 2;
  2934. goto out;
  2935. }
  2936. path->slots[0]--;
  2937. leaf = path->nodes[0];
  2938. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2939. if (found_key.objectid != inode->i_ino ||
  2940. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  2941. BTRFS_I(inode)->index_cnt = 2;
  2942. goto out;
  2943. }
  2944. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  2945. out:
  2946. btrfs_free_path(path);
  2947. return ret;
  2948. }
  2949. /*
  2950. * helper to find a free sequence number in a given directory. This current
  2951. * code is very simple, later versions will do smarter things in the btree
  2952. */
  2953. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  2954. {
  2955. int ret = 0;
  2956. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  2957. ret = btrfs_set_inode_index_count(dir);
  2958. if (ret) {
  2959. return ret;
  2960. }
  2961. }
  2962. *index = BTRFS_I(dir)->index_cnt;
  2963. BTRFS_I(dir)->index_cnt++;
  2964. return ret;
  2965. }
  2966. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  2967. struct btrfs_root *root,
  2968. struct inode *dir,
  2969. const char *name, int name_len,
  2970. u64 ref_objectid,
  2971. u64 objectid,
  2972. struct btrfs_block_group_cache *group,
  2973. int mode, u64 *index)
  2974. {
  2975. struct inode *inode;
  2976. struct btrfs_inode_item *inode_item;
  2977. struct btrfs_block_group_cache *new_inode_group;
  2978. struct btrfs_key *location;
  2979. struct btrfs_path *path;
  2980. struct btrfs_inode_ref *ref;
  2981. struct btrfs_key key[2];
  2982. u32 sizes[2];
  2983. unsigned long ptr;
  2984. int ret;
  2985. int owner;
  2986. path = btrfs_alloc_path();
  2987. BUG_ON(!path);
  2988. inode = new_inode(root->fs_info->sb);
  2989. if (!inode)
  2990. return ERR_PTR(-ENOMEM);
  2991. if (dir) {
  2992. ret = btrfs_set_inode_index(dir, index);
  2993. if (ret)
  2994. return ERR_PTR(ret);
  2995. }
  2996. /*
  2997. * index_cnt is ignored for everything but a dir,
  2998. * btrfs_get_inode_index_count has an explanation for the magic
  2999. * number
  3000. */
  3001. init_btrfs_i(inode);
  3002. BTRFS_I(inode)->index_cnt = 2;
  3003. BTRFS_I(inode)->root = root;
  3004. BTRFS_I(inode)->generation = trans->transid;
  3005. if (mode & S_IFDIR)
  3006. owner = 0;
  3007. else
  3008. owner = 1;
  3009. new_inode_group = btrfs_find_block_group(root, group, 0,
  3010. BTRFS_BLOCK_GROUP_METADATA, owner);
  3011. if (!new_inode_group) {
  3012. printk("find_block group failed\n");
  3013. new_inode_group = group;
  3014. }
  3015. BTRFS_I(inode)->block_group = new_inode_group;
  3016. key[0].objectid = objectid;
  3017. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3018. key[0].offset = 0;
  3019. key[1].objectid = objectid;
  3020. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3021. key[1].offset = ref_objectid;
  3022. sizes[0] = sizeof(struct btrfs_inode_item);
  3023. sizes[1] = name_len + sizeof(*ref);
  3024. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3025. if (ret != 0)
  3026. goto fail;
  3027. if (objectid > root->highest_inode)
  3028. root->highest_inode = objectid;
  3029. inode->i_uid = current_fsuid();
  3030. inode->i_gid = current_fsgid();
  3031. inode->i_mode = mode;
  3032. inode->i_ino = objectid;
  3033. inode_set_bytes(inode, 0);
  3034. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3035. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3036. struct btrfs_inode_item);
  3037. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3038. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3039. struct btrfs_inode_ref);
  3040. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3041. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3042. ptr = (unsigned long)(ref + 1);
  3043. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3044. btrfs_mark_buffer_dirty(path->nodes[0]);
  3045. btrfs_free_path(path);
  3046. location = &BTRFS_I(inode)->location;
  3047. location->objectid = objectid;
  3048. location->offset = 0;
  3049. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3050. insert_inode_hash(inode);
  3051. return inode;
  3052. fail:
  3053. if (dir)
  3054. BTRFS_I(dir)->index_cnt--;
  3055. btrfs_free_path(path);
  3056. return ERR_PTR(ret);
  3057. }
  3058. static inline u8 btrfs_inode_type(struct inode *inode)
  3059. {
  3060. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  3061. }
  3062. /*
  3063. * utility function to add 'inode' into 'parent_inode' with
  3064. * a give name and a given sequence number.
  3065. * if 'add_backref' is true, also insert a backref from the
  3066. * inode to the parent directory.
  3067. */
  3068. int btrfs_add_link(struct btrfs_trans_handle *trans,
  3069. struct inode *parent_inode, struct inode *inode,
  3070. const char *name, int name_len, int add_backref, u64 index)
  3071. {
  3072. int ret;
  3073. struct btrfs_key key;
  3074. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  3075. key.objectid = inode->i_ino;
  3076. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  3077. key.offset = 0;
  3078. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  3079. parent_inode->i_ino,
  3080. &key, btrfs_inode_type(inode),
  3081. index);
  3082. if (ret == 0) {
  3083. if (add_backref) {
  3084. ret = btrfs_insert_inode_ref(trans, root,
  3085. name, name_len,
  3086. inode->i_ino,
  3087. parent_inode->i_ino,
  3088. index);
  3089. }
  3090. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  3091. name_len * 2);
  3092. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  3093. ret = btrfs_update_inode(trans, root, parent_inode);
  3094. }
  3095. return ret;
  3096. }
  3097. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  3098. struct dentry *dentry, struct inode *inode,
  3099. int backref, u64 index)
  3100. {
  3101. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3102. inode, dentry->d_name.name,
  3103. dentry->d_name.len, backref, index);
  3104. if (!err) {
  3105. d_instantiate(dentry, inode);
  3106. return 0;
  3107. }
  3108. if (err > 0)
  3109. err = -EEXIST;
  3110. return err;
  3111. }
  3112. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  3113. int mode, dev_t rdev)
  3114. {
  3115. struct btrfs_trans_handle *trans;
  3116. struct btrfs_root *root = BTRFS_I(dir)->root;
  3117. struct inode *inode = NULL;
  3118. int err;
  3119. int drop_inode = 0;
  3120. u64 objectid;
  3121. unsigned long nr = 0;
  3122. u64 index = 0;
  3123. if (!new_valid_dev(rdev))
  3124. return -EINVAL;
  3125. err = btrfs_check_free_space(root, 1, 0);
  3126. if (err)
  3127. goto fail;
  3128. trans = btrfs_start_transaction(root, 1);
  3129. btrfs_set_trans_block_group(trans, dir);
  3130. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3131. if (err) {
  3132. err = -ENOSPC;
  3133. goto out_unlock;
  3134. }
  3135. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3136. dentry->d_name.len,
  3137. dentry->d_parent->d_inode->i_ino, objectid,
  3138. BTRFS_I(dir)->block_group, mode, &index);
  3139. err = PTR_ERR(inode);
  3140. if (IS_ERR(inode))
  3141. goto out_unlock;
  3142. err = btrfs_init_acl(inode, dir);
  3143. if (err) {
  3144. drop_inode = 1;
  3145. goto out_unlock;
  3146. }
  3147. btrfs_set_trans_block_group(trans, inode);
  3148. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3149. if (err)
  3150. drop_inode = 1;
  3151. else {
  3152. inode->i_op = &btrfs_special_inode_operations;
  3153. init_special_inode(inode, inode->i_mode, rdev);
  3154. btrfs_update_inode(trans, root, inode);
  3155. }
  3156. dir->i_sb->s_dirt = 1;
  3157. btrfs_update_inode_block_group(trans, inode);
  3158. btrfs_update_inode_block_group(trans, dir);
  3159. out_unlock:
  3160. nr = trans->blocks_used;
  3161. btrfs_end_transaction_throttle(trans, root);
  3162. fail:
  3163. if (drop_inode) {
  3164. inode_dec_link_count(inode);
  3165. iput(inode);
  3166. }
  3167. btrfs_btree_balance_dirty(root, nr);
  3168. return err;
  3169. }
  3170. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  3171. int mode, struct nameidata *nd)
  3172. {
  3173. struct btrfs_trans_handle *trans;
  3174. struct btrfs_root *root = BTRFS_I(dir)->root;
  3175. struct inode *inode = NULL;
  3176. int err;
  3177. int drop_inode = 0;
  3178. unsigned long nr = 0;
  3179. u64 objectid;
  3180. u64 index = 0;
  3181. err = btrfs_check_free_space(root, 1, 0);
  3182. if (err)
  3183. goto fail;
  3184. trans = btrfs_start_transaction(root, 1);
  3185. btrfs_set_trans_block_group(trans, dir);
  3186. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3187. if (err) {
  3188. err = -ENOSPC;
  3189. goto out_unlock;
  3190. }
  3191. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3192. dentry->d_name.len,
  3193. dentry->d_parent->d_inode->i_ino,
  3194. objectid, BTRFS_I(dir)->block_group, mode,
  3195. &index);
  3196. err = PTR_ERR(inode);
  3197. if (IS_ERR(inode))
  3198. goto out_unlock;
  3199. err = btrfs_init_acl(inode, dir);
  3200. if (err) {
  3201. drop_inode = 1;
  3202. goto out_unlock;
  3203. }
  3204. btrfs_set_trans_block_group(trans, inode);
  3205. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3206. if (err)
  3207. drop_inode = 1;
  3208. else {
  3209. inode->i_mapping->a_ops = &btrfs_aops;
  3210. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3211. inode->i_fop = &btrfs_file_operations;
  3212. inode->i_op = &btrfs_file_inode_operations;
  3213. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3214. }
  3215. dir->i_sb->s_dirt = 1;
  3216. btrfs_update_inode_block_group(trans, inode);
  3217. btrfs_update_inode_block_group(trans, dir);
  3218. out_unlock:
  3219. nr = trans->blocks_used;
  3220. btrfs_end_transaction_throttle(trans, root);
  3221. fail:
  3222. if (drop_inode) {
  3223. inode_dec_link_count(inode);
  3224. iput(inode);
  3225. }
  3226. btrfs_btree_balance_dirty(root, nr);
  3227. return err;
  3228. }
  3229. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  3230. struct dentry *dentry)
  3231. {
  3232. struct btrfs_trans_handle *trans;
  3233. struct btrfs_root *root = BTRFS_I(dir)->root;
  3234. struct inode *inode = old_dentry->d_inode;
  3235. u64 index;
  3236. unsigned long nr = 0;
  3237. int err;
  3238. int drop_inode = 0;
  3239. if (inode->i_nlink == 0)
  3240. return -ENOENT;
  3241. btrfs_inc_nlink(inode);
  3242. err = btrfs_check_free_space(root, 1, 0);
  3243. if (err)
  3244. goto fail;
  3245. err = btrfs_set_inode_index(dir, &index);
  3246. if (err)
  3247. goto fail;
  3248. trans = btrfs_start_transaction(root, 1);
  3249. btrfs_set_trans_block_group(trans, dir);
  3250. atomic_inc(&inode->i_count);
  3251. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  3252. if (err)
  3253. drop_inode = 1;
  3254. dir->i_sb->s_dirt = 1;
  3255. btrfs_update_inode_block_group(trans, dir);
  3256. err = btrfs_update_inode(trans, root, inode);
  3257. if (err)
  3258. drop_inode = 1;
  3259. nr = trans->blocks_used;
  3260. btrfs_end_transaction_throttle(trans, root);
  3261. fail:
  3262. if (drop_inode) {
  3263. inode_dec_link_count(inode);
  3264. iput(inode);
  3265. }
  3266. btrfs_btree_balance_dirty(root, nr);
  3267. return err;
  3268. }
  3269. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  3270. {
  3271. struct inode *inode = NULL;
  3272. struct btrfs_trans_handle *trans;
  3273. struct btrfs_root *root = BTRFS_I(dir)->root;
  3274. int err = 0;
  3275. int drop_on_err = 0;
  3276. u64 objectid = 0;
  3277. u64 index = 0;
  3278. unsigned long nr = 1;
  3279. err = btrfs_check_free_space(root, 1, 0);
  3280. if (err)
  3281. goto out_unlock;
  3282. trans = btrfs_start_transaction(root, 1);
  3283. btrfs_set_trans_block_group(trans, dir);
  3284. if (IS_ERR(trans)) {
  3285. err = PTR_ERR(trans);
  3286. goto out_unlock;
  3287. }
  3288. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3289. if (err) {
  3290. err = -ENOSPC;
  3291. goto out_unlock;
  3292. }
  3293. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3294. dentry->d_name.len,
  3295. dentry->d_parent->d_inode->i_ino, objectid,
  3296. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  3297. &index);
  3298. if (IS_ERR(inode)) {
  3299. err = PTR_ERR(inode);
  3300. goto out_fail;
  3301. }
  3302. drop_on_err = 1;
  3303. err = btrfs_init_acl(inode, dir);
  3304. if (err)
  3305. goto out_fail;
  3306. inode->i_op = &btrfs_dir_inode_operations;
  3307. inode->i_fop = &btrfs_dir_file_operations;
  3308. btrfs_set_trans_block_group(trans, inode);
  3309. btrfs_i_size_write(inode, 0);
  3310. err = btrfs_update_inode(trans, root, inode);
  3311. if (err)
  3312. goto out_fail;
  3313. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3314. inode, dentry->d_name.name,
  3315. dentry->d_name.len, 0, index);
  3316. if (err)
  3317. goto out_fail;
  3318. d_instantiate(dentry, inode);
  3319. drop_on_err = 0;
  3320. dir->i_sb->s_dirt = 1;
  3321. btrfs_update_inode_block_group(trans, inode);
  3322. btrfs_update_inode_block_group(trans, dir);
  3323. out_fail:
  3324. nr = trans->blocks_used;
  3325. btrfs_end_transaction_throttle(trans, root);
  3326. out_unlock:
  3327. if (drop_on_err)
  3328. iput(inode);
  3329. btrfs_btree_balance_dirty(root, nr);
  3330. return err;
  3331. }
  3332. /* helper for btfs_get_extent. Given an existing extent in the tree,
  3333. * and an extent that you want to insert, deal with overlap and insert
  3334. * the new extent into the tree.
  3335. */
  3336. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  3337. struct extent_map *existing,
  3338. struct extent_map *em,
  3339. u64 map_start, u64 map_len)
  3340. {
  3341. u64 start_diff;
  3342. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  3343. start_diff = map_start - em->start;
  3344. em->start = map_start;
  3345. em->len = map_len;
  3346. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  3347. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  3348. em->block_start += start_diff;
  3349. em->block_len -= start_diff;
  3350. }
  3351. return add_extent_mapping(em_tree, em);
  3352. }
  3353. static noinline int uncompress_inline(struct btrfs_path *path,
  3354. struct inode *inode, struct page *page,
  3355. size_t pg_offset, u64 extent_offset,
  3356. struct btrfs_file_extent_item *item)
  3357. {
  3358. int ret;
  3359. struct extent_buffer *leaf = path->nodes[0];
  3360. char *tmp;
  3361. size_t max_size;
  3362. unsigned long inline_size;
  3363. unsigned long ptr;
  3364. WARN_ON(pg_offset != 0);
  3365. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  3366. inline_size = btrfs_file_extent_inline_item_len(leaf,
  3367. btrfs_item_nr(leaf, path->slots[0]));
  3368. tmp = kmalloc(inline_size, GFP_NOFS);
  3369. ptr = btrfs_file_extent_inline_start(item);
  3370. read_extent_buffer(leaf, tmp, ptr, inline_size);
  3371. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  3372. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  3373. inline_size, max_size);
  3374. if (ret) {
  3375. char *kaddr = kmap_atomic(page, KM_USER0);
  3376. unsigned long copy_size = min_t(u64,
  3377. PAGE_CACHE_SIZE - pg_offset,
  3378. max_size - extent_offset);
  3379. memset(kaddr + pg_offset, 0, copy_size);
  3380. kunmap_atomic(kaddr, KM_USER0);
  3381. }
  3382. kfree(tmp);
  3383. return 0;
  3384. }
  3385. /*
  3386. * a bit scary, this does extent mapping from logical file offset to the disk.
  3387. * the ugly parts come from merging extents from the disk with the
  3388. * in-ram representation. This gets more complex because of the data=ordered code,
  3389. * where the in-ram extents might be locked pending data=ordered completion.
  3390. *
  3391. * This also copies inline extents directly into the page.
  3392. */
  3393. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  3394. size_t pg_offset, u64 start, u64 len,
  3395. int create)
  3396. {
  3397. int ret;
  3398. int err = 0;
  3399. u64 bytenr;
  3400. u64 extent_start = 0;
  3401. u64 extent_end = 0;
  3402. u64 objectid = inode->i_ino;
  3403. u32 found_type;
  3404. struct btrfs_path *path = NULL;
  3405. struct btrfs_root *root = BTRFS_I(inode)->root;
  3406. struct btrfs_file_extent_item *item;
  3407. struct extent_buffer *leaf;
  3408. struct btrfs_key found_key;
  3409. struct extent_map *em = NULL;
  3410. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3411. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3412. struct btrfs_trans_handle *trans = NULL;
  3413. int compressed;
  3414. again:
  3415. spin_lock(&em_tree->lock);
  3416. em = lookup_extent_mapping(em_tree, start, len);
  3417. if (em)
  3418. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3419. spin_unlock(&em_tree->lock);
  3420. if (em) {
  3421. if (em->start > start || em->start + em->len <= start)
  3422. free_extent_map(em);
  3423. else if (em->block_start == EXTENT_MAP_INLINE && page)
  3424. free_extent_map(em);
  3425. else
  3426. goto out;
  3427. }
  3428. em = alloc_extent_map(GFP_NOFS);
  3429. if (!em) {
  3430. err = -ENOMEM;
  3431. goto out;
  3432. }
  3433. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3434. em->start = EXTENT_MAP_HOLE;
  3435. em->orig_start = EXTENT_MAP_HOLE;
  3436. em->len = (u64)-1;
  3437. em->block_len = (u64)-1;
  3438. if (!path) {
  3439. path = btrfs_alloc_path();
  3440. BUG_ON(!path);
  3441. }
  3442. ret = btrfs_lookup_file_extent(trans, root, path,
  3443. objectid, start, trans != NULL);
  3444. if (ret < 0) {
  3445. err = ret;
  3446. goto out;
  3447. }
  3448. if (ret != 0) {
  3449. if (path->slots[0] == 0)
  3450. goto not_found;
  3451. path->slots[0]--;
  3452. }
  3453. leaf = path->nodes[0];
  3454. item = btrfs_item_ptr(leaf, path->slots[0],
  3455. struct btrfs_file_extent_item);
  3456. /* are we inside the extent that was found? */
  3457. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3458. found_type = btrfs_key_type(&found_key);
  3459. if (found_key.objectid != objectid ||
  3460. found_type != BTRFS_EXTENT_DATA_KEY) {
  3461. goto not_found;
  3462. }
  3463. found_type = btrfs_file_extent_type(leaf, item);
  3464. extent_start = found_key.offset;
  3465. compressed = btrfs_file_extent_compression(leaf, item);
  3466. if (found_type == BTRFS_FILE_EXTENT_REG ||
  3467. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  3468. extent_end = extent_start +
  3469. btrfs_file_extent_num_bytes(leaf, item);
  3470. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  3471. size_t size;
  3472. size = btrfs_file_extent_inline_len(leaf, item);
  3473. extent_end = (extent_start + size + root->sectorsize - 1) &
  3474. ~((u64)root->sectorsize - 1);
  3475. }
  3476. if (start >= extent_end) {
  3477. path->slots[0]++;
  3478. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  3479. ret = btrfs_next_leaf(root, path);
  3480. if (ret < 0) {
  3481. err = ret;
  3482. goto out;
  3483. }
  3484. if (ret > 0)
  3485. goto not_found;
  3486. leaf = path->nodes[0];
  3487. }
  3488. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3489. if (found_key.objectid != objectid ||
  3490. found_key.type != BTRFS_EXTENT_DATA_KEY)
  3491. goto not_found;
  3492. if (start + len <= found_key.offset)
  3493. goto not_found;
  3494. em->start = start;
  3495. em->len = found_key.offset - start;
  3496. goto not_found_em;
  3497. }
  3498. if (found_type == BTRFS_FILE_EXTENT_REG ||
  3499. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  3500. em->start = extent_start;
  3501. em->len = extent_end - extent_start;
  3502. em->orig_start = extent_start -
  3503. btrfs_file_extent_offset(leaf, item);
  3504. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  3505. if (bytenr == 0) {
  3506. em->block_start = EXTENT_MAP_HOLE;
  3507. goto insert;
  3508. }
  3509. if (compressed) {
  3510. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3511. em->block_start = bytenr;
  3512. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  3513. item);
  3514. } else {
  3515. bytenr += btrfs_file_extent_offset(leaf, item);
  3516. em->block_start = bytenr;
  3517. em->block_len = em->len;
  3518. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  3519. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  3520. }
  3521. goto insert;
  3522. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  3523. unsigned long ptr;
  3524. char *map;
  3525. size_t size;
  3526. size_t extent_offset;
  3527. size_t copy_size;
  3528. em->block_start = EXTENT_MAP_INLINE;
  3529. if (!page || create) {
  3530. em->start = extent_start;
  3531. em->len = extent_end - extent_start;
  3532. goto out;
  3533. }
  3534. size = btrfs_file_extent_inline_len(leaf, item);
  3535. extent_offset = page_offset(page) + pg_offset - extent_start;
  3536. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  3537. size - extent_offset);
  3538. em->start = extent_start + extent_offset;
  3539. em->len = (copy_size + root->sectorsize - 1) &
  3540. ~((u64)root->sectorsize - 1);
  3541. em->orig_start = EXTENT_MAP_INLINE;
  3542. if (compressed)
  3543. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3544. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  3545. if (create == 0 && !PageUptodate(page)) {
  3546. if (btrfs_file_extent_compression(leaf, item) ==
  3547. BTRFS_COMPRESS_ZLIB) {
  3548. ret = uncompress_inline(path, inode, page,
  3549. pg_offset,
  3550. extent_offset, item);
  3551. BUG_ON(ret);
  3552. } else {
  3553. map = kmap(page);
  3554. read_extent_buffer(leaf, map + pg_offset, ptr,
  3555. copy_size);
  3556. kunmap(page);
  3557. }
  3558. flush_dcache_page(page);
  3559. } else if (create && PageUptodate(page)) {
  3560. if (!trans) {
  3561. kunmap(page);
  3562. free_extent_map(em);
  3563. em = NULL;
  3564. btrfs_release_path(root, path);
  3565. trans = btrfs_join_transaction(root, 1);
  3566. goto again;
  3567. }
  3568. map = kmap(page);
  3569. write_extent_buffer(leaf, map + pg_offset, ptr,
  3570. copy_size);
  3571. kunmap(page);
  3572. btrfs_mark_buffer_dirty(leaf);
  3573. }
  3574. set_extent_uptodate(io_tree, em->start,
  3575. extent_map_end(em) - 1, GFP_NOFS);
  3576. goto insert;
  3577. } else {
  3578. printk("unkknown found_type %d\n", found_type);
  3579. WARN_ON(1);
  3580. }
  3581. not_found:
  3582. em->start = start;
  3583. em->len = len;
  3584. not_found_em:
  3585. em->block_start = EXTENT_MAP_HOLE;
  3586. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  3587. insert:
  3588. btrfs_release_path(root, path);
  3589. if (em->start > start || extent_map_end(em) <= start) {
  3590. printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
  3591. err = -EIO;
  3592. goto out;
  3593. }
  3594. err = 0;
  3595. spin_lock(&em_tree->lock);
  3596. ret = add_extent_mapping(em_tree, em);
  3597. /* it is possible that someone inserted the extent into the tree
  3598. * while we had the lock dropped. It is also possible that
  3599. * an overlapping map exists in the tree
  3600. */
  3601. if (ret == -EEXIST) {
  3602. struct extent_map *existing;
  3603. ret = 0;
  3604. existing = lookup_extent_mapping(em_tree, start, len);
  3605. if (existing && (existing->start > start ||
  3606. existing->start + existing->len <= start)) {
  3607. free_extent_map(existing);
  3608. existing = NULL;
  3609. }
  3610. if (!existing) {
  3611. existing = lookup_extent_mapping(em_tree, em->start,
  3612. em->len);
  3613. if (existing) {
  3614. err = merge_extent_mapping(em_tree, existing,
  3615. em, start,
  3616. root->sectorsize);
  3617. free_extent_map(existing);
  3618. if (err) {
  3619. free_extent_map(em);
  3620. em = NULL;
  3621. }
  3622. } else {
  3623. err = -EIO;
  3624. printk("failing to insert %Lu %Lu\n",
  3625. start, len);
  3626. free_extent_map(em);
  3627. em = NULL;
  3628. }
  3629. } else {
  3630. free_extent_map(em);
  3631. em = existing;
  3632. err = 0;
  3633. }
  3634. }
  3635. spin_unlock(&em_tree->lock);
  3636. out:
  3637. if (path)
  3638. btrfs_free_path(path);
  3639. if (trans) {
  3640. ret = btrfs_end_transaction(trans, root);
  3641. if (!err) {
  3642. err = ret;
  3643. }
  3644. }
  3645. if (err) {
  3646. free_extent_map(em);
  3647. WARN_ON(1);
  3648. return ERR_PTR(err);
  3649. }
  3650. return em;
  3651. }
  3652. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  3653. const struct iovec *iov, loff_t offset,
  3654. unsigned long nr_segs)
  3655. {
  3656. return -EINVAL;
  3657. }
  3658. static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
  3659. {
  3660. return extent_bmap(mapping, iblock, btrfs_get_extent);
  3661. }
  3662. int btrfs_readpage(struct file *file, struct page *page)
  3663. {
  3664. struct extent_io_tree *tree;
  3665. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3666. return extent_read_full_page(tree, page, btrfs_get_extent);
  3667. }
  3668. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  3669. {
  3670. struct extent_io_tree *tree;
  3671. if (current->flags & PF_MEMALLOC) {
  3672. redirty_page_for_writepage(wbc, page);
  3673. unlock_page(page);
  3674. return 0;
  3675. }
  3676. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3677. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  3678. }
  3679. int btrfs_writepages(struct address_space *mapping,
  3680. struct writeback_control *wbc)
  3681. {
  3682. struct extent_io_tree *tree;
  3683. tree = &BTRFS_I(mapping->host)->io_tree;
  3684. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  3685. }
  3686. static int
  3687. btrfs_readpages(struct file *file, struct address_space *mapping,
  3688. struct list_head *pages, unsigned nr_pages)
  3689. {
  3690. struct extent_io_tree *tree;
  3691. tree = &BTRFS_I(mapping->host)->io_tree;
  3692. return extent_readpages(tree, mapping, pages, nr_pages,
  3693. btrfs_get_extent);
  3694. }
  3695. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  3696. {
  3697. struct extent_io_tree *tree;
  3698. struct extent_map_tree *map;
  3699. int ret;
  3700. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3701. map = &BTRFS_I(page->mapping->host)->extent_tree;
  3702. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  3703. if (ret == 1) {
  3704. ClearPagePrivate(page);
  3705. set_page_private(page, 0);
  3706. page_cache_release(page);
  3707. }
  3708. return ret;
  3709. }
  3710. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  3711. {
  3712. if (PageWriteback(page) || PageDirty(page))
  3713. return 0;
  3714. return __btrfs_releasepage(page, gfp_flags);
  3715. }
  3716. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  3717. {
  3718. struct extent_io_tree *tree;
  3719. struct btrfs_ordered_extent *ordered;
  3720. u64 page_start = page_offset(page);
  3721. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  3722. wait_on_page_writeback(page);
  3723. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3724. if (offset) {
  3725. btrfs_releasepage(page, GFP_NOFS);
  3726. return;
  3727. }
  3728. lock_extent(tree, page_start, page_end, GFP_NOFS);
  3729. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  3730. page_offset(page));
  3731. if (ordered) {
  3732. /*
  3733. * IO on this page will never be started, so we need
  3734. * to account for any ordered extents now
  3735. */
  3736. clear_extent_bit(tree, page_start, page_end,
  3737. EXTENT_DIRTY | EXTENT_DELALLOC |
  3738. EXTENT_LOCKED, 1, 0, GFP_NOFS);
  3739. btrfs_finish_ordered_io(page->mapping->host,
  3740. page_start, page_end);
  3741. btrfs_put_ordered_extent(ordered);
  3742. lock_extent(tree, page_start, page_end, GFP_NOFS);
  3743. }
  3744. clear_extent_bit(tree, page_start, page_end,
  3745. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3746. EXTENT_ORDERED,
  3747. 1, 1, GFP_NOFS);
  3748. __btrfs_releasepage(page, GFP_NOFS);
  3749. ClearPageChecked(page);
  3750. if (PagePrivate(page)) {
  3751. ClearPagePrivate(page);
  3752. set_page_private(page, 0);
  3753. page_cache_release(page);
  3754. }
  3755. }
  3756. /*
  3757. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  3758. * called from a page fault handler when a page is first dirtied. Hence we must
  3759. * be careful to check for EOF conditions here. We set the page up correctly
  3760. * for a written page which means we get ENOSPC checking when writing into
  3761. * holes and correct delalloc and unwritten extent mapping on filesystems that
  3762. * support these features.
  3763. *
  3764. * We are not allowed to take the i_mutex here so we have to play games to
  3765. * protect against truncate races as the page could now be beyond EOF. Because
  3766. * vmtruncate() writes the inode size before removing pages, once we have the
  3767. * page lock we can determine safely if the page is beyond EOF. If it is not
  3768. * beyond EOF, then the page is guaranteed safe against truncation until we
  3769. * unlock the page.
  3770. */
  3771. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  3772. {
  3773. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  3774. struct btrfs_root *root = BTRFS_I(inode)->root;
  3775. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3776. struct btrfs_ordered_extent *ordered;
  3777. char *kaddr;
  3778. unsigned long zero_start;
  3779. loff_t size;
  3780. int ret;
  3781. u64 page_start;
  3782. u64 page_end;
  3783. ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
  3784. if (ret)
  3785. goto out;
  3786. ret = -EINVAL;
  3787. again:
  3788. lock_page(page);
  3789. size = i_size_read(inode);
  3790. page_start = page_offset(page);
  3791. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3792. if ((page->mapping != inode->i_mapping) ||
  3793. (page_start >= size)) {
  3794. /* page got truncated out from underneath us */
  3795. goto out_unlock;
  3796. }
  3797. wait_on_page_writeback(page);
  3798. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3799. set_page_extent_mapped(page);
  3800. /*
  3801. * we can't set the delalloc bits if there are pending ordered
  3802. * extents. Drop our locks and wait for them to finish
  3803. */
  3804. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3805. if (ordered) {
  3806. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3807. unlock_page(page);
  3808. btrfs_start_ordered_extent(inode, ordered, 1);
  3809. btrfs_put_ordered_extent(ordered);
  3810. goto again;
  3811. }
  3812. btrfs_set_extent_delalloc(inode, page_start, page_end);
  3813. ret = 0;
  3814. /* page is wholly or partially inside EOF */
  3815. if (page_start + PAGE_CACHE_SIZE > size)
  3816. zero_start = size & ~PAGE_CACHE_MASK;
  3817. else
  3818. zero_start = PAGE_CACHE_SIZE;
  3819. if (zero_start != PAGE_CACHE_SIZE) {
  3820. kaddr = kmap(page);
  3821. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  3822. flush_dcache_page(page);
  3823. kunmap(page);
  3824. }
  3825. ClearPageChecked(page);
  3826. set_page_dirty(page);
  3827. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3828. out_unlock:
  3829. unlock_page(page);
  3830. out:
  3831. return ret;
  3832. }
  3833. static void btrfs_truncate(struct inode *inode)
  3834. {
  3835. struct btrfs_root *root = BTRFS_I(inode)->root;
  3836. int ret;
  3837. struct btrfs_trans_handle *trans;
  3838. unsigned long nr;
  3839. u64 mask = root->sectorsize - 1;
  3840. if (!S_ISREG(inode->i_mode))
  3841. return;
  3842. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3843. return;
  3844. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  3845. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  3846. trans = btrfs_start_transaction(root, 1);
  3847. btrfs_set_trans_block_group(trans, inode);
  3848. btrfs_i_size_write(inode, inode->i_size);
  3849. ret = btrfs_orphan_add(trans, inode);
  3850. if (ret)
  3851. goto out;
  3852. /* FIXME, add redo link to tree so we don't leak on crash */
  3853. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
  3854. BTRFS_EXTENT_DATA_KEY);
  3855. btrfs_update_inode(trans, root, inode);
  3856. ret = btrfs_orphan_del(trans, inode);
  3857. BUG_ON(ret);
  3858. out:
  3859. nr = trans->blocks_used;
  3860. ret = btrfs_end_transaction_throttle(trans, root);
  3861. BUG_ON(ret);
  3862. btrfs_btree_balance_dirty(root, nr);
  3863. }
  3864. /*
  3865. * create a new subvolume directory/inode (helper for the ioctl).
  3866. */
  3867. int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry,
  3868. struct btrfs_trans_handle *trans, u64 new_dirid,
  3869. struct btrfs_block_group_cache *block_group)
  3870. {
  3871. struct inode *inode;
  3872. int error;
  3873. u64 index = 0;
  3874. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  3875. new_dirid, block_group, S_IFDIR | 0700, &index);
  3876. if (IS_ERR(inode))
  3877. return PTR_ERR(inode);
  3878. inode->i_op = &btrfs_dir_inode_operations;
  3879. inode->i_fop = &btrfs_dir_file_operations;
  3880. inode->i_nlink = 1;
  3881. btrfs_i_size_write(inode, 0);
  3882. error = btrfs_update_inode(trans, new_root, inode);
  3883. if (error)
  3884. return error;
  3885. d_instantiate(dentry, inode);
  3886. return 0;
  3887. }
  3888. /* helper function for file defrag and space balancing. This
  3889. * forces readahead on a given range of bytes in an inode
  3890. */
  3891. unsigned long btrfs_force_ra(struct address_space *mapping,
  3892. struct file_ra_state *ra, struct file *file,
  3893. pgoff_t offset, pgoff_t last_index)
  3894. {
  3895. pgoff_t req_size = last_index - offset + 1;
  3896. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  3897. return offset + req_size;
  3898. }
  3899. struct inode *btrfs_alloc_inode(struct super_block *sb)
  3900. {
  3901. struct btrfs_inode *ei;
  3902. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  3903. if (!ei)
  3904. return NULL;
  3905. ei->last_trans = 0;
  3906. ei->logged_trans = 0;
  3907. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  3908. ei->i_acl = BTRFS_ACL_NOT_CACHED;
  3909. ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
  3910. INIT_LIST_HEAD(&ei->i_orphan);
  3911. return &ei->vfs_inode;
  3912. }
  3913. void btrfs_destroy_inode(struct inode *inode)
  3914. {
  3915. struct btrfs_ordered_extent *ordered;
  3916. WARN_ON(!list_empty(&inode->i_dentry));
  3917. WARN_ON(inode->i_data.nrpages);
  3918. if (BTRFS_I(inode)->i_acl &&
  3919. BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
  3920. posix_acl_release(BTRFS_I(inode)->i_acl);
  3921. if (BTRFS_I(inode)->i_default_acl &&
  3922. BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
  3923. posix_acl_release(BTRFS_I(inode)->i_default_acl);
  3924. spin_lock(&BTRFS_I(inode)->root->list_lock);
  3925. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  3926. printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
  3927. " list\n", inode->i_ino);
  3928. dump_stack();
  3929. }
  3930. spin_unlock(&BTRFS_I(inode)->root->list_lock);
  3931. while(1) {
  3932. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  3933. if (!ordered)
  3934. break;
  3935. else {
  3936. printk("found ordered extent %Lu %Lu\n",
  3937. ordered->file_offset, ordered->len);
  3938. btrfs_remove_ordered_extent(inode, ordered);
  3939. btrfs_put_ordered_extent(ordered);
  3940. btrfs_put_ordered_extent(ordered);
  3941. }
  3942. }
  3943. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  3944. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  3945. }
  3946. static void init_once(void *foo)
  3947. {
  3948. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  3949. inode_init_once(&ei->vfs_inode);
  3950. }
  3951. void btrfs_destroy_cachep(void)
  3952. {
  3953. if (btrfs_inode_cachep)
  3954. kmem_cache_destroy(btrfs_inode_cachep);
  3955. if (btrfs_trans_handle_cachep)
  3956. kmem_cache_destroy(btrfs_trans_handle_cachep);
  3957. if (btrfs_transaction_cachep)
  3958. kmem_cache_destroy(btrfs_transaction_cachep);
  3959. if (btrfs_bit_radix_cachep)
  3960. kmem_cache_destroy(btrfs_bit_radix_cachep);
  3961. if (btrfs_path_cachep)
  3962. kmem_cache_destroy(btrfs_path_cachep);
  3963. }
  3964. struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
  3965. unsigned long extra_flags,
  3966. void (*ctor)(void *))
  3967. {
  3968. return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
  3969. SLAB_MEM_SPREAD | extra_flags), ctor);
  3970. }
  3971. int btrfs_init_cachep(void)
  3972. {
  3973. btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
  3974. sizeof(struct btrfs_inode),
  3975. 0, init_once);
  3976. if (!btrfs_inode_cachep)
  3977. goto fail;
  3978. btrfs_trans_handle_cachep =
  3979. btrfs_cache_create("btrfs_trans_handle_cache",
  3980. sizeof(struct btrfs_trans_handle),
  3981. 0, NULL);
  3982. if (!btrfs_trans_handle_cachep)
  3983. goto fail;
  3984. btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
  3985. sizeof(struct btrfs_transaction),
  3986. 0, NULL);
  3987. if (!btrfs_transaction_cachep)
  3988. goto fail;
  3989. btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
  3990. sizeof(struct btrfs_path),
  3991. 0, NULL);
  3992. if (!btrfs_path_cachep)
  3993. goto fail;
  3994. btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
  3995. SLAB_DESTROY_BY_RCU, NULL);
  3996. if (!btrfs_bit_radix_cachep)
  3997. goto fail;
  3998. return 0;
  3999. fail:
  4000. btrfs_destroy_cachep();
  4001. return -ENOMEM;
  4002. }
  4003. static int btrfs_getattr(struct vfsmount *mnt,
  4004. struct dentry *dentry, struct kstat *stat)
  4005. {
  4006. struct inode *inode = dentry->d_inode;
  4007. generic_fillattr(inode, stat);
  4008. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  4009. stat->blksize = PAGE_CACHE_SIZE;
  4010. stat->blocks = (inode_get_bytes(inode) +
  4011. BTRFS_I(inode)->delalloc_bytes) >> 9;
  4012. return 0;
  4013. }
  4014. static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
  4015. struct inode * new_dir,struct dentry *new_dentry)
  4016. {
  4017. struct btrfs_trans_handle *trans;
  4018. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  4019. struct inode *new_inode = new_dentry->d_inode;
  4020. struct inode *old_inode = old_dentry->d_inode;
  4021. struct timespec ctime = CURRENT_TIME;
  4022. u64 index = 0;
  4023. int ret;
  4024. /* we're not allowed to rename between subvolumes */
  4025. if (BTRFS_I(old_inode)->root->root_key.objectid !=
  4026. BTRFS_I(new_dir)->root->root_key.objectid)
  4027. return -EXDEV;
  4028. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  4029. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
  4030. return -ENOTEMPTY;
  4031. }
  4032. /* to rename a snapshot or subvolume, we need to juggle the
  4033. * backrefs. This isn't coded yet
  4034. */
  4035. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4036. return -EXDEV;
  4037. ret = btrfs_check_free_space(root, 1, 0);
  4038. if (ret)
  4039. goto out_unlock;
  4040. trans = btrfs_start_transaction(root, 1);
  4041. btrfs_set_trans_block_group(trans, new_dir);
  4042. btrfs_inc_nlink(old_dentry->d_inode);
  4043. old_dir->i_ctime = old_dir->i_mtime = ctime;
  4044. new_dir->i_ctime = new_dir->i_mtime = ctime;
  4045. old_inode->i_ctime = ctime;
  4046. ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
  4047. old_dentry->d_name.name,
  4048. old_dentry->d_name.len);
  4049. if (ret)
  4050. goto out_fail;
  4051. if (new_inode) {
  4052. new_inode->i_ctime = CURRENT_TIME;
  4053. ret = btrfs_unlink_inode(trans, root, new_dir,
  4054. new_dentry->d_inode,
  4055. new_dentry->d_name.name,
  4056. new_dentry->d_name.len);
  4057. if (ret)
  4058. goto out_fail;
  4059. if (new_inode->i_nlink == 0) {
  4060. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  4061. if (ret)
  4062. goto out_fail;
  4063. }
  4064. }
  4065. ret = btrfs_set_inode_index(new_dir, &index);
  4066. if (ret)
  4067. goto out_fail;
  4068. ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
  4069. old_inode, new_dentry->d_name.name,
  4070. new_dentry->d_name.len, 1, index);
  4071. if (ret)
  4072. goto out_fail;
  4073. out_fail:
  4074. btrfs_end_transaction_throttle(trans, root);
  4075. out_unlock:
  4076. return ret;
  4077. }
  4078. /*
  4079. * some fairly slow code that needs optimization. This walks the list
  4080. * of all the inodes with pending delalloc and forces them to disk.
  4081. */
  4082. int btrfs_start_delalloc_inodes(struct btrfs_root *root)
  4083. {
  4084. struct list_head *head = &root->fs_info->delalloc_inodes;
  4085. struct btrfs_inode *binode;
  4086. struct inode *inode;
  4087. unsigned long flags;
  4088. if (root->fs_info->sb->s_flags & MS_RDONLY)
  4089. return -EROFS;
  4090. spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
  4091. while(!list_empty(head)) {
  4092. binode = list_entry(head->next, struct btrfs_inode,
  4093. delalloc_inodes);
  4094. inode = igrab(&binode->vfs_inode);
  4095. if (!inode)
  4096. list_del_init(&binode->delalloc_inodes);
  4097. spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
  4098. if (inode) {
  4099. filemap_flush(inode->i_mapping);
  4100. iput(inode);
  4101. }
  4102. cond_resched();
  4103. spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
  4104. }
  4105. spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
  4106. /* the filemap_flush will queue IO into the worker threads, but
  4107. * we have to make sure the IO is actually started and that
  4108. * ordered extents get created before we return
  4109. */
  4110. atomic_inc(&root->fs_info->async_submit_draining);
  4111. while(atomic_read(&root->fs_info->nr_async_submits) ||
  4112. atomic_read(&root->fs_info->async_delalloc_pages)) {
  4113. wait_event(root->fs_info->async_submit_wait,
  4114. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  4115. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  4116. }
  4117. atomic_dec(&root->fs_info->async_submit_draining);
  4118. return 0;
  4119. }
  4120. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  4121. const char *symname)
  4122. {
  4123. struct btrfs_trans_handle *trans;
  4124. struct btrfs_root *root = BTRFS_I(dir)->root;
  4125. struct btrfs_path *path;
  4126. struct btrfs_key key;
  4127. struct inode *inode = NULL;
  4128. int err;
  4129. int drop_inode = 0;
  4130. u64 objectid;
  4131. u64 index = 0 ;
  4132. int name_len;
  4133. int datasize;
  4134. unsigned long ptr;
  4135. struct btrfs_file_extent_item *ei;
  4136. struct extent_buffer *leaf;
  4137. unsigned long nr = 0;
  4138. name_len = strlen(symname) + 1;
  4139. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  4140. return -ENAMETOOLONG;
  4141. err = btrfs_check_free_space(root, 1, 0);
  4142. if (err)
  4143. goto out_fail;
  4144. trans = btrfs_start_transaction(root, 1);
  4145. btrfs_set_trans_block_group(trans, dir);
  4146. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  4147. if (err) {
  4148. err = -ENOSPC;
  4149. goto out_unlock;
  4150. }
  4151. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4152. dentry->d_name.len,
  4153. dentry->d_parent->d_inode->i_ino, objectid,
  4154. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  4155. &index);
  4156. err = PTR_ERR(inode);
  4157. if (IS_ERR(inode))
  4158. goto out_unlock;
  4159. err = btrfs_init_acl(inode, dir);
  4160. if (err) {
  4161. drop_inode = 1;
  4162. goto out_unlock;
  4163. }
  4164. btrfs_set_trans_block_group(trans, inode);
  4165. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  4166. if (err)
  4167. drop_inode = 1;
  4168. else {
  4169. inode->i_mapping->a_ops = &btrfs_aops;
  4170. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4171. inode->i_fop = &btrfs_file_operations;
  4172. inode->i_op = &btrfs_file_inode_operations;
  4173. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4174. }
  4175. dir->i_sb->s_dirt = 1;
  4176. btrfs_update_inode_block_group(trans, inode);
  4177. btrfs_update_inode_block_group(trans, dir);
  4178. if (drop_inode)
  4179. goto out_unlock;
  4180. path = btrfs_alloc_path();
  4181. BUG_ON(!path);
  4182. key.objectid = inode->i_ino;
  4183. key.offset = 0;
  4184. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  4185. datasize = btrfs_file_extent_calc_inline_size(name_len);
  4186. err = btrfs_insert_empty_item(trans, root, path, &key,
  4187. datasize);
  4188. if (err) {
  4189. drop_inode = 1;
  4190. goto out_unlock;
  4191. }
  4192. leaf = path->nodes[0];
  4193. ei = btrfs_item_ptr(leaf, path->slots[0],
  4194. struct btrfs_file_extent_item);
  4195. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  4196. btrfs_set_file_extent_type(leaf, ei,
  4197. BTRFS_FILE_EXTENT_INLINE);
  4198. btrfs_set_file_extent_encryption(leaf, ei, 0);
  4199. btrfs_set_file_extent_compression(leaf, ei, 0);
  4200. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  4201. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  4202. ptr = btrfs_file_extent_inline_start(ei);
  4203. write_extent_buffer(leaf, symname, ptr, name_len);
  4204. btrfs_mark_buffer_dirty(leaf);
  4205. btrfs_free_path(path);
  4206. inode->i_op = &btrfs_symlink_inode_operations;
  4207. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  4208. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4209. inode_set_bytes(inode, name_len);
  4210. btrfs_i_size_write(inode, name_len - 1);
  4211. err = btrfs_update_inode(trans, root, inode);
  4212. if (err)
  4213. drop_inode = 1;
  4214. out_unlock:
  4215. nr = trans->blocks_used;
  4216. btrfs_end_transaction_throttle(trans, root);
  4217. out_fail:
  4218. if (drop_inode) {
  4219. inode_dec_link_count(inode);
  4220. iput(inode);
  4221. }
  4222. btrfs_btree_balance_dirty(root, nr);
  4223. return err;
  4224. }
  4225. static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
  4226. u64 alloc_hint, int mode)
  4227. {
  4228. struct btrfs_trans_handle *trans;
  4229. struct btrfs_root *root = BTRFS_I(inode)->root;
  4230. struct btrfs_key ins;
  4231. u64 alloc_size;
  4232. u64 cur_offset = start;
  4233. u64 num_bytes = end - start;
  4234. int ret = 0;
  4235. trans = btrfs_join_transaction(root, 1);
  4236. BUG_ON(!trans);
  4237. btrfs_set_trans_block_group(trans, inode);
  4238. while (num_bytes > 0) {
  4239. alloc_size = min(num_bytes, root->fs_info->max_extent);
  4240. ret = btrfs_reserve_extent(trans, root, alloc_size,
  4241. root->sectorsize, 0, alloc_hint,
  4242. (u64)-1, &ins, 1);
  4243. if (ret) {
  4244. WARN_ON(1);
  4245. goto out;
  4246. }
  4247. ret = insert_reserved_file_extent(trans, inode,
  4248. cur_offset, ins.objectid,
  4249. ins.offset, ins.offset,
  4250. ins.offset, 0, 0, 0,
  4251. BTRFS_FILE_EXTENT_PREALLOC);
  4252. BUG_ON(ret);
  4253. num_bytes -= ins.offset;
  4254. cur_offset += ins.offset;
  4255. alloc_hint = ins.objectid + ins.offset;
  4256. }
  4257. out:
  4258. if (cur_offset > start) {
  4259. inode->i_ctime = CURRENT_TIME;
  4260. btrfs_set_flag(inode, PREALLOC);
  4261. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  4262. cur_offset > i_size_read(inode))
  4263. btrfs_i_size_write(inode, cur_offset);
  4264. ret = btrfs_update_inode(trans, root, inode);
  4265. BUG_ON(ret);
  4266. }
  4267. btrfs_end_transaction(trans, root);
  4268. return ret;
  4269. }
  4270. static long btrfs_fallocate(struct inode *inode, int mode,
  4271. loff_t offset, loff_t len)
  4272. {
  4273. u64 cur_offset;
  4274. u64 last_byte;
  4275. u64 alloc_start;
  4276. u64 alloc_end;
  4277. u64 alloc_hint = 0;
  4278. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  4279. struct extent_map *em;
  4280. int ret;
  4281. alloc_start = offset & ~mask;
  4282. alloc_end = (offset + len + mask) & ~mask;
  4283. mutex_lock(&inode->i_mutex);
  4284. if (alloc_start > inode->i_size) {
  4285. ret = btrfs_cont_expand(inode, alloc_start);
  4286. if (ret)
  4287. goto out;
  4288. }
  4289. while (1) {
  4290. struct btrfs_ordered_extent *ordered;
  4291. lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
  4292. alloc_end - 1, GFP_NOFS);
  4293. ordered = btrfs_lookup_first_ordered_extent(inode,
  4294. alloc_end - 1);
  4295. if (ordered &&
  4296. ordered->file_offset + ordered->len > alloc_start &&
  4297. ordered->file_offset < alloc_end) {
  4298. btrfs_put_ordered_extent(ordered);
  4299. unlock_extent(&BTRFS_I(inode)->io_tree,
  4300. alloc_start, alloc_end - 1, GFP_NOFS);
  4301. btrfs_wait_ordered_range(inode, alloc_start,
  4302. alloc_end - alloc_start);
  4303. } else {
  4304. if (ordered)
  4305. btrfs_put_ordered_extent(ordered);
  4306. break;
  4307. }
  4308. }
  4309. cur_offset = alloc_start;
  4310. while (1) {
  4311. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  4312. alloc_end - cur_offset, 0);
  4313. BUG_ON(IS_ERR(em) || !em);
  4314. last_byte = min(extent_map_end(em), alloc_end);
  4315. last_byte = (last_byte + mask) & ~mask;
  4316. if (em->block_start == EXTENT_MAP_HOLE) {
  4317. ret = prealloc_file_range(inode, cur_offset,
  4318. last_byte, alloc_hint, mode);
  4319. if (ret < 0) {
  4320. free_extent_map(em);
  4321. break;
  4322. }
  4323. }
  4324. if (em->block_start <= EXTENT_MAP_LAST_BYTE)
  4325. alloc_hint = em->block_start;
  4326. free_extent_map(em);
  4327. cur_offset = last_byte;
  4328. if (cur_offset >= alloc_end) {
  4329. ret = 0;
  4330. break;
  4331. }
  4332. }
  4333. unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
  4334. GFP_NOFS);
  4335. out:
  4336. mutex_unlock(&inode->i_mutex);
  4337. return ret;
  4338. }
  4339. static int btrfs_set_page_dirty(struct page *page)
  4340. {
  4341. return __set_page_dirty_nobuffers(page);
  4342. }
  4343. static int btrfs_permission(struct inode *inode, int mask)
  4344. {
  4345. if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
  4346. return -EACCES;
  4347. return generic_permission(inode, mask, btrfs_check_acl);
  4348. }
  4349. static struct inode_operations btrfs_dir_inode_operations = {
  4350. .getattr = btrfs_getattr,
  4351. .lookup = btrfs_lookup,
  4352. .create = btrfs_create,
  4353. .unlink = btrfs_unlink,
  4354. .link = btrfs_link,
  4355. .mkdir = btrfs_mkdir,
  4356. .rmdir = btrfs_rmdir,
  4357. .rename = btrfs_rename,
  4358. .symlink = btrfs_symlink,
  4359. .setattr = btrfs_setattr,
  4360. .mknod = btrfs_mknod,
  4361. .setxattr = btrfs_setxattr,
  4362. .getxattr = btrfs_getxattr,
  4363. .listxattr = btrfs_listxattr,
  4364. .removexattr = btrfs_removexattr,
  4365. .permission = btrfs_permission,
  4366. };
  4367. static struct inode_operations btrfs_dir_ro_inode_operations = {
  4368. .lookup = btrfs_lookup,
  4369. .permission = btrfs_permission,
  4370. };
  4371. static struct file_operations btrfs_dir_file_operations = {
  4372. .llseek = generic_file_llseek,
  4373. .read = generic_read_dir,
  4374. .readdir = btrfs_real_readdir,
  4375. .unlocked_ioctl = btrfs_ioctl,
  4376. #ifdef CONFIG_COMPAT
  4377. .compat_ioctl = btrfs_ioctl,
  4378. #endif
  4379. .release = btrfs_release_file,
  4380. .fsync = btrfs_sync_file,
  4381. };
  4382. static struct extent_io_ops btrfs_extent_io_ops = {
  4383. .fill_delalloc = run_delalloc_range,
  4384. .submit_bio_hook = btrfs_submit_bio_hook,
  4385. .merge_bio_hook = btrfs_merge_bio_hook,
  4386. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  4387. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  4388. .writepage_start_hook = btrfs_writepage_start_hook,
  4389. .readpage_io_failed_hook = btrfs_io_failed_hook,
  4390. .set_bit_hook = btrfs_set_bit_hook,
  4391. .clear_bit_hook = btrfs_clear_bit_hook,
  4392. };
  4393. static struct address_space_operations btrfs_aops = {
  4394. .readpage = btrfs_readpage,
  4395. .writepage = btrfs_writepage,
  4396. .writepages = btrfs_writepages,
  4397. .readpages = btrfs_readpages,
  4398. .sync_page = block_sync_page,
  4399. .bmap = btrfs_bmap,
  4400. .direct_IO = btrfs_direct_IO,
  4401. .invalidatepage = btrfs_invalidatepage,
  4402. .releasepage = btrfs_releasepage,
  4403. .set_page_dirty = btrfs_set_page_dirty,
  4404. };
  4405. static struct address_space_operations btrfs_symlink_aops = {
  4406. .readpage = btrfs_readpage,
  4407. .writepage = btrfs_writepage,
  4408. .invalidatepage = btrfs_invalidatepage,
  4409. .releasepage = btrfs_releasepage,
  4410. };
  4411. static struct inode_operations btrfs_file_inode_operations = {
  4412. .truncate = btrfs_truncate,
  4413. .getattr = btrfs_getattr,
  4414. .setattr = btrfs_setattr,
  4415. .setxattr = btrfs_setxattr,
  4416. .getxattr = btrfs_getxattr,
  4417. .listxattr = btrfs_listxattr,
  4418. .removexattr = btrfs_removexattr,
  4419. .permission = btrfs_permission,
  4420. .fallocate = btrfs_fallocate,
  4421. };
  4422. static struct inode_operations btrfs_special_inode_operations = {
  4423. .getattr = btrfs_getattr,
  4424. .setattr = btrfs_setattr,
  4425. .permission = btrfs_permission,
  4426. .setxattr = btrfs_setxattr,
  4427. .getxattr = btrfs_getxattr,
  4428. .listxattr = btrfs_listxattr,
  4429. .removexattr = btrfs_removexattr,
  4430. };
  4431. static struct inode_operations btrfs_symlink_inode_operations = {
  4432. .readlink = generic_readlink,
  4433. .follow_link = page_follow_link_light,
  4434. .put_link = page_put_link,
  4435. .permission = btrfs_permission,
  4436. };