inode.c 135 KB

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