inode.c 144 KB

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