inode.c 143 KB

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