inode.c 135 KB

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