inode.c 156 KB

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