inode.c 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077
  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. struct btrfs_path *path;
  1343. int compressed = 0;
  1344. int ret;
  1345. ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
  1346. if (!ret)
  1347. return 0;
  1348. /*
  1349. * before we join the transaction, try to do some of our IO.
  1350. * This will limit the amount of IO that we have to do with
  1351. * the transaction running. We're unlikely to need to do any
  1352. * IO if the file extents are new, the disk_i_size checks
  1353. * covers the most common case.
  1354. */
  1355. if (start < BTRFS_I(inode)->disk_i_size) {
  1356. path = btrfs_alloc_path();
  1357. if (path) {
  1358. ret = btrfs_lookup_file_extent(NULL, root, path,
  1359. inode->i_ino,
  1360. start, 0);
  1361. btrfs_free_path(path);
  1362. }
  1363. }
  1364. trans = btrfs_join_transaction(root, 1);
  1365. ordered_extent = btrfs_lookup_ordered_extent(inode, start);
  1366. BUG_ON(!ordered_extent);
  1367. if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
  1368. goto nocow;
  1369. lock_extent(io_tree, ordered_extent->file_offset,
  1370. ordered_extent->file_offset + ordered_extent->len - 1,
  1371. GFP_NOFS);
  1372. if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
  1373. compressed = 1;
  1374. if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
  1375. BUG_ON(compressed);
  1376. ret = btrfs_mark_extent_written(trans, root, inode,
  1377. ordered_extent->file_offset,
  1378. ordered_extent->file_offset +
  1379. ordered_extent->len);
  1380. BUG_ON(ret);
  1381. } else {
  1382. ret = insert_reserved_file_extent(trans, inode,
  1383. ordered_extent->file_offset,
  1384. ordered_extent->start,
  1385. ordered_extent->disk_len,
  1386. ordered_extent->len,
  1387. ordered_extent->len,
  1388. compressed, 0, 0,
  1389. BTRFS_FILE_EXTENT_REG);
  1390. BUG_ON(ret);
  1391. }
  1392. unlock_extent(io_tree, ordered_extent->file_offset,
  1393. ordered_extent->file_offset + ordered_extent->len - 1,
  1394. GFP_NOFS);
  1395. nocow:
  1396. add_pending_csums(trans, inode, ordered_extent->file_offset,
  1397. &ordered_extent->list);
  1398. mutex_lock(&BTRFS_I(inode)->extent_mutex);
  1399. btrfs_ordered_update_i_size(inode, ordered_extent);
  1400. btrfs_update_inode(trans, root, inode);
  1401. btrfs_remove_ordered_extent(inode, ordered_extent);
  1402. mutex_unlock(&BTRFS_I(inode)->extent_mutex);
  1403. /* once for us */
  1404. btrfs_put_ordered_extent(ordered_extent);
  1405. /* once for the tree */
  1406. btrfs_put_ordered_extent(ordered_extent);
  1407. btrfs_end_transaction(trans, root);
  1408. return 0;
  1409. }
  1410. static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
  1411. struct extent_state *state, int uptodate)
  1412. {
  1413. return btrfs_finish_ordered_io(page->mapping->host, start, end);
  1414. }
  1415. /*
  1416. * When IO fails, either with EIO or csum verification fails, we
  1417. * try other mirrors that might have a good copy of the data. This
  1418. * io_failure_record is used to record state as we go through all the
  1419. * mirrors. If another mirror has good data, the page is set up to date
  1420. * and things continue. If a good mirror can't be found, the original
  1421. * bio end_io callback is called to indicate things have failed.
  1422. */
  1423. struct io_failure_record {
  1424. struct page *page;
  1425. u64 start;
  1426. u64 len;
  1427. u64 logical;
  1428. unsigned long bio_flags;
  1429. int last_mirror;
  1430. };
  1431. static int btrfs_io_failed_hook(struct bio *failed_bio,
  1432. struct page *page, u64 start, u64 end,
  1433. struct extent_state *state)
  1434. {
  1435. struct io_failure_record *failrec = NULL;
  1436. u64 private;
  1437. struct extent_map *em;
  1438. struct inode *inode = page->mapping->host;
  1439. struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
  1440. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  1441. struct bio *bio;
  1442. int num_copies;
  1443. int ret;
  1444. int rw;
  1445. u64 logical;
  1446. ret = get_state_private(failure_tree, start, &private);
  1447. if (ret) {
  1448. failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
  1449. if (!failrec)
  1450. return -ENOMEM;
  1451. failrec->start = start;
  1452. failrec->len = end - start + 1;
  1453. failrec->last_mirror = 0;
  1454. failrec->bio_flags = 0;
  1455. spin_lock(&em_tree->lock);
  1456. em = lookup_extent_mapping(em_tree, start, failrec->len);
  1457. if (em->start > start || em->start + em->len < start) {
  1458. free_extent_map(em);
  1459. em = NULL;
  1460. }
  1461. spin_unlock(&em_tree->lock);
  1462. if (!em || IS_ERR(em)) {
  1463. kfree(failrec);
  1464. return -EIO;
  1465. }
  1466. logical = start - em->start;
  1467. logical = em->block_start + logical;
  1468. if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  1469. logical = em->block_start;
  1470. failrec->bio_flags = EXTENT_BIO_COMPRESSED;
  1471. }
  1472. failrec->logical = logical;
  1473. free_extent_map(em);
  1474. set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
  1475. EXTENT_DIRTY, GFP_NOFS);
  1476. set_state_private(failure_tree, start,
  1477. (u64)(unsigned long)failrec);
  1478. } else {
  1479. failrec = (struct io_failure_record *)(unsigned long)private;
  1480. }
  1481. num_copies = btrfs_num_copies(
  1482. &BTRFS_I(inode)->root->fs_info->mapping_tree,
  1483. failrec->logical, failrec->len);
  1484. failrec->last_mirror++;
  1485. if (!state) {
  1486. spin_lock(&BTRFS_I(inode)->io_tree.lock);
  1487. state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
  1488. failrec->start,
  1489. EXTENT_LOCKED);
  1490. if (state && state->start != failrec->start)
  1491. state = NULL;
  1492. spin_unlock(&BTRFS_I(inode)->io_tree.lock);
  1493. }
  1494. if (!state || failrec->last_mirror > num_copies) {
  1495. set_state_private(failure_tree, failrec->start, 0);
  1496. clear_extent_bits(failure_tree, failrec->start,
  1497. failrec->start + failrec->len - 1,
  1498. EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
  1499. kfree(failrec);
  1500. return -EIO;
  1501. }
  1502. bio = bio_alloc(GFP_NOFS, 1);
  1503. bio->bi_private = state;
  1504. bio->bi_end_io = failed_bio->bi_end_io;
  1505. bio->bi_sector = failrec->logical >> 9;
  1506. bio->bi_bdev = failed_bio->bi_bdev;
  1507. bio->bi_size = 0;
  1508. bio_add_page(bio, page, failrec->len, start - page_offset(page));
  1509. if (failed_bio->bi_rw & (1 << BIO_RW))
  1510. rw = WRITE;
  1511. else
  1512. rw = READ;
  1513. BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
  1514. failrec->last_mirror,
  1515. failrec->bio_flags);
  1516. return 0;
  1517. }
  1518. /*
  1519. * each time an IO finishes, we do a fast check in the IO failure tree
  1520. * to see if we need to process or clean up an io_failure_record
  1521. */
  1522. static int btrfs_clean_io_failures(struct inode *inode, u64 start)
  1523. {
  1524. u64 private;
  1525. u64 private_failure;
  1526. struct io_failure_record *failure;
  1527. int ret;
  1528. private = 0;
  1529. if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
  1530. (u64)-1, 1, EXTENT_DIRTY)) {
  1531. ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
  1532. start, &private_failure);
  1533. if (ret == 0) {
  1534. failure = (struct io_failure_record *)(unsigned long)
  1535. private_failure;
  1536. set_state_private(&BTRFS_I(inode)->io_failure_tree,
  1537. failure->start, 0);
  1538. clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
  1539. failure->start,
  1540. failure->start + failure->len - 1,
  1541. EXTENT_DIRTY | EXTENT_LOCKED,
  1542. GFP_NOFS);
  1543. kfree(failure);
  1544. }
  1545. }
  1546. return 0;
  1547. }
  1548. /*
  1549. * when reads are done, we need to check csums to verify the data is correct
  1550. * if there's a match, we allow the bio to finish. If not, we go through
  1551. * the io_failure_record routines to find good copies
  1552. */
  1553. static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
  1554. struct extent_state *state)
  1555. {
  1556. size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
  1557. struct inode *inode = page->mapping->host;
  1558. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  1559. char *kaddr;
  1560. u64 private = ~(u32)0;
  1561. int ret;
  1562. struct btrfs_root *root = BTRFS_I(inode)->root;
  1563. u32 csum = ~(u32)0;
  1564. if (PageChecked(page)) {
  1565. ClearPageChecked(page);
  1566. goto good;
  1567. }
  1568. if (btrfs_test_flag(inode, NODATASUM))
  1569. return 0;
  1570. if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
  1571. test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1)) {
  1572. clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
  1573. GFP_NOFS);
  1574. return 0;
  1575. }
  1576. if (state && state->start == start) {
  1577. private = state->private;
  1578. ret = 0;
  1579. } else {
  1580. ret = get_state_private(io_tree, start, &private);
  1581. }
  1582. kaddr = kmap_atomic(page, KM_USER0);
  1583. if (ret)
  1584. goto zeroit;
  1585. csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
  1586. btrfs_csum_final(csum, (char *)&csum);
  1587. if (csum != private)
  1588. goto zeroit;
  1589. kunmap_atomic(kaddr, KM_USER0);
  1590. good:
  1591. /* if the io failure tree for this inode is non-empty,
  1592. * check to see if we've recovered from a failed IO
  1593. */
  1594. btrfs_clean_io_failures(inode, start);
  1595. return 0;
  1596. zeroit:
  1597. printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
  1598. "private %llu\n", page->mapping->host->i_ino,
  1599. (unsigned long long)start, csum,
  1600. (unsigned long long)private);
  1601. memset(kaddr + offset, 1, end - start + 1);
  1602. flush_dcache_page(page);
  1603. kunmap_atomic(kaddr, KM_USER0);
  1604. if (private == 0)
  1605. return 0;
  1606. return -EIO;
  1607. }
  1608. /*
  1609. * This creates an orphan entry for the given inode in case something goes
  1610. * wrong in the middle of an unlink/truncate.
  1611. */
  1612. int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
  1613. {
  1614. struct btrfs_root *root = BTRFS_I(inode)->root;
  1615. int ret = 0;
  1616. spin_lock(&root->list_lock);
  1617. /* already on the orphan list, we're good */
  1618. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  1619. spin_unlock(&root->list_lock);
  1620. return 0;
  1621. }
  1622. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1623. spin_unlock(&root->list_lock);
  1624. /*
  1625. * insert an orphan item to track this unlinked/truncated file
  1626. */
  1627. ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
  1628. return ret;
  1629. }
  1630. /*
  1631. * We have done the truncate/delete so we can go ahead and remove the orphan
  1632. * item for this particular inode.
  1633. */
  1634. int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
  1635. {
  1636. struct btrfs_root *root = BTRFS_I(inode)->root;
  1637. int ret = 0;
  1638. spin_lock(&root->list_lock);
  1639. if (list_empty(&BTRFS_I(inode)->i_orphan)) {
  1640. spin_unlock(&root->list_lock);
  1641. return 0;
  1642. }
  1643. list_del_init(&BTRFS_I(inode)->i_orphan);
  1644. if (!trans) {
  1645. spin_unlock(&root->list_lock);
  1646. return 0;
  1647. }
  1648. spin_unlock(&root->list_lock);
  1649. ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
  1650. return ret;
  1651. }
  1652. /*
  1653. * this cleans up any orphans that may be left on the list from the last use
  1654. * of this root.
  1655. */
  1656. void btrfs_orphan_cleanup(struct btrfs_root *root)
  1657. {
  1658. struct btrfs_path *path;
  1659. struct extent_buffer *leaf;
  1660. struct btrfs_item *item;
  1661. struct btrfs_key key, found_key;
  1662. struct btrfs_trans_handle *trans;
  1663. struct inode *inode;
  1664. int ret = 0, nr_unlink = 0, nr_truncate = 0;
  1665. path = btrfs_alloc_path();
  1666. if (!path)
  1667. return;
  1668. path->reada = -1;
  1669. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1670. btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
  1671. key.offset = (u64)-1;
  1672. while (1) {
  1673. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1674. if (ret < 0) {
  1675. printk(KERN_ERR "Error searching slot for orphan: %d"
  1676. "\n", ret);
  1677. break;
  1678. }
  1679. /*
  1680. * if ret == 0 means we found what we were searching for, which
  1681. * is weird, but possible, so only screw with path if we didnt
  1682. * find the key and see if we have stuff that matches
  1683. */
  1684. if (ret > 0) {
  1685. if (path->slots[0] == 0)
  1686. break;
  1687. path->slots[0]--;
  1688. }
  1689. /* pull out the item */
  1690. leaf = path->nodes[0];
  1691. item = btrfs_item_nr(leaf, path->slots[0]);
  1692. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1693. /* make sure the item matches what we want */
  1694. if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
  1695. break;
  1696. if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
  1697. break;
  1698. /* release the path since we're done with it */
  1699. btrfs_release_path(root, path);
  1700. /*
  1701. * this is where we are basically btrfs_lookup, without the
  1702. * crossing root thing. we store the inode number in the
  1703. * offset of the orphan item.
  1704. */
  1705. inode = btrfs_iget_locked(root->fs_info->sb,
  1706. found_key.offset, root);
  1707. if (!inode)
  1708. break;
  1709. if (inode->i_state & I_NEW) {
  1710. BTRFS_I(inode)->root = root;
  1711. /* have to set the location manually */
  1712. BTRFS_I(inode)->location.objectid = inode->i_ino;
  1713. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  1714. BTRFS_I(inode)->location.offset = 0;
  1715. btrfs_read_locked_inode(inode);
  1716. unlock_new_inode(inode);
  1717. }
  1718. /*
  1719. * add this inode to the orphan list so btrfs_orphan_del does
  1720. * the proper thing when we hit it
  1721. */
  1722. spin_lock(&root->list_lock);
  1723. list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
  1724. spin_unlock(&root->list_lock);
  1725. /*
  1726. * if this is a bad inode, means we actually succeeded in
  1727. * removing the inode, but not the orphan record, which means
  1728. * we need to manually delete the orphan since iput will just
  1729. * do a destroy_inode
  1730. */
  1731. if (is_bad_inode(inode)) {
  1732. trans = btrfs_start_transaction(root, 1);
  1733. btrfs_orphan_del(trans, inode);
  1734. btrfs_end_transaction(trans, root);
  1735. iput(inode);
  1736. continue;
  1737. }
  1738. /* if we have links, this was a truncate, lets do that */
  1739. if (inode->i_nlink) {
  1740. nr_truncate++;
  1741. btrfs_truncate(inode);
  1742. } else {
  1743. nr_unlink++;
  1744. }
  1745. /* this will do delete_inode and everything for us */
  1746. iput(inode);
  1747. }
  1748. if (nr_unlink)
  1749. printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
  1750. if (nr_truncate)
  1751. printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
  1752. btrfs_free_path(path);
  1753. }
  1754. /*
  1755. * read an inode from the btree into the in-memory inode
  1756. */
  1757. void btrfs_read_locked_inode(struct inode *inode)
  1758. {
  1759. struct btrfs_path *path;
  1760. struct extent_buffer *leaf;
  1761. struct btrfs_inode_item *inode_item;
  1762. struct btrfs_timespec *tspec;
  1763. struct btrfs_root *root = BTRFS_I(inode)->root;
  1764. struct btrfs_key location;
  1765. u64 alloc_group_block;
  1766. u32 rdev;
  1767. int ret;
  1768. path = btrfs_alloc_path();
  1769. BUG_ON(!path);
  1770. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  1771. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  1772. if (ret)
  1773. goto make_bad;
  1774. leaf = path->nodes[0];
  1775. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1776. struct btrfs_inode_item);
  1777. inode->i_mode = btrfs_inode_mode(leaf, inode_item);
  1778. inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
  1779. inode->i_uid = btrfs_inode_uid(leaf, inode_item);
  1780. inode->i_gid = btrfs_inode_gid(leaf, inode_item);
  1781. btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
  1782. tspec = btrfs_inode_atime(inode_item);
  1783. inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1784. inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1785. tspec = btrfs_inode_mtime(inode_item);
  1786. inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1787. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1788. tspec = btrfs_inode_ctime(inode_item);
  1789. inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
  1790. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
  1791. inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
  1792. BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
  1793. BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
  1794. inode->i_generation = BTRFS_I(inode)->generation;
  1795. inode->i_rdev = 0;
  1796. rdev = btrfs_inode_rdev(leaf, inode_item);
  1797. BTRFS_I(inode)->index_cnt = (u64)-1;
  1798. BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
  1799. alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
  1800. BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
  1801. alloc_group_block, 0);
  1802. btrfs_free_path(path);
  1803. inode_item = NULL;
  1804. switch (inode->i_mode & S_IFMT) {
  1805. case S_IFREG:
  1806. inode->i_mapping->a_ops = &btrfs_aops;
  1807. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1808. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  1809. inode->i_fop = &btrfs_file_operations;
  1810. inode->i_op = &btrfs_file_inode_operations;
  1811. break;
  1812. case S_IFDIR:
  1813. inode->i_fop = &btrfs_dir_file_operations;
  1814. if (root == root->fs_info->tree_root)
  1815. inode->i_op = &btrfs_dir_ro_inode_operations;
  1816. else
  1817. inode->i_op = &btrfs_dir_inode_operations;
  1818. break;
  1819. case S_IFLNK:
  1820. inode->i_op = &btrfs_symlink_inode_operations;
  1821. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  1822. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  1823. break;
  1824. default:
  1825. inode->i_op = &btrfs_special_inode_operations;
  1826. init_special_inode(inode, inode->i_mode, rdev);
  1827. break;
  1828. }
  1829. return;
  1830. make_bad:
  1831. btrfs_free_path(path);
  1832. make_bad_inode(inode);
  1833. }
  1834. /*
  1835. * given a leaf and an inode, copy the inode fields into the leaf
  1836. */
  1837. static void fill_inode_item(struct btrfs_trans_handle *trans,
  1838. struct extent_buffer *leaf,
  1839. struct btrfs_inode_item *item,
  1840. struct inode *inode)
  1841. {
  1842. btrfs_set_inode_uid(leaf, item, inode->i_uid);
  1843. btrfs_set_inode_gid(leaf, item, inode->i_gid);
  1844. btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
  1845. btrfs_set_inode_mode(leaf, item, inode->i_mode);
  1846. btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
  1847. btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
  1848. inode->i_atime.tv_sec);
  1849. btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
  1850. inode->i_atime.tv_nsec);
  1851. btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
  1852. inode->i_mtime.tv_sec);
  1853. btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
  1854. inode->i_mtime.tv_nsec);
  1855. btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
  1856. inode->i_ctime.tv_sec);
  1857. btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
  1858. inode->i_ctime.tv_nsec);
  1859. btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
  1860. btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
  1861. btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
  1862. btrfs_set_inode_transid(leaf, item, trans->transid);
  1863. btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
  1864. btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
  1865. btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
  1866. }
  1867. /*
  1868. * copy everything in the in-memory inode into the btree.
  1869. */
  1870. noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
  1871. struct btrfs_root *root, struct inode *inode)
  1872. {
  1873. struct btrfs_inode_item *inode_item;
  1874. struct btrfs_path *path;
  1875. struct extent_buffer *leaf;
  1876. int ret;
  1877. path = btrfs_alloc_path();
  1878. BUG_ON(!path);
  1879. ret = btrfs_lookup_inode(trans, root, path,
  1880. &BTRFS_I(inode)->location, 1);
  1881. if (ret) {
  1882. if (ret > 0)
  1883. ret = -ENOENT;
  1884. goto failed;
  1885. }
  1886. btrfs_unlock_up_safe(path, 1);
  1887. leaf = path->nodes[0];
  1888. inode_item = btrfs_item_ptr(leaf, path->slots[0],
  1889. struct btrfs_inode_item);
  1890. fill_inode_item(trans, leaf, inode_item, inode);
  1891. btrfs_mark_buffer_dirty(leaf);
  1892. btrfs_set_inode_last_trans(trans, inode);
  1893. ret = 0;
  1894. failed:
  1895. btrfs_free_path(path);
  1896. return ret;
  1897. }
  1898. /*
  1899. * unlink helper that gets used here in inode.c and in the tree logging
  1900. * recovery code. It remove a link in a directory with a given name, and
  1901. * also drops the back refs in the inode to the directory
  1902. */
  1903. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  1904. struct btrfs_root *root,
  1905. struct inode *dir, struct inode *inode,
  1906. const char *name, int name_len)
  1907. {
  1908. struct btrfs_path *path;
  1909. int ret = 0;
  1910. struct extent_buffer *leaf;
  1911. struct btrfs_dir_item *di;
  1912. struct btrfs_key key;
  1913. u64 index;
  1914. path = btrfs_alloc_path();
  1915. if (!path) {
  1916. ret = -ENOMEM;
  1917. goto err;
  1918. }
  1919. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  1920. name, name_len, -1);
  1921. if (IS_ERR(di)) {
  1922. ret = PTR_ERR(di);
  1923. goto err;
  1924. }
  1925. if (!di) {
  1926. ret = -ENOENT;
  1927. goto err;
  1928. }
  1929. leaf = path->nodes[0];
  1930. btrfs_dir_item_key_to_cpu(leaf, di, &key);
  1931. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  1932. if (ret)
  1933. goto err;
  1934. btrfs_release_path(root, path);
  1935. ret = btrfs_del_inode_ref(trans, root, name, name_len,
  1936. inode->i_ino,
  1937. dir->i_ino, &index);
  1938. if (ret) {
  1939. printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
  1940. "inode %lu parent %lu\n", name_len, name,
  1941. inode->i_ino, dir->i_ino);
  1942. goto err;
  1943. }
  1944. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  1945. index, name, name_len, -1);
  1946. if (IS_ERR(di)) {
  1947. ret = PTR_ERR(di);
  1948. goto err;
  1949. }
  1950. if (!di) {
  1951. ret = -ENOENT;
  1952. goto err;
  1953. }
  1954. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  1955. btrfs_release_path(root, path);
  1956. ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
  1957. inode, dir->i_ino);
  1958. BUG_ON(ret != 0 && ret != -ENOENT);
  1959. if (ret != -ENOENT)
  1960. BTRFS_I(dir)->log_dirty_trans = trans->transid;
  1961. ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
  1962. dir, index);
  1963. BUG_ON(ret);
  1964. err:
  1965. btrfs_free_path(path);
  1966. if (ret)
  1967. goto out;
  1968. btrfs_i_size_write(dir, dir->i_size - name_len * 2);
  1969. inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  1970. btrfs_update_inode(trans, root, dir);
  1971. btrfs_drop_nlink(inode);
  1972. ret = btrfs_update_inode(trans, root, inode);
  1973. dir->i_sb->s_dirt = 1;
  1974. out:
  1975. return ret;
  1976. }
  1977. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  1978. {
  1979. struct btrfs_root *root;
  1980. struct btrfs_trans_handle *trans;
  1981. struct inode *inode = dentry->d_inode;
  1982. int ret;
  1983. unsigned long nr = 0;
  1984. root = BTRFS_I(dir)->root;
  1985. trans = btrfs_start_transaction(root, 1);
  1986. btrfs_set_trans_block_group(trans, dir);
  1987. ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  1988. dentry->d_name.name, dentry->d_name.len);
  1989. if (inode->i_nlink == 0)
  1990. ret = btrfs_orphan_add(trans, inode);
  1991. nr = trans->blocks_used;
  1992. btrfs_end_transaction_throttle(trans, root);
  1993. btrfs_btree_balance_dirty(root, nr);
  1994. return ret;
  1995. }
  1996. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  1997. {
  1998. struct inode *inode = dentry->d_inode;
  1999. int err = 0;
  2000. int ret;
  2001. struct btrfs_root *root = BTRFS_I(dir)->root;
  2002. struct btrfs_trans_handle *trans;
  2003. unsigned long nr = 0;
  2004. /*
  2005. * the FIRST_FREE_OBJECTID check makes sure we don't try to rmdir
  2006. * the root of a subvolume or snapshot
  2007. */
  2008. if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
  2009. inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  2010. return -ENOTEMPTY;
  2011. }
  2012. trans = btrfs_start_transaction(root, 1);
  2013. btrfs_set_trans_block_group(trans, dir);
  2014. err = btrfs_orphan_add(trans, inode);
  2015. if (err)
  2016. goto fail_trans;
  2017. /* now the directory is empty */
  2018. err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
  2019. dentry->d_name.name, dentry->d_name.len);
  2020. if (!err)
  2021. btrfs_i_size_write(inode, 0);
  2022. fail_trans:
  2023. nr = trans->blocks_used;
  2024. ret = btrfs_end_transaction_throttle(trans, root);
  2025. btrfs_btree_balance_dirty(root, nr);
  2026. if (ret && !err)
  2027. err = ret;
  2028. return err;
  2029. }
  2030. #if 0
  2031. /*
  2032. * when truncating bytes in a file, it is possible to avoid reading
  2033. * the leaves that contain only checksum items. This can be the
  2034. * majority of the IO required to delete a large file, but it must
  2035. * be done carefully.
  2036. *
  2037. * The keys in the level just above the leaves are checked to make sure
  2038. * the lowest key in a given leaf is a csum key, and starts at an offset
  2039. * after the new size.
  2040. *
  2041. * Then the key for the next leaf is checked to make sure it also has
  2042. * a checksum item for the same file. If it does, we know our target leaf
  2043. * contains only checksum items, and it can be safely freed without reading
  2044. * it.
  2045. *
  2046. * This is just an optimization targeted at large files. It may do
  2047. * nothing. It will return 0 unless things went badly.
  2048. */
  2049. static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
  2050. struct btrfs_root *root,
  2051. struct btrfs_path *path,
  2052. struct inode *inode, u64 new_size)
  2053. {
  2054. struct btrfs_key key;
  2055. int ret;
  2056. int nritems;
  2057. struct btrfs_key found_key;
  2058. struct btrfs_key other_key;
  2059. struct btrfs_leaf_ref *ref;
  2060. u64 leaf_gen;
  2061. u64 leaf_start;
  2062. path->lowest_level = 1;
  2063. key.objectid = inode->i_ino;
  2064. key.type = BTRFS_CSUM_ITEM_KEY;
  2065. key.offset = new_size;
  2066. again:
  2067. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2068. if (ret < 0)
  2069. goto out;
  2070. if (path->nodes[1] == NULL) {
  2071. ret = 0;
  2072. goto out;
  2073. }
  2074. ret = 0;
  2075. btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
  2076. nritems = btrfs_header_nritems(path->nodes[1]);
  2077. if (!nritems)
  2078. goto out;
  2079. if (path->slots[1] >= nritems)
  2080. goto next_node;
  2081. /* did we find a key greater than anything we want to delete? */
  2082. if (found_key.objectid > inode->i_ino ||
  2083. (found_key.objectid == inode->i_ino && found_key.type > key.type))
  2084. goto out;
  2085. /* we check the next key in the node to make sure the leave contains
  2086. * only checksum items. This comparison doesn't work if our
  2087. * leaf is the last one in the node
  2088. */
  2089. if (path->slots[1] + 1 >= nritems) {
  2090. next_node:
  2091. /* search forward from the last key in the node, this
  2092. * will bring us into the next node in the tree
  2093. */
  2094. btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
  2095. /* unlikely, but we inc below, so check to be safe */
  2096. if (found_key.offset == (u64)-1)
  2097. goto out;
  2098. /* search_forward needs a path with locks held, do the
  2099. * search again for the original key. It is possible
  2100. * this will race with a balance and return a path that
  2101. * we could modify, but this drop is just an optimization
  2102. * and is allowed to miss some leaves.
  2103. */
  2104. btrfs_release_path(root, path);
  2105. found_key.offset++;
  2106. /* setup a max key for search_forward */
  2107. other_key.offset = (u64)-1;
  2108. other_key.type = key.type;
  2109. other_key.objectid = key.objectid;
  2110. path->keep_locks = 1;
  2111. ret = btrfs_search_forward(root, &found_key, &other_key,
  2112. path, 0, 0);
  2113. path->keep_locks = 0;
  2114. if (ret || found_key.objectid != key.objectid ||
  2115. found_key.type != key.type) {
  2116. ret = 0;
  2117. goto out;
  2118. }
  2119. key.offset = found_key.offset;
  2120. btrfs_release_path(root, path);
  2121. cond_resched();
  2122. goto again;
  2123. }
  2124. /* we know there's one more slot after us in the tree,
  2125. * read that key so we can verify it is also a checksum item
  2126. */
  2127. btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
  2128. if (found_key.objectid < inode->i_ino)
  2129. goto next_key;
  2130. if (found_key.type != key.type || found_key.offset < new_size)
  2131. goto next_key;
  2132. /*
  2133. * if the key for the next leaf isn't a csum key from this objectid,
  2134. * we can't be sure there aren't good items inside this leaf.
  2135. * Bail out
  2136. */
  2137. if (other_key.objectid != inode->i_ino || other_key.type != key.type)
  2138. goto out;
  2139. leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
  2140. leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
  2141. /*
  2142. * it is safe to delete this leaf, it contains only
  2143. * csum items from this inode at an offset >= new_size
  2144. */
  2145. ret = btrfs_del_leaf(trans, root, path, leaf_start);
  2146. BUG_ON(ret);
  2147. if (root->ref_cows && leaf_gen < trans->transid) {
  2148. ref = btrfs_alloc_leaf_ref(root, 0);
  2149. if (ref) {
  2150. ref->root_gen = root->root_key.offset;
  2151. ref->bytenr = leaf_start;
  2152. ref->owner = 0;
  2153. ref->generation = leaf_gen;
  2154. ref->nritems = 0;
  2155. btrfs_sort_leaf_ref(ref);
  2156. ret = btrfs_add_leaf_ref(root, ref, 0);
  2157. WARN_ON(ret);
  2158. btrfs_free_leaf_ref(root, ref);
  2159. } else {
  2160. WARN_ON(1);
  2161. }
  2162. }
  2163. next_key:
  2164. btrfs_release_path(root, path);
  2165. if (other_key.objectid == inode->i_ino &&
  2166. other_key.type == key.type && other_key.offset > key.offset) {
  2167. key.offset = other_key.offset;
  2168. cond_resched();
  2169. goto again;
  2170. }
  2171. ret = 0;
  2172. out:
  2173. /* fixup any changes we've made to the path */
  2174. path->lowest_level = 0;
  2175. path->keep_locks = 0;
  2176. btrfs_release_path(root, path);
  2177. return ret;
  2178. }
  2179. #endif
  2180. /*
  2181. * this can truncate away extent items, csum items and directory items.
  2182. * It starts at a high offset and removes keys until it can't find
  2183. * any higher than new_size
  2184. *
  2185. * csum items that cross the new i_size are truncated to the new size
  2186. * as well.
  2187. *
  2188. * min_type is the minimum key type to truncate down to. If set to 0, this
  2189. * will kill all the items on this inode, including the INODE_ITEM_KEY.
  2190. */
  2191. noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2192. struct btrfs_root *root,
  2193. struct inode *inode,
  2194. u64 new_size, u32 min_type)
  2195. {
  2196. int ret;
  2197. struct btrfs_path *path;
  2198. struct btrfs_key key;
  2199. struct btrfs_key found_key;
  2200. u32 found_type = (u8)-1;
  2201. struct extent_buffer *leaf;
  2202. struct btrfs_file_extent_item *fi;
  2203. u64 extent_start = 0;
  2204. u64 extent_num_bytes = 0;
  2205. u64 item_end = 0;
  2206. u64 root_gen = 0;
  2207. u64 root_owner = 0;
  2208. int found_extent;
  2209. int del_item;
  2210. int pending_del_nr = 0;
  2211. int pending_del_slot = 0;
  2212. int extent_type = -1;
  2213. int encoding;
  2214. u64 mask = root->sectorsize - 1;
  2215. if (root->ref_cows)
  2216. btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
  2217. path = btrfs_alloc_path();
  2218. path->reada = -1;
  2219. BUG_ON(!path);
  2220. /* FIXME, add redo link to tree so we don't leak on crash */
  2221. key.objectid = inode->i_ino;
  2222. key.offset = (u64)-1;
  2223. key.type = (u8)-1;
  2224. search_again:
  2225. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  2226. if (ret < 0)
  2227. goto error;
  2228. if (ret > 0) {
  2229. /* there are no items in the tree for us to truncate, we're
  2230. * done
  2231. */
  2232. if (path->slots[0] == 0) {
  2233. ret = 0;
  2234. goto error;
  2235. }
  2236. path->slots[0]--;
  2237. }
  2238. while (1) {
  2239. fi = NULL;
  2240. leaf = path->nodes[0];
  2241. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2242. found_type = btrfs_key_type(&found_key);
  2243. encoding = 0;
  2244. if (found_key.objectid != inode->i_ino)
  2245. break;
  2246. if (found_type < min_type)
  2247. break;
  2248. item_end = found_key.offset;
  2249. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  2250. fi = btrfs_item_ptr(leaf, path->slots[0],
  2251. struct btrfs_file_extent_item);
  2252. extent_type = btrfs_file_extent_type(leaf, fi);
  2253. encoding = btrfs_file_extent_compression(leaf, fi);
  2254. encoding |= btrfs_file_extent_encryption(leaf, fi);
  2255. encoding |= btrfs_file_extent_other_encoding(leaf, fi);
  2256. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2257. item_end +=
  2258. btrfs_file_extent_num_bytes(leaf, fi);
  2259. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2260. item_end += btrfs_file_extent_inline_len(leaf,
  2261. fi);
  2262. }
  2263. item_end--;
  2264. }
  2265. if (item_end < new_size) {
  2266. if (found_type == BTRFS_DIR_ITEM_KEY)
  2267. found_type = BTRFS_INODE_ITEM_KEY;
  2268. else if (found_type == BTRFS_EXTENT_ITEM_KEY)
  2269. found_type = BTRFS_EXTENT_DATA_KEY;
  2270. else if (found_type == BTRFS_EXTENT_DATA_KEY)
  2271. found_type = BTRFS_XATTR_ITEM_KEY;
  2272. else if (found_type == BTRFS_XATTR_ITEM_KEY)
  2273. found_type = BTRFS_INODE_REF_KEY;
  2274. else if (found_type)
  2275. found_type--;
  2276. else
  2277. break;
  2278. btrfs_set_key_type(&key, found_type);
  2279. goto next;
  2280. }
  2281. if (found_key.offset >= new_size)
  2282. del_item = 1;
  2283. else
  2284. del_item = 0;
  2285. found_extent = 0;
  2286. /* FIXME, shrink the extent if the ref count is only 1 */
  2287. if (found_type != BTRFS_EXTENT_DATA_KEY)
  2288. goto delete;
  2289. if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
  2290. u64 num_dec;
  2291. extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
  2292. if (!del_item && !encoding) {
  2293. u64 orig_num_bytes =
  2294. btrfs_file_extent_num_bytes(leaf, fi);
  2295. extent_num_bytes = new_size -
  2296. found_key.offset + root->sectorsize - 1;
  2297. extent_num_bytes = extent_num_bytes &
  2298. ~((u64)root->sectorsize - 1);
  2299. btrfs_set_file_extent_num_bytes(leaf, fi,
  2300. extent_num_bytes);
  2301. num_dec = (orig_num_bytes -
  2302. extent_num_bytes);
  2303. if (root->ref_cows && extent_start != 0)
  2304. inode_sub_bytes(inode, num_dec);
  2305. btrfs_mark_buffer_dirty(leaf);
  2306. } else {
  2307. extent_num_bytes =
  2308. btrfs_file_extent_disk_num_bytes(leaf,
  2309. fi);
  2310. /* FIXME blocksize != 4096 */
  2311. num_dec = btrfs_file_extent_num_bytes(leaf, fi);
  2312. if (extent_start != 0) {
  2313. found_extent = 1;
  2314. if (root->ref_cows)
  2315. inode_sub_bytes(inode, num_dec);
  2316. }
  2317. root_gen = btrfs_header_generation(leaf);
  2318. root_owner = btrfs_header_owner(leaf);
  2319. }
  2320. } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
  2321. /*
  2322. * we can't truncate inline items that have had
  2323. * special encodings
  2324. */
  2325. if (!del_item &&
  2326. btrfs_file_extent_compression(leaf, fi) == 0 &&
  2327. btrfs_file_extent_encryption(leaf, fi) == 0 &&
  2328. btrfs_file_extent_other_encoding(leaf, fi) == 0) {
  2329. u32 size = new_size - found_key.offset;
  2330. if (root->ref_cows) {
  2331. inode_sub_bytes(inode, item_end + 1 -
  2332. new_size);
  2333. }
  2334. size =
  2335. btrfs_file_extent_calc_inline_size(size);
  2336. ret = btrfs_truncate_item(trans, root, path,
  2337. size, 1);
  2338. BUG_ON(ret);
  2339. } else if (root->ref_cows) {
  2340. inode_sub_bytes(inode, item_end + 1 -
  2341. found_key.offset);
  2342. }
  2343. }
  2344. delete:
  2345. if (del_item) {
  2346. if (!pending_del_nr) {
  2347. /* no pending yet, add ourselves */
  2348. pending_del_slot = path->slots[0];
  2349. pending_del_nr = 1;
  2350. } else if (pending_del_nr &&
  2351. path->slots[0] + 1 == pending_del_slot) {
  2352. /* hop on the pending chunk */
  2353. pending_del_nr++;
  2354. pending_del_slot = path->slots[0];
  2355. } else {
  2356. BUG();
  2357. }
  2358. } else {
  2359. break;
  2360. }
  2361. if (found_extent) {
  2362. ret = btrfs_free_extent(trans, root, extent_start,
  2363. extent_num_bytes,
  2364. leaf->start, root_owner,
  2365. root_gen, inode->i_ino, 0);
  2366. BUG_ON(ret);
  2367. }
  2368. next:
  2369. if (path->slots[0] == 0) {
  2370. if (pending_del_nr)
  2371. goto del_pending;
  2372. btrfs_release_path(root, path);
  2373. if (found_type == BTRFS_INODE_ITEM_KEY)
  2374. break;
  2375. goto search_again;
  2376. }
  2377. path->slots[0]--;
  2378. if (pending_del_nr &&
  2379. path->slots[0] + 1 != pending_del_slot) {
  2380. struct btrfs_key debug;
  2381. del_pending:
  2382. btrfs_item_key_to_cpu(path->nodes[0], &debug,
  2383. pending_del_slot);
  2384. ret = btrfs_del_items(trans, root, path,
  2385. pending_del_slot,
  2386. pending_del_nr);
  2387. BUG_ON(ret);
  2388. pending_del_nr = 0;
  2389. btrfs_release_path(root, path);
  2390. if (found_type == BTRFS_INODE_ITEM_KEY)
  2391. break;
  2392. goto search_again;
  2393. }
  2394. }
  2395. ret = 0;
  2396. error:
  2397. if (pending_del_nr) {
  2398. ret = btrfs_del_items(trans, root, path, pending_del_slot,
  2399. pending_del_nr);
  2400. }
  2401. btrfs_free_path(path);
  2402. inode->i_sb->s_dirt = 1;
  2403. return ret;
  2404. }
  2405. /*
  2406. * taken from block_truncate_page, but does cow as it zeros out
  2407. * any bytes left in the last page in the file.
  2408. */
  2409. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  2410. {
  2411. struct inode *inode = mapping->host;
  2412. struct btrfs_root *root = BTRFS_I(inode)->root;
  2413. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2414. struct btrfs_ordered_extent *ordered;
  2415. char *kaddr;
  2416. u32 blocksize = root->sectorsize;
  2417. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  2418. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  2419. struct page *page;
  2420. int ret = 0;
  2421. u64 page_start;
  2422. u64 page_end;
  2423. if ((offset & (blocksize - 1)) == 0)
  2424. goto out;
  2425. ret = -ENOMEM;
  2426. again:
  2427. page = grab_cache_page(mapping, index);
  2428. if (!page)
  2429. goto out;
  2430. page_start = page_offset(page);
  2431. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2432. if (!PageUptodate(page)) {
  2433. ret = btrfs_readpage(NULL, page);
  2434. lock_page(page);
  2435. if (page->mapping != mapping) {
  2436. unlock_page(page);
  2437. page_cache_release(page);
  2438. goto again;
  2439. }
  2440. if (!PageUptodate(page)) {
  2441. ret = -EIO;
  2442. goto out_unlock;
  2443. }
  2444. }
  2445. wait_on_page_writeback(page);
  2446. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2447. set_page_extent_mapped(page);
  2448. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2449. if (ordered) {
  2450. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2451. unlock_page(page);
  2452. page_cache_release(page);
  2453. btrfs_start_ordered_extent(inode, ordered, 1);
  2454. btrfs_put_ordered_extent(ordered);
  2455. goto again;
  2456. }
  2457. btrfs_set_extent_delalloc(inode, page_start, page_end);
  2458. ret = 0;
  2459. if (offset != PAGE_CACHE_SIZE) {
  2460. kaddr = kmap(page);
  2461. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  2462. flush_dcache_page(page);
  2463. kunmap(page);
  2464. }
  2465. ClearPageChecked(page);
  2466. set_page_dirty(page);
  2467. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2468. out_unlock:
  2469. unlock_page(page);
  2470. page_cache_release(page);
  2471. out:
  2472. return ret;
  2473. }
  2474. int btrfs_cont_expand(struct inode *inode, loff_t size)
  2475. {
  2476. struct btrfs_trans_handle *trans;
  2477. struct btrfs_root *root = BTRFS_I(inode)->root;
  2478. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2479. struct extent_map *em;
  2480. u64 mask = root->sectorsize - 1;
  2481. u64 hole_start = (inode->i_size + mask) & ~mask;
  2482. u64 block_end = (size + mask) & ~mask;
  2483. u64 last_byte;
  2484. u64 cur_offset;
  2485. u64 hole_size;
  2486. int err;
  2487. if (size <= hole_start)
  2488. return 0;
  2489. err = btrfs_check_metadata_free_space(root);
  2490. if (err)
  2491. return err;
  2492. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  2493. while (1) {
  2494. struct btrfs_ordered_extent *ordered;
  2495. btrfs_wait_ordered_range(inode, hole_start,
  2496. block_end - hole_start);
  2497. lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2498. ordered = btrfs_lookup_ordered_extent(inode, hole_start);
  2499. if (!ordered)
  2500. break;
  2501. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2502. btrfs_put_ordered_extent(ordered);
  2503. }
  2504. trans = btrfs_start_transaction(root, 1);
  2505. btrfs_set_trans_block_group(trans, inode);
  2506. cur_offset = hole_start;
  2507. while (1) {
  2508. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  2509. block_end - cur_offset, 0);
  2510. BUG_ON(IS_ERR(em) || !em);
  2511. last_byte = min(extent_map_end(em), block_end);
  2512. last_byte = (last_byte + mask) & ~mask;
  2513. if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
  2514. u64 hint_byte = 0;
  2515. hole_size = last_byte - cur_offset;
  2516. err = btrfs_drop_extents(trans, root, inode,
  2517. cur_offset,
  2518. cur_offset + hole_size,
  2519. cur_offset, &hint_byte);
  2520. if (err)
  2521. break;
  2522. err = btrfs_insert_file_extent(trans, root,
  2523. inode->i_ino, cur_offset, 0,
  2524. 0, hole_size, 0, hole_size,
  2525. 0, 0, 0);
  2526. btrfs_drop_extent_cache(inode, hole_start,
  2527. last_byte - 1, 0);
  2528. }
  2529. free_extent_map(em);
  2530. cur_offset = last_byte;
  2531. if (err || cur_offset >= block_end)
  2532. break;
  2533. }
  2534. btrfs_end_transaction(trans, root);
  2535. unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
  2536. return err;
  2537. }
  2538. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  2539. {
  2540. struct inode *inode = dentry->d_inode;
  2541. int err;
  2542. err = inode_change_ok(inode, attr);
  2543. if (err)
  2544. return err;
  2545. if (S_ISREG(inode->i_mode) &&
  2546. attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
  2547. err = btrfs_cont_expand(inode, attr->ia_size);
  2548. if (err)
  2549. return err;
  2550. }
  2551. err = inode_setattr(inode, attr);
  2552. if (!err && ((attr->ia_valid & ATTR_MODE)))
  2553. err = btrfs_acl_chmod(inode);
  2554. return err;
  2555. }
  2556. void btrfs_delete_inode(struct inode *inode)
  2557. {
  2558. struct btrfs_trans_handle *trans;
  2559. struct btrfs_root *root = BTRFS_I(inode)->root;
  2560. unsigned long nr;
  2561. int ret;
  2562. truncate_inode_pages(&inode->i_data, 0);
  2563. if (is_bad_inode(inode)) {
  2564. btrfs_orphan_del(NULL, inode);
  2565. goto no_delete;
  2566. }
  2567. btrfs_wait_ordered_range(inode, 0, (u64)-1);
  2568. btrfs_i_size_write(inode, 0);
  2569. trans = btrfs_join_transaction(root, 1);
  2570. btrfs_set_trans_block_group(trans, inode);
  2571. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
  2572. if (ret) {
  2573. btrfs_orphan_del(NULL, inode);
  2574. goto no_delete_lock;
  2575. }
  2576. btrfs_orphan_del(trans, inode);
  2577. nr = trans->blocks_used;
  2578. clear_inode(inode);
  2579. btrfs_end_transaction(trans, root);
  2580. btrfs_btree_balance_dirty(root, nr);
  2581. return;
  2582. no_delete_lock:
  2583. nr = trans->blocks_used;
  2584. btrfs_end_transaction(trans, root);
  2585. btrfs_btree_balance_dirty(root, nr);
  2586. no_delete:
  2587. clear_inode(inode);
  2588. }
  2589. /*
  2590. * this returns the key found in the dir entry in the location pointer.
  2591. * If no dir entries were found, location->objectid is 0.
  2592. */
  2593. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  2594. struct btrfs_key *location)
  2595. {
  2596. const char *name = dentry->d_name.name;
  2597. int namelen = dentry->d_name.len;
  2598. struct btrfs_dir_item *di;
  2599. struct btrfs_path *path;
  2600. struct btrfs_root *root = BTRFS_I(dir)->root;
  2601. int ret = 0;
  2602. path = btrfs_alloc_path();
  2603. BUG_ON(!path);
  2604. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  2605. namelen, 0);
  2606. if (IS_ERR(di))
  2607. ret = PTR_ERR(di);
  2608. if (!di || IS_ERR(di))
  2609. goto out_err;
  2610. btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
  2611. out:
  2612. btrfs_free_path(path);
  2613. return ret;
  2614. out_err:
  2615. location->objectid = 0;
  2616. goto out;
  2617. }
  2618. /*
  2619. * when we hit a tree root in a directory, the btrfs part of the inode
  2620. * needs to be changed to reflect the root directory of the tree root. This
  2621. * is kind of like crossing a mount point.
  2622. */
  2623. static int fixup_tree_root_location(struct btrfs_root *root,
  2624. struct btrfs_key *location,
  2625. struct btrfs_root **sub_root,
  2626. struct dentry *dentry)
  2627. {
  2628. struct btrfs_root_item *ri;
  2629. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  2630. return 0;
  2631. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  2632. return 0;
  2633. *sub_root = btrfs_read_fs_root(root->fs_info, location,
  2634. dentry->d_name.name,
  2635. dentry->d_name.len);
  2636. if (IS_ERR(*sub_root))
  2637. return PTR_ERR(*sub_root);
  2638. ri = &(*sub_root)->root_item;
  2639. location->objectid = btrfs_root_dirid(ri);
  2640. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  2641. location->offset = 0;
  2642. return 0;
  2643. }
  2644. static noinline void init_btrfs_i(struct inode *inode)
  2645. {
  2646. struct btrfs_inode *bi = BTRFS_I(inode);
  2647. bi->i_acl = NULL;
  2648. bi->i_default_acl = NULL;
  2649. bi->generation = 0;
  2650. bi->sequence = 0;
  2651. bi->last_trans = 0;
  2652. bi->logged_trans = 0;
  2653. bi->delalloc_bytes = 0;
  2654. bi->reserved_bytes = 0;
  2655. bi->disk_i_size = 0;
  2656. bi->flags = 0;
  2657. bi->index_cnt = (u64)-1;
  2658. bi->log_dirty_trans = 0;
  2659. extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
  2660. extent_io_tree_init(&BTRFS_I(inode)->io_tree,
  2661. inode->i_mapping, GFP_NOFS);
  2662. extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
  2663. inode->i_mapping, GFP_NOFS);
  2664. INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
  2665. btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
  2666. mutex_init(&BTRFS_I(inode)->extent_mutex);
  2667. mutex_init(&BTRFS_I(inode)->log_mutex);
  2668. }
  2669. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  2670. {
  2671. struct btrfs_iget_args *args = p;
  2672. inode->i_ino = args->ino;
  2673. init_btrfs_i(inode);
  2674. BTRFS_I(inode)->root = args->root;
  2675. btrfs_set_inode_space_info(args->root, inode);
  2676. return 0;
  2677. }
  2678. static int btrfs_find_actor(struct inode *inode, void *opaque)
  2679. {
  2680. struct btrfs_iget_args *args = opaque;
  2681. return args->ino == inode->i_ino &&
  2682. args->root == BTRFS_I(inode)->root;
  2683. }
  2684. struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
  2685. struct btrfs_root *root, int wait)
  2686. {
  2687. struct inode *inode;
  2688. struct btrfs_iget_args args;
  2689. args.ino = objectid;
  2690. args.root = root;
  2691. if (wait) {
  2692. inode = ilookup5(s, objectid, btrfs_find_actor,
  2693. (void *)&args);
  2694. } else {
  2695. inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
  2696. (void *)&args);
  2697. }
  2698. return inode;
  2699. }
  2700. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  2701. struct btrfs_root *root)
  2702. {
  2703. struct inode *inode;
  2704. struct btrfs_iget_args args;
  2705. args.ino = objectid;
  2706. args.root = root;
  2707. inode = iget5_locked(s, objectid, btrfs_find_actor,
  2708. btrfs_init_locked_inode,
  2709. (void *)&args);
  2710. return inode;
  2711. }
  2712. /* Get an inode object given its location and corresponding root.
  2713. * Returns in *is_new if the inode was read from disk
  2714. */
  2715. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  2716. struct btrfs_root *root, int *is_new)
  2717. {
  2718. struct inode *inode;
  2719. inode = btrfs_iget_locked(s, location->objectid, root);
  2720. if (!inode)
  2721. return ERR_PTR(-EACCES);
  2722. if (inode->i_state & I_NEW) {
  2723. BTRFS_I(inode)->root = root;
  2724. memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
  2725. btrfs_read_locked_inode(inode);
  2726. unlock_new_inode(inode);
  2727. if (is_new)
  2728. *is_new = 1;
  2729. } else {
  2730. if (is_new)
  2731. *is_new = 0;
  2732. }
  2733. return inode;
  2734. }
  2735. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
  2736. {
  2737. struct inode *inode;
  2738. struct btrfs_inode *bi = BTRFS_I(dir);
  2739. struct btrfs_root *root = bi->root;
  2740. struct btrfs_root *sub_root = root;
  2741. struct btrfs_key location;
  2742. int ret, new;
  2743. if (dentry->d_name.len > BTRFS_NAME_LEN)
  2744. return ERR_PTR(-ENAMETOOLONG);
  2745. ret = btrfs_inode_by_name(dir, dentry, &location);
  2746. if (ret < 0)
  2747. return ERR_PTR(ret);
  2748. inode = NULL;
  2749. if (location.objectid) {
  2750. ret = fixup_tree_root_location(root, &location, &sub_root,
  2751. dentry);
  2752. if (ret < 0)
  2753. return ERR_PTR(ret);
  2754. if (ret > 0)
  2755. return ERR_PTR(-ENOENT);
  2756. inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
  2757. if (IS_ERR(inode))
  2758. return ERR_CAST(inode);
  2759. }
  2760. return inode;
  2761. }
  2762. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  2763. struct nameidata *nd)
  2764. {
  2765. struct inode *inode;
  2766. if (dentry->d_name.len > BTRFS_NAME_LEN)
  2767. return ERR_PTR(-ENAMETOOLONG);
  2768. inode = btrfs_lookup_dentry(dir, dentry);
  2769. if (IS_ERR(inode))
  2770. return ERR_CAST(inode);
  2771. return d_splice_alias(inode, dentry);
  2772. }
  2773. static unsigned char btrfs_filetype_table[] = {
  2774. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  2775. };
  2776. static int btrfs_real_readdir(struct file *filp, void *dirent,
  2777. filldir_t filldir)
  2778. {
  2779. struct inode *inode = filp->f_dentry->d_inode;
  2780. struct btrfs_root *root = BTRFS_I(inode)->root;
  2781. struct btrfs_item *item;
  2782. struct btrfs_dir_item *di;
  2783. struct btrfs_key key;
  2784. struct btrfs_key found_key;
  2785. struct btrfs_path *path;
  2786. int ret;
  2787. u32 nritems;
  2788. struct extent_buffer *leaf;
  2789. int slot;
  2790. int advance;
  2791. unsigned char d_type;
  2792. int over = 0;
  2793. u32 di_cur;
  2794. u32 di_total;
  2795. u32 di_len;
  2796. int key_type = BTRFS_DIR_INDEX_KEY;
  2797. char tmp_name[32];
  2798. char *name_ptr;
  2799. int name_len;
  2800. /* FIXME, use a real flag for deciding about the key type */
  2801. if (root->fs_info->tree_root == root)
  2802. key_type = BTRFS_DIR_ITEM_KEY;
  2803. /* special case for "." */
  2804. if (filp->f_pos == 0) {
  2805. over = filldir(dirent, ".", 1,
  2806. 1, inode->i_ino,
  2807. DT_DIR);
  2808. if (over)
  2809. return 0;
  2810. filp->f_pos = 1;
  2811. }
  2812. /* special case for .., just use the back ref */
  2813. if (filp->f_pos == 1) {
  2814. u64 pino = parent_ino(filp->f_path.dentry);
  2815. over = filldir(dirent, "..", 2,
  2816. 2, pino, DT_DIR);
  2817. if (over)
  2818. return 0;
  2819. filp->f_pos = 2;
  2820. }
  2821. path = btrfs_alloc_path();
  2822. path->reada = 2;
  2823. btrfs_set_key_type(&key, key_type);
  2824. key.offset = filp->f_pos;
  2825. key.objectid = inode->i_ino;
  2826. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2827. if (ret < 0)
  2828. goto err;
  2829. advance = 0;
  2830. while (1) {
  2831. leaf = path->nodes[0];
  2832. nritems = btrfs_header_nritems(leaf);
  2833. slot = path->slots[0];
  2834. if (advance || slot >= nritems) {
  2835. if (slot >= nritems - 1) {
  2836. ret = btrfs_next_leaf(root, path);
  2837. if (ret)
  2838. break;
  2839. leaf = path->nodes[0];
  2840. nritems = btrfs_header_nritems(leaf);
  2841. slot = path->slots[0];
  2842. } else {
  2843. slot++;
  2844. path->slots[0]++;
  2845. }
  2846. }
  2847. advance = 1;
  2848. item = btrfs_item_nr(leaf, slot);
  2849. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2850. if (found_key.objectid != key.objectid)
  2851. break;
  2852. if (btrfs_key_type(&found_key) != key_type)
  2853. break;
  2854. if (found_key.offset < filp->f_pos)
  2855. continue;
  2856. filp->f_pos = found_key.offset;
  2857. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  2858. di_cur = 0;
  2859. di_total = btrfs_item_size(leaf, item);
  2860. while (di_cur < di_total) {
  2861. struct btrfs_key location;
  2862. name_len = btrfs_dir_name_len(leaf, di);
  2863. if (name_len <= sizeof(tmp_name)) {
  2864. name_ptr = tmp_name;
  2865. } else {
  2866. name_ptr = kmalloc(name_len, GFP_NOFS);
  2867. if (!name_ptr) {
  2868. ret = -ENOMEM;
  2869. goto err;
  2870. }
  2871. }
  2872. read_extent_buffer(leaf, name_ptr,
  2873. (unsigned long)(di + 1), name_len);
  2874. d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
  2875. btrfs_dir_item_key_to_cpu(leaf, di, &location);
  2876. /* is this a reference to our own snapshot? If so
  2877. * skip it
  2878. */
  2879. if (location.type == BTRFS_ROOT_ITEM_KEY &&
  2880. location.objectid == root->root_key.objectid) {
  2881. over = 0;
  2882. goto skip;
  2883. }
  2884. over = filldir(dirent, name_ptr, name_len,
  2885. found_key.offset, location.objectid,
  2886. d_type);
  2887. skip:
  2888. if (name_ptr != tmp_name)
  2889. kfree(name_ptr);
  2890. if (over)
  2891. goto nopos;
  2892. di_len = btrfs_dir_name_len(leaf, di) +
  2893. btrfs_dir_data_len(leaf, di) + sizeof(*di);
  2894. di_cur += di_len;
  2895. di = (struct btrfs_dir_item *)((char *)di + di_len);
  2896. }
  2897. }
  2898. /* Reached end of directory/root. Bump pos past the last item. */
  2899. if (key_type == BTRFS_DIR_INDEX_KEY)
  2900. filp->f_pos = INT_LIMIT(off_t);
  2901. else
  2902. filp->f_pos++;
  2903. nopos:
  2904. ret = 0;
  2905. err:
  2906. btrfs_free_path(path);
  2907. return ret;
  2908. }
  2909. int btrfs_write_inode(struct inode *inode, int wait)
  2910. {
  2911. struct btrfs_root *root = BTRFS_I(inode)->root;
  2912. struct btrfs_trans_handle *trans;
  2913. int ret = 0;
  2914. if (root->fs_info->btree_inode == inode)
  2915. return 0;
  2916. if (wait) {
  2917. trans = btrfs_join_transaction(root, 1);
  2918. btrfs_set_trans_block_group(trans, inode);
  2919. ret = btrfs_commit_transaction(trans, root);
  2920. }
  2921. return ret;
  2922. }
  2923. /*
  2924. * This is somewhat expensive, updating the tree every time the
  2925. * inode changes. But, it is most likely to find the inode in cache.
  2926. * FIXME, needs more benchmarking...there are no reasons other than performance
  2927. * to keep or drop this code.
  2928. */
  2929. void btrfs_dirty_inode(struct inode *inode)
  2930. {
  2931. struct btrfs_root *root = BTRFS_I(inode)->root;
  2932. struct btrfs_trans_handle *trans;
  2933. trans = btrfs_join_transaction(root, 1);
  2934. btrfs_set_trans_block_group(trans, inode);
  2935. btrfs_update_inode(trans, root, inode);
  2936. btrfs_end_transaction(trans, root);
  2937. }
  2938. /*
  2939. * find the highest existing sequence number in a directory
  2940. * and then set the in-memory index_cnt variable to reflect
  2941. * free sequence numbers
  2942. */
  2943. static int btrfs_set_inode_index_count(struct inode *inode)
  2944. {
  2945. struct btrfs_root *root = BTRFS_I(inode)->root;
  2946. struct btrfs_key key, found_key;
  2947. struct btrfs_path *path;
  2948. struct extent_buffer *leaf;
  2949. int ret;
  2950. key.objectid = inode->i_ino;
  2951. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  2952. key.offset = (u64)-1;
  2953. path = btrfs_alloc_path();
  2954. if (!path)
  2955. return -ENOMEM;
  2956. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2957. if (ret < 0)
  2958. goto out;
  2959. /* FIXME: we should be able to handle this */
  2960. if (ret == 0)
  2961. goto out;
  2962. ret = 0;
  2963. /*
  2964. * MAGIC NUMBER EXPLANATION:
  2965. * since we search a directory based on f_pos we have to start at 2
  2966. * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
  2967. * else has to start at 2
  2968. */
  2969. if (path->slots[0] == 0) {
  2970. BTRFS_I(inode)->index_cnt = 2;
  2971. goto out;
  2972. }
  2973. path->slots[0]--;
  2974. leaf = path->nodes[0];
  2975. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2976. if (found_key.objectid != inode->i_ino ||
  2977. btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
  2978. BTRFS_I(inode)->index_cnt = 2;
  2979. goto out;
  2980. }
  2981. BTRFS_I(inode)->index_cnt = found_key.offset + 1;
  2982. out:
  2983. btrfs_free_path(path);
  2984. return ret;
  2985. }
  2986. /*
  2987. * helper to find a free sequence number in a given directory. This current
  2988. * code is very simple, later versions will do smarter things in the btree
  2989. */
  2990. int btrfs_set_inode_index(struct inode *dir, u64 *index)
  2991. {
  2992. int ret = 0;
  2993. if (BTRFS_I(dir)->index_cnt == (u64)-1) {
  2994. ret = btrfs_set_inode_index_count(dir);
  2995. if (ret)
  2996. return ret;
  2997. }
  2998. *index = BTRFS_I(dir)->index_cnt;
  2999. BTRFS_I(dir)->index_cnt++;
  3000. return ret;
  3001. }
  3002. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  3003. struct btrfs_root *root,
  3004. struct inode *dir,
  3005. const char *name, int name_len,
  3006. u64 ref_objectid, u64 objectid,
  3007. u64 alloc_hint, int mode, u64 *index)
  3008. {
  3009. struct inode *inode;
  3010. struct btrfs_inode_item *inode_item;
  3011. struct btrfs_key *location;
  3012. struct btrfs_path *path;
  3013. struct btrfs_inode_ref *ref;
  3014. struct btrfs_key key[2];
  3015. u32 sizes[2];
  3016. unsigned long ptr;
  3017. int ret;
  3018. int owner;
  3019. path = btrfs_alloc_path();
  3020. BUG_ON(!path);
  3021. inode = new_inode(root->fs_info->sb);
  3022. if (!inode)
  3023. return ERR_PTR(-ENOMEM);
  3024. if (dir) {
  3025. ret = btrfs_set_inode_index(dir, index);
  3026. if (ret)
  3027. return ERR_PTR(ret);
  3028. }
  3029. /*
  3030. * index_cnt is ignored for everything but a dir,
  3031. * btrfs_get_inode_index_count has an explanation for the magic
  3032. * number
  3033. */
  3034. init_btrfs_i(inode);
  3035. BTRFS_I(inode)->index_cnt = 2;
  3036. BTRFS_I(inode)->root = root;
  3037. BTRFS_I(inode)->generation = trans->transid;
  3038. btrfs_set_inode_space_info(root, inode);
  3039. if (mode & S_IFDIR)
  3040. owner = 0;
  3041. else
  3042. owner = 1;
  3043. BTRFS_I(inode)->block_group =
  3044. btrfs_find_block_group(root, 0, alloc_hint, owner);
  3045. if ((mode & S_IFREG)) {
  3046. if (btrfs_test_opt(root, NODATASUM))
  3047. btrfs_set_flag(inode, NODATASUM);
  3048. if (btrfs_test_opt(root, NODATACOW))
  3049. btrfs_set_flag(inode, NODATACOW);
  3050. }
  3051. key[0].objectid = objectid;
  3052. btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
  3053. key[0].offset = 0;
  3054. key[1].objectid = objectid;
  3055. btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
  3056. key[1].offset = ref_objectid;
  3057. sizes[0] = sizeof(struct btrfs_inode_item);
  3058. sizes[1] = name_len + sizeof(*ref);
  3059. ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
  3060. if (ret != 0)
  3061. goto fail;
  3062. if (objectid > root->highest_inode)
  3063. root->highest_inode = objectid;
  3064. inode->i_uid = current_fsuid();
  3065. if (dir && (dir->i_mode & S_ISGID)) {
  3066. inode->i_gid = dir->i_gid;
  3067. if (S_ISDIR(mode))
  3068. mode |= S_ISGID;
  3069. } else
  3070. inode->i_gid = current_fsgid();
  3071. inode->i_mode = mode;
  3072. inode->i_ino = objectid;
  3073. inode_set_bytes(inode, 0);
  3074. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  3075. inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  3076. struct btrfs_inode_item);
  3077. fill_inode_item(trans, path->nodes[0], inode_item, inode);
  3078. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  3079. struct btrfs_inode_ref);
  3080. btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
  3081. btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
  3082. ptr = (unsigned long)(ref + 1);
  3083. write_extent_buffer(path->nodes[0], name, ptr, name_len);
  3084. btrfs_mark_buffer_dirty(path->nodes[0]);
  3085. btrfs_free_path(path);
  3086. location = &BTRFS_I(inode)->location;
  3087. location->objectid = objectid;
  3088. location->offset = 0;
  3089. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  3090. insert_inode_hash(inode);
  3091. return inode;
  3092. fail:
  3093. if (dir)
  3094. BTRFS_I(dir)->index_cnt--;
  3095. btrfs_free_path(path);
  3096. return ERR_PTR(ret);
  3097. }
  3098. static inline u8 btrfs_inode_type(struct inode *inode)
  3099. {
  3100. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  3101. }
  3102. /*
  3103. * utility function to add 'inode' into 'parent_inode' with
  3104. * a give name and a given sequence number.
  3105. * if 'add_backref' is true, also insert a backref from the
  3106. * inode to the parent directory.
  3107. */
  3108. int btrfs_add_link(struct btrfs_trans_handle *trans,
  3109. struct inode *parent_inode, struct inode *inode,
  3110. const char *name, int name_len, int add_backref, u64 index)
  3111. {
  3112. int ret;
  3113. struct btrfs_key key;
  3114. struct btrfs_root *root = BTRFS_I(parent_inode)->root;
  3115. key.objectid = inode->i_ino;
  3116. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  3117. key.offset = 0;
  3118. ret = btrfs_insert_dir_item(trans, root, name, name_len,
  3119. parent_inode->i_ino,
  3120. &key, btrfs_inode_type(inode),
  3121. index);
  3122. if (ret == 0) {
  3123. if (add_backref) {
  3124. ret = btrfs_insert_inode_ref(trans, root,
  3125. name, name_len,
  3126. inode->i_ino,
  3127. parent_inode->i_ino,
  3128. index);
  3129. }
  3130. btrfs_i_size_write(parent_inode, parent_inode->i_size +
  3131. name_len * 2);
  3132. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  3133. ret = btrfs_update_inode(trans, root, parent_inode);
  3134. }
  3135. return ret;
  3136. }
  3137. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  3138. struct dentry *dentry, struct inode *inode,
  3139. int backref, u64 index)
  3140. {
  3141. int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3142. inode, dentry->d_name.name,
  3143. dentry->d_name.len, backref, index);
  3144. if (!err) {
  3145. d_instantiate(dentry, inode);
  3146. return 0;
  3147. }
  3148. if (err > 0)
  3149. err = -EEXIST;
  3150. return err;
  3151. }
  3152. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  3153. int mode, dev_t rdev)
  3154. {
  3155. struct btrfs_trans_handle *trans;
  3156. struct btrfs_root *root = BTRFS_I(dir)->root;
  3157. struct inode *inode = NULL;
  3158. int err;
  3159. int drop_inode = 0;
  3160. u64 objectid;
  3161. unsigned long nr = 0;
  3162. u64 index = 0;
  3163. if (!new_valid_dev(rdev))
  3164. return -EINVAL;
  3165. err = btrfs_check_metadata_free_space(root);
  3166. if (err)
  3167. goto fail;
  3168. trans = btrfs_start_transaction(root, 1);
  3169. btrfs_set_trans_block_group(trans, dir);
  3170. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3171. if (err) {
  3172. err = -ENOSPC;
  3173. goto out_unlock;
  3174. }
  3175. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3176. dentry->d_name.len,
  3177. dentry->d_parent->d_inode->i_ino, objectid,
  3178. BTRFS_I(dir)->block_group, mode, &index);
  3179. err = PTR_ERR(inode);
  3180. if (IS_ERR(inode))
  3181. goto out_unlock;
  3182. err = btrfs_init_inode_security(inode, dir);
  3183. if (err) {
  3184. drop_inode = 1;
  3185. goto out_unlock;
  3186. }
  3187. btrfs_set_trans_block_group(trans, inode);
  3188. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3189. if (err)
  3190. drop_inode = 1;
  3191. else {
  3192. inode->i_op = &btrfs_special_inode_operations;
  3193. init_special_inode(inode, inode->i_mode, rdev);
  3194. btrfs_update_inode(trans, root, inode);
  3195. }
  3196. dir->i_sb->s_dirt = 1;
  3197. btrfs_update_inode_block_group(trans, inode);
  3198. btrfs_update_inode_block_group(trans, dir);
  3199. out_unlock:
  3200. nr = trans->blocks_used;
  3201. btrfs_end_transaction_throttle(trans, root);
  3202. fail:
  3203. if (drop_inode) {
  3204. inode_dec_link_count(inode);
  3205. iput(inode);
  3206. }
  3207. btrfs_btree_balance_dirty(root, nr);
  3208. return err;
  3209. }
  3210. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  3211. int mode, struct nameidata *nd)
  3212. {
  3213. struct btrfs_trans_handle *trans;
  3214. struct btrfs_root *root = BTRFS_I(dir)->root;
  3215. struct inode *inode = NULL;
  3216. int err;
  3217. int drop_inode = 0;
  3218. unsigned long nr = 0;
  3219. u64 objectid;
  3220. u64 index = 0;
  3221. err = btrfs_check_metadata_free_space(root);
  3222. if (err)
  3223. goto fail;
  3224. trans = btrfs_start_transaction(root, 1);
  3225. btrfs_set_trans_block_group(trans, dir);
  3226. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3227. if (err) {
  3228. err = -ENOSPC;
  3229. goto out_unlock;
  3230. }
  3231. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3232. dentry->d_name.len,
  3233. dentry->d_parent->d_inode->i_ino,
  3234. objectid, BTRFS_I(dir)->block_group, mode,
  3235. &index);
  3236. err = PTR_ERR(inode);
  3237. if (IS_ERR(inode))
  3238. goto out_unlock;
  3239. err = btrfs_init_inode_security(inode, dir);
  3240. if (err) {
  3241. drop_inode = 1;
  3242. goto out_unlock;
  3243. }
  3244. btrfs_set_trans_block_group(trans, inode);
  3245. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  3246. if (err)
  3247. drop_inode = 1;
  3248. else {
  3249. inode->i_mapping->a_ops = &btrfs_aops;
  3250. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  3251. inode->i_fop = &btrfs_file_operations;
  3252. inode->i_op = &btrfs_file_inode_operations;
  3253. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  3254. }
  3255. dir->i_sb->s_dirt = 1;
  3256. btrfs_update_inode_block_group(trans, inode);
  3257. btrfs_update_inode_block_group(trans, dir);
  3258. out_unlock:
  3259. nr = trans->blocks_used;
  3260. btrfs_end_transaction_throttle(trans, root);
  3261. fail:
  3262. if (drop_inode) {
  3263. inode_dec_link_count(inode);
  3264. iput(inode);
  3265. }
  3266. btrfs_btree_balance_dirty(root, nr);
  3267. return err;
  3268. }
  3269. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  3270. struct dentry *dentry)
  3271. {
  3272. struct btrfs_trans_handle *trans;
  3273. struct btrfs_root *root = BTRFS_I(dir)->root;
  3274. struct inode *inode = old_dentry->d_inode;
  3275. u64 index;
  3276. unsigned long nr = 0;
  3277. int err;
  3278. int drop_inode = 0;
  3279. if (inode->i_nlink == 0)
  3280. return -ENOENT;
  3281. btrfs_inc_nlink(inode);
  3282. err = btrfs_check_metadata_free_space(root);
  3283. if (err)
  3284. goto fail;
  3285. err = btrfs_set_inode_index(dir, &index);
  3286. if (err)
  3287. goto fail;
  3288. trans = btrfs_start_transaction(root, 1);
  3289. btrfs_set_trans_block_group(trans, dir);
  3290. atomic_inc(&inode->i_count);
  3291. err = btrfs_add_nondir(trans, dentry, inode, 1, index);
  3292. if (err)
  3293. drop_inode = 1;
  3294. dir->i_sb->s_dirt = 1;
  3295. btrfs_update_inode_block_group(trans, dir);
  3296. err = btrfs_update_inode(trans, root, inode);
  3297. if (err)
  3298. drop_inode = 1;
  3299. nr = trans->blocks_used;
  3300. btrfs_end_transaction_throttle(trans, root);
  3301. fail:
  3302. if (drop_inode) {
  3303. inode_dec_link_count(inode);
  3304. iput(inode);
  3305. }
  3306. btrfs_btree_balance_dirty(root, nr);
  3307. return err;
  3308. }
  3309. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  3310. {
  3311. struct inode *inode = NULL;
  3312. struct btrfs_trans_handle *trans;
  3313. struct btrfs_root *root = BTRFS_I(dir)->root;
  3314. int err = 0;
  3315. int drop_on_err = 0;
  3316. u64 objectid = 0;
  3317. u64 index = 0;
  3318. unsigned long nr = 1;
  3319. err = btrfs_check_metadata_free_space(root);
  3320. if (err)
  3321. goto out_unlock;
  3322. trans = btrfs_start_transaction(root, 1);
  3323. btrfs_set_trans_block_group(trans, dir);
  3324. if (IS_ERR(trans)) {
  3325. err = PTR_ERR(trans);
  3326. goto out_unlock;
  3327. }
  3328. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  3329. if (err) {
  3330. err = -ENOSPC;
  3331. goto out_unlock;
  3332. }
  3333. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  3334. dentry->d_name.len,
  3335. dentry->d_parent->d_inode->i_ino, objectid,
  3336. BTRFS_I(dir)->block_group, S_IFDIR | mode,
  3337. &index);
  3338. if (IS_ERR(inode)) {
  3339. err = PTR_ERR(inode);
  3340. goto out_fail;
  3341. }
  3342. drop_on_err = 1;
  3343. err = btrfs_init_inode_security(inode, dir);
  3344. if (err)
  3345. goto out_fail;
  3346. inode->i_op = &btrfs_dir_inode_operations;
  3347. inode->i_fop = &btrfs_dir_file_operations;
  3348. btrfs_set_trans_block_group(trans, inode);
  3349. btrfs_i_size_write(inode, 0);
  3350. err = btrfs_update_inode(trans, root, inode);
  3351. if (err)
  3352. goto out_fail;
  3353. err = btrfs_add_link(trans, dentry->d_parent->d_inode,
  3354. inode, dentry->d_name.name,
  3355. dentry->d_name.len, 0, index);
  3356. if (err)
  3357. goto out_fail;
  3358. d_instantiate(dentry, inode);
  3359. drop_on_err = 0;
  3360. dir->i_sb->s_dirt = 1;
  3361. btrfs_update_inode_block_group(trans, inode);
  3362. btrfs_update_inode_block_group(trans, dir);
  3363. out_fail:
  3364. nr = trans->blocks_used;
  3365. btrfs_end_transaction_throttle(trans, root);
  3366. out_unlock:
  3367. if (drop_on_err)
  3368. iput(inode);
  3369. btrfs_btree_balance_dirty(root, nr);
  3370. return err;
  3371. }
  3372. /* helper for btfs_get_extent. Given an existing extent in the tree,
  3373. * and an extent that you want to insert, deal with overlap and insert
  3374. * the new extent into the tree.
  3375. */
  3376. static int merge_extent_mapping(struct extent_map_tree *em_tree,
  3377. struct extent_map *existing,
  3378. struct extent_map *em,
  3379. u64 map_start, u64 map_len)
  3380. {
  3381. u64 start_diff;
  3382. BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
  3383. start_diff = map_start - em->start;
  3384. em->start = map_start;
  3385. em->len = map_len;
  3386. if (em->block_start < EXTENT_MAP_LAST_BYTE &&
  3387. !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
  3388. em->block_start += start_diff;
  3389. em->block_len -= start_diff;
  3390. }
  3391. return add_extent_mapping(em_tree, em);
  3392. }
  3393. static noinline int uncompress_inline(struct btrfs_path *path,
  3394. struct inode *inode, struct page *page,
  3395. size_t pg_offset, u64 extent_offset,
  3396. struct btrfs_file_extent_item *item)
  3397. {
  3398. int ret;
  3399. struct extent_buffer *leaf = path->nodes[0];
  3400. char *tmp;
  3401. size_t max_size;
  3402. unsigned long inline_size;
  3403. unsigned long ptr;
  3404. WARN_ON(pg_offset != 0);
  3405. max_size = btrfs_file_extent_ram_bytes(leaf, item);
  3406. inline_size = btrfs_file_extent_inline_item_len(leaf,
  3407. btrfs_item_nr(leaf, path->slots[0]));
  3408. tmp = kmalloc(inline_size, GFP_NOFS);
  3409. ptr = btrfs_file_extent_inline_start(item);
  3410. read_extent_buffer(leaf, tmp, ptr, inline_size);
  3411. max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
  3412. ret = btrfs_zlib_decompress(tmp, page, extent_offset,
  3413. inline_size, max_size);
  3414. if (ret) {
  3415. char *kaddr = kmap_atomic(page, KM_USER0);
  3416. unsigned long copy_size = min_t(u64,
  3417. PAGE_CACHE_SIZE - pg_offset,
  3418. max_size - extent_offset);
  3419. memset(kaddr + pg_offset, 0, copy_size);
  3420. kunmap_atomic(kaddr, KM_USER0);
  3421. }
  3422. kfree(tmp);
  3423. return 0;
  3424. }
  3425. /*
  3426. * a bit scary, this does extent mapping from logical file offset to the disk.
  3427. * the ugly parts come from merging extents from the disk with the in-ram
  3428. * representation. This gets more complex because of the data=ordered code,
  3429. * where the in-ram extents might be locked pending data=ordered completion.
  3430. *
  3431. * This also copies inline extents directly into the page.
  3432. */
  3433. struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
  3434. size_t pg_offset, u64 start, u64 len,
  3435. int create)
  3436. {
  3437. int ret;
  3438. int err = 0;
  3439. u64 bytenr;
  3440. u64 extent_start = 0;
  3441. u64 extent_end = 0;
  3442. u64 objectid = inode->i_ino;
  3443. u32 found_type;
  3444. struct btrfs_path *path = NULL;
  3445. struct btrfs_root *root = BTRFS_I(inode)->root;
  3446. struct btrfs_file_extent_item *item;
  3447. struct extent_buffer *leaf;
  3448. struct btrfs_key found_key;
  3449. struct extent_map *em = NULL;
  3450. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  3451. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3452. struct btrfs_trans_handle *trans = NULL;
  3453. int compressed;
  3454. again:
  3455. spin_lock(&em_tree->lock);
  3456. em = lookup_extent_mapping(em_tree, start, len);
  3457. if (em)
  3458. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3459. spin_unlock(&em_tree->lock);
  3460. if (em) {
  3461. if (em->start > start || em->start + em->len <= start)
  3462. free_extent_map(em);
  3463. else if (em->block_start == EXTENT_MAP_INLINE && page)
  3464. free_extent_map(em);
  3465. else
  3466. goto out;
  3467. }
  3468. em = alloc_extent_map(GFP_NOFS);
  3469. if (!em) {
  3470. err = -ENOMEM;
  3471. goto out;
  3472. }
  3473. em->bdev = root->fs_info->fs_devices->latest_bdev;
  3474. em->start = EXTENT_MAP_HOLE;
  3475. em->orig_start = EXTENT_MAP_HOLE;
  3476. em->len = (u64)-1;
  3477. em->block_len = (u64)-1;
  3478. if (!path) {
  3479. path = btrfs_alloc_path();
  3480. BUG_ON(!path);
  3481. }
  3482. ret = btrfs_lookup_file_extent(trans, root, path,
  3483. objectid, start, trans != NULL);
  3484. if (ret < 0) {
  3485. err = ret;
  3486. goto out;
  3487. }
  3488. if (ret != 0) {
  3489. if (path->slots[0] == 0)
  3490. goto not_found;
  3491. path->slots[0]--;
  3492. }
  3493. leaf = path->nodes[0];
  3494. item = btrfs_item_ptr(leaf, path->slots[0],
  3495. struct btrfs_file_extent_item);
  3496. /* are we inside the extent that was found? */
  3497. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3498. found_type = btrfs_key_type(&found_key);
  3499. if (found_key.objectid != objectid ||
  3500. found_type != BTRFS_EXTENT_DATA_KEY) {
  3501. goto not_found;
  3502. }
  3503. found_type = btrfs_file_extent_type(leaf, item);
  3504. extent_start = found_key.offset;
  3505. compressed = btrfs_file_extent_compression(leaf, item);
  3506. if (found_type == BTRFS_FILE_EXTENT_REG ||
  3507. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  3508. extent_end = extent_start +
  3509. btrfs_file_extent_num_bytes(leaf, item);
  3510. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  3511. size_t size;
  3512. size = btrfs_file_extent_inline_len(leaf, item);
  3513. extent_end = (extent_start + size + root->sectorsize - 1) &
  3514. ~((u64)root->sectorsize - 1);
  3515. }
  3516. if (start >= extent_end) {
  3517. path->slots[0]++;
  3518. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  3519. ret = btrfs_next_leaf(root, path);
  3520. if (ret < 0) {
  3521. err = ret;
  3522. goto out;
  3523. }
  3524. if (ret > 0)
  3525. goto not_found;
  3526. leaf = path->nodes[0];
  3527. }
  3528. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3529. if (found_key.objectid != objectid ||
  3530. found_key.type != BTRFS_EXTENT_DATA_KEY)
  3531. goto not_found;
  3532. if (start + len <= found_key.offset)
  3533. goto not_found;
  3534. em->start = start;
  3535. em->len = found_key.offset - start;
  3536. goto not_found_em;
  3537. }
  3538. if (found_type == BTRFS_FILE_EXTENT_REG ||
  3539. found_type == BTRFS_FILE_EXTENT_PREALLOC) {
  3540. em->start = extent_start;
  3541. em->len = extent_end - extent_start;
  3542. em->orig_start = extent_start -
  3543. btrfs_file_extent_offset(leaf, item);
  3544. bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
  3545. if (bytenr == 0) {
  3546. em->block_start = EXTENT_MAP_HOLE;
  3547. goto insert;
  3548. }
  3549. if (compressed) {
  3550. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3551. em->block_start = bytenr;
  3552. em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
  3553. item);
  3554. } else {
  3555. bytenr += btrfs_file_extent_offset(leaf, item);
  3556. em->block_start = bytenr;
  3557. em->block_len = em->len;
  3558. if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
  3559. set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
  3560. }
  3561. goto insert;
  3562. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  3563. unsigned long ptr;
  3564. char *map;
  3565. size_t size;
  3566. size_t extent_offset;
  3567. size_t copy_size;
  3568. em->block_start = EXTENT_MAP_INLINE;
  3569. if (!page || create) {
  3570. em->start = extent_start;
  3571. em->len = extent_end - extent_start;
  3572. goto out;
  3573. }
  3574. size = btrfs_file_extent_inline_len(leaf, item);
  3575. extent_offset = page_offset(page) + pg_offset - extent_start;
  3576. copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
  3577. size - extent_offset);
  3578. em->start = extent_start + extent_offset;
  3579. em->len = (copy_size + root->sectorsize - 1) &
  3580. ~((u64)root->sectorsize - 1);
  3581. em->orig_start = EXTENT_MAP_INLINE;
  3582. if (compressed)
  3583. set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
  3584. ptr = btrfs_file_extent_inline_start(item) + extent_offset;
  3585. if (create == 0 && !PageUptodate(page)) {
  3586. if (btrfs_file_extent_compression(leaf, item) ==
  3587. BTRFS_COMPRESS_ZLIB) {
  3588. ret = uncompress_inline(path, inode, page,
  3589. pg_offset,
  3590. extent_offset, item);
  3591. BUG_ON(ret);
  3592. } else {
  3593. map = kmap(page);
  3594. read_extent_buffer(leaf, map + pg_offset, ptr,
  3595. copy_size);
  3596. kunmap(page);
  3597. }
  3598. flush_dcache_page(page);
  3599. } else if (create && PageUptodate(page)) {
  3600. if (!trans) {
  3601. kunmap(page);
  3602. free_extent_map(em);
  3603. em = NULL;
  3604. btrfs_release_path(root, path);
  3605. trans = btrfs_join_transaction(root, 1);
  3606. goto again;
  3607. }
  3608. map = kmap(page);
  3609. write_extent_buffer(leaf, map + pg_offset, ptr,
  3610. copy_size);
  3611. kunmap(page);
  3612. btrfs_mark_buffer_dirty(leaf);
  3613. }
  3614. set_extent_uptodate(io_tree, em->start,
  3615. extent_map_end(em) - 1, GFP_NOFS);
  3616. goto insert;
  3617. } else {
  3618. printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
  3619. WARN_ON(1);
  3620. }
  3621. not_found:
  3622. em->start = start;
  3623. em->len = len;
  3624. not_found_em:
  3625. em->block_start = EXTENT_MAP_HOLE;
  3626. set_bit(EXTENT_FLAG_VACANCY, &em->flags);
  3627. insert:
  3628. btrfs_release_path(root, path);
  3629. if (em->start > start || extent_map_end(em) <= start) {
  3630. printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
  3631. "[%llu %llu]\n", (unsigned long long)em->start,
  3632. (unsigned long long)em->len,
  3633. (unsigned long long)start,
  3634. (unsigned long long)len);
  3635. err = -EIO;
  3636. goto out;
  3637. }
  3638. err = 0;
  3639. spin_lock(&em_tree->lock);
  3640. ret = add_extent_mapping(em_tree, em);
  3641. /* it is possible that someone inserted the extent into the tree
  3642. * while we had the lock dropped. It is also possible that
  3643. * an overlapping map exists in the tree
  3644. */
  3645. if (ret == -EEXIST) {
  3646. struct extent_map *existing;
  3647. ret = 0;
  3648. existing = lookup_extent_mapping(em_tree, start, len);
  3649. if (existing && (existing->start > start ||
  3650. existing->start + existing->len <= start)) {
  3651. free_extent_map(existing);
  3652. existing = NULL;
  3653. }
  3654. if (!existing) {
  3655. existing = lookup_extent_mapping(em_tree, em->start,
  3656. em->len);
  3657. if (existing) {
  3658. err = merge_extent_mapping(em_tree, existing,
  3659. em, start,
  3660. root->sectorsize);
  3661. free_extent_map(existing);
  3662. if (err) {
  3663. free_extent_map(em);
  3664. em = NULL;
  3665. }
  3666. } else {
  3667. err = -EIO;
  3668. free_extent_map(em);
  3669. em = NULL;
  3670. }
  3671. } else {
  3672. free_extent_map(em);
  3673. em = existing;
  3674. err = 0;
  3675. }
  3676. }
  3677. spin_unlock(&em_tree->lock);
  3678. out:
  3679. if (path)
  3680. btrfs_free_path(path);
  3681. if (trans) {
  3682. ret = btrfs_end_transaction(trans, root);
  3683. if (!err)
  3684. err = ret;
  3685. }
  3686. if (err) {
  3687. free_extent_map(em);
  3688. WARN_ON(1);
  3689. return ERR_PTR(err);
  3690. }
  3691. return em;
  3692. }
  3693. static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
  3694. const struct iovec *iov, loff_t offset,
  3695. unsigned long nr_segs)
  3696. {
  3697. return -EINVAL;
  3698. }
  3699. static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3700. __u64 start, __u64 len)
  3701. {
  3702. return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
  3703. }
  3704. int btrfs_readpage(struct file *file, struct page *page)
  3705. {
  3706. struct extent_io_tree *tree;
  3707. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3708. return extent_read_full_page(tree, page, btrfs_get_extent);
  3709. }
  3710. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  3711. {
  3712. struct extent_io_tree *tree;
  3713. if (current->flags & PF_MEMALLOC) {
  3714. redirty_page_for_writepage(wbc, page);
  3715. unlock_page(page);
  3716. return 0;
  3717. }
  3718. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3719. return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
  3720. }
  3721. int btrfs_writepages(struct address_space *mapping,
  3722. struct writeback_control *wbc)
  3723. {
  3724. struct extent_io_tree *tree;
  3725. tree = &BTRFS_I(mapping->host)->io_tree;
  3726. return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
  3727. }
  3728. static int
  3729. btrfs_readpages(struct file *file, struct address_space *mapping,
  3730. struct list_head *pages, unsigned nr_pages)
  3731. {
  3732. struct extent_io_tree *tree;
  3733. tree = &BTRFS_I(mapping->host)->io_tree;
  3734. return extent_readpages(tree, mapping, pages, nr_pages,
  3735. btrfs_get_extent);
  3736. }
  3737. static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  3738. {
  3739. struct extent_io_tree *tree;
  3740. struct extent_map_tree *map;
  3741. int ret;
  3742. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3743. map = &BTRFS_I(page->mapping->host)->extent_tree;
  3744. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  3745. if (ret == 1) {
  3746. ClearPagePrivate(page);
  3747. set_page_private(page, 0);
  3748. page_cache_release(page);
  3749. }
  3750. return ret;
  3751. }
  3752. static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
  3753. {
  3754. if (PageWriteback(page) || PageDirty(page))
  3755. return 0;
  3756. return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
  3757. }
  3758. static void btrfs_invalidatepage(struct page *page, unsigned long offset)
  3759. {
  3760. struct extent_io_tree *tree;
  3761. struct btrfs_ordered_extent *ordered;
  3762. u64 page_start = page_offset(page);
  3763. u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
  3764. wait_on_page_writeback(page);
  3765. tree = &BTRFS_I(page->mapping->host)->io_tree;
  3766. if (offset) {
  3767. btrfs_releasepage(page, GFP_NOFS);
  3768. return;
  3769. }
  3770. lock_extent(tree, page_start, page_end, GFP_NOFS);
  3771. ordered = btrfs_lookup_ordered_extent(page->mapping->host,
  3772. page_offset(page));
  3773. if (ordered) {
  3774. /*
  3775. * IO on this page will never be started, so we need
  3776. * to account for any ordered extents now
  3777. */
  3778. clear_extent_bit(tree, page_start, page_end,
  3779. EXTENT_DIRTY | EXTENT_DELALLOC |
  3780. EXTENT_LOCKED, 1, 0, GFP_NOFS);
  3781. btrfs_finish_ordered_io(page->mapping->host,
  3782. page_start, page_end);
  3783. btrfs_put_ordered_extent(ordered);
  3784. lock_extent(tree, page_start, page_end, GFP_NOFS);
  3785. }
  3786. clear_extent_bit(tree, page_start, page_end,
  3787. EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
  3788. EXTENT_ORDERED,
  3789. 1, 1, GFP_NOFS);
  3790. __btrfs_releasepage(page, GFP_NOFS);
  3791. ClearPageChecked(page);
  3792. if (PagePrivate(page)) {
  3793. ClearPagePrivate(page);
  3794. set_page_private(page, 0);
  3795. page_cache_release(page);
  3796. }
  3797. }
  3798. /*
  3799. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  3800. * called from a page fault handler when a page is first dirtied. Hence we must
  3801. * be careful to check for EOF conditions here. We set the page up correctly
  3802. * for a written page which means we get ENOSPC checking when writing into
  3803. * holes and correct delalloc and unwritten extent mapping on filesystems that
  3804. * support these features.
  3805. *
  3806. * We are not allowed to take the i_mutex here so we have to play games to
  3807. * protect against truncate races as the page could now be beyond EOF. Because
  3808. * vmtruncate() writes the inode size before removing pages, once we have the
  3809. * page lock we can determine safely if the page is beyond EOF. If it is not
  3810. * beyond EOF, then the page is guaranteed safe against truncation until we
  3811. * unlock the page.
  3812. */
  3813. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  3814. {
  3815. struct inode *inode = fdentry(vma->vm_file)->d_inode;
  3816. struct btrfs_root *root = BTRFS_I(inode)->root;
  3817. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  3818. struct btrfs_ordered_extent *ordered;
  3819. char *kaddr;
  3820. unsigned long zero_start;
  3821. loff_t size;
  3822. int ret;
  3823. u64 page_start;
  3824. u64 page_end;
  3825. ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
  3826. if (ret)
  3827. goto out;
  3828. ret = -EINVAL;
  3829. again:
  3830. lock_page(page);
  3831. size = i_size_read(inode);
  3832. page_start = page_offset(page);
  3833. page_end = page_start + PAGE_CACHE_SIZE - 1;
  3834. if ((page->mapping != inode->i_mapping) ||
  3835. (page_start >= size)) {
  3836. btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
  3837. /* page got truncated out from underneath us */
  3838. goto out_unlock;
  3839. }
  3840. wait_on_page_writeback(page);
  3841. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3842. set_page_extent_mapped(page);
  3843. /*
  3844. * we can't set the delalloc bits if there are pending ordered
  3845. * extents. Drop our locks and wait for them to finish
  3846. */
  3847. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  3848. if (ordered) {
  3849. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3850. unlock_page(page);
  3851. btrfs_start_ordered_extent(inode, ordered, 1);
  3852. btrfs_put_ordered_extent(ordered);
  3853. goto again;
  3854. }
  3855. btrfs_set_extent_delalloc(inode, page_start, page_end);
  3856. ret = 0;
  3857. /* page is wholly or partially inside EOF */
  3858. if (page_start + PAGE_CACHE_SIZE > size)
  3859. zero_start = size & ~PAGE_CACHE_MASK;
  3860. else
  3861. zero_start = PAGE_CACHE_SIZE;
  3862. if (zero_start != PAGE_CACHE_SIZE) {
  3863. kaddr = kmap(page);
  3864. memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
  3865. flush_dcache_page(page);
  3866. kunmap(page);
  3867. }
  3868. ClearPageChecked(page);
  3869. set_page_dirty(page);
  3870. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  3871. out_unlock:
  3872. unlock_page(page);
  3873. out:
  3874. return ret;
  3875. }
  3876. static void btrfs_truncate(struct inode *inode)
  3877. {
  3878. struct btrfs_root *root = BTRFS_I(inode)->root;
  3879. int ret;
  3880. struct btrfs_trans_handle *trans;
  3881. unsigned long nr;
  3882. u64 mask = root->sectorsize - 1;
  3883. if (!S_ISREG(inode->i_mode))
  3884. return;
  3885. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3886. return;
  3887. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  3888. btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
  3889. trans = btrfs_start_transaction(root, 1);
  3890. btrfs_set_trans_block_group(trans, inode);
  3891. btrfs_i_size_write(inode, inode->i_size);
  3892. ret = btrfs_orphan_add(trans, inode);
  3893. if (ret)
  3894. goto out;
  3895. /* FIXME, add redo link to tree so we don't leak on crash */
  3896. ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
  3897. BTRFS_EXTENT_DATA_KEY);
  3898. btrfs_update_inode(trans, root, inode);
  3899. ret = btrfs_orphan_del(trans, inode);
  3900. BUG_ON(ret);
  3901. out:
  3902. nr = trans->blocks_used;
  3903. ret = btrfs_end_transaction_throttle(trans, root);
  3904. BUG_ON(ret);
  3905. btrfs_btree_balance_dirty(root, nr);
  3906. }
  3907. /*
  3908. * create a new subvolume directory/inode (helper for the ioctl).
  3909. */
  3910. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  3911. struct btrfs_root *new_root, struct dentry *dentry,
  3912. u64 new_dirid, u64 alloc_hint)
  3913. {
  3914. struct inode *inode;
  3915. int error;
  3916. u64 index = 0;
  3917. inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
  3918. new_dirid, alloc_hint, S_IFDIR | 0700, &index);
  3919. if (IS_ERR(inode))
  3920. return PTR_ERR(inode);
  3921. inode->i_op = &btrfs_dir_inode_operations;
  3922. inode->i_fop = &btrfs_dir_file_operations;
  3923. inode->i_nlink = 1;
  3924. btrfs_i_size_write(inode, 0);
  3925. error = btrfs_update_inode(trans, new_root, inode);
  3926. if (error)
  3927. return error;
  3928. d_instantiate(dentry, inode);
  3929. return 0;
  3930. }
  3931. /* helper function for file defrag and space balancing. This
  3932. * forces readahead on a given range of bytes in an inode
  3933. */
  3934. unsigned long btrfs_force_ra(struct address_space *mapping,
  3935. struct file_ra_state *ra, struct file *file,
  3936. pgoff_t offset, pgoff_t last_index)
  3937. {
  3938. pgoff_t req_size = last_index - offset + 1;
  3939. page_cache_sync_readahead(mapping, ra, file, offset, req_size);
  3940. return offset + req_size;
  3941. }
  3942. struct inode *btrfs_alloc_inode(struct super_block *sb)
  3943. {
  3944. struct btrfs_inode *ei;
  3945. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  3946. if (!ei)
  3947. return NULL;
  3948. ei->last_trans = 0;
  3949. ei->logged_trans = 0;
  3950. btrfs_ordered_inode_tree_init(&ei->ordered_tree);
  3951. ei->i_acl = BTRFS_ACL_NOT_CACHED;
  3952. ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
  3953. INIT_LIST_HEAD(&ei->i_orphan);
  3954. return &ei->vfs_inode;
  3955. }
  3956. void btrfs_destroy_inode(struct inode *inode)
  3957. {
  3958. struct btrfs_ordered_extent *ordered;
  3959. WARN_ON(!list_empty(&inode->i_dentry));
  3960. WARN_ON(inode->i_data.nrpages);
  3961. if (BTRFS_I(inode)->i_acl &&
  3962. BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
  3963. posix_acl_release(BTRFS_I(inode)->i_acl);
  3964. if (BTRFS_I(inode)->i_default_acl &&
  3965. BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
  3966. posix_acl_release(BTRFS_I(inode)->i_default_acl);
  3967. spin_lock(&BTRFS_I(inode)->root->list_lock);
  3968. if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
  3969. printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
  3970. " list\n", inode->i_ino);
  3971. dump_stack();
  3972. }
  3973. spin_unlock(&BTRFS_I(inode)->root->list_lock);
  3974. while (1) {
  3975. ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
  3976. if (!ordered)
  3977. break;
  3978. else {
  3979. printk(KERN_ERR "btrfs found ordered "
  3980. "extent %llu %llu on inode cleanup\n",
  3981. (unsigned long long)ordered->file_offset,
  3982. (unsigned long long)ordered->len);
  3983. btrfs_remove_ordered_extent(inode, ordered);
  3984. btrfs_put_ordered_extent(ordered);
  3985. btrfs_put_ordered_extent(ordered);
  3986. }
  3987. }
  3988. btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
  3989. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  3990. }
  3991. static void init_once(void *foo)
  3992. {
  3993. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  3994. inode_init_once(&ei->vfs_inode);
  3995. }
  3996. void btrfs_destroy_cachep(void)
  3997. {
  3998. if (btrfs_inode_cachep)
  3999. kmem_cache_destroy(btrfs_inode_cachep);
  4000. if (btrfs_trans_handle_cachep)
  4001. kmem_cache_destroy(btrfs_trans_handle_cachep);
  4002. if (btrfs_transaction_cachep)
  4003. kmem_cache_destroy(btrfs_transaction_cachep);
  4004. if (btrfs_bit_radix_cachep)
  4005. kmem_cache_destroy(btrfs_bit_radix_cachep);
  4006. if (btrfs_path_cachep)
  4007. kmem_cache_destroy(btrfs_path_cachep);
  4008. }
  4009. struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
  4010. unsigned long extra_flags,
  4011. void (*ctor)(void *))
  4012. {
  4013. return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
  4014. SLAB_MEM_SPREAD | extra_flags), ctor);
  4015. }
  4016. int btrfs_init_cachep(void)
  4017. {
  4018. btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
  4019. sizeof(struct btrfs_inode),
  4020. 0, init_once);
  4021. if (!btrfs_inode_cachep)
  4022. goto fail;
  4023. btrfs_trans_handle_cachep =
  4024. btrfs_cache_create("btrfs_trans_handle_cache",
  4025. sizeof(struct btrfs_trans_handle),
  4026. 0, NULL);
  4027. if (!btrfs_trans_handle_cachep)
  4028. goto fail;
  4029. btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
  4030. sizeof(struct btrfs_transaction),
  4031. 0, NULL);
  4032. if (!btrfs_transaction_cachep)
  4033. goto fail;
  4034. btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
  4035. sizeof(struct btrfs_path),
  4036. 0, NULL);
  4037. if (!btrfs_path_cachep)
  4038. goto fail;
  4039. btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
  4040. SLAB_DESTROY_BY_RCU, NULL);
  4041. if (!btrfs_bit_radix_cachep)
  4042. goto fail;
  4043. return 0;
  4044. fail:
  4045. btrfs_destroy_cachep();
  4046. return -ENOMEM;
  4047. }
  4048. static int btrfs_getattr(struct vfsmount *mnt,
  4049. struct dentry *dentry, struct kstat *stat)
  4050. {
  4051. struct inode *inode = dentry->d_inode;
  4052. generic_fillattr(inode, stat);
  4053. stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
  4054. stat->blksize = PAGE_CACHE_SIZE;
  4055. stat->blocks = (inode_get_bytes(inode) +
  4056. BTRFS_I(inode)->delalloc_bytes) >> 9;
  4057. return 0;
  4058. }
  4059. static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  4060. struct inode *new_dir, struct dentry *new_dentry)
  4061. {
  4062. struct btrfs_trans_handle *trans;
  4063. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  4064. struct inode *new_inode = new_dentry->d_inode;
  4065. struct inode *old_inode = old_dentry->d_inode;
  4066. struct timespec ctime = CURRENT_TIME;
  4067. u64 index = 0;
  4068. int ret;
  4069. /* we're not allowed to rename between subvolumes */
  4070. if (BTRFS_I(old_inode)->root->root_key.objectid !=
  4071. BTRFS_I(new_dir)->root->root_key.objectid)
  4072. return -EXDEV;
  4073. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  4074. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
  4075. return -ENOTEMPTY;
  4076. }
  4077. /* to rename a snapshot or subvolume, we need to juggle the
  4078. * backrefs. This isn't coded yet
  4079. */
  4080. if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
  4081. return -EXDEV;
  4082. ret = btrfs_check_metadata_free_space(root);
  4083. if (ret)
  4084. goto out_unlock;
  4085. trans = btrfs_start_transaction(root, 1);
  4086. btrfs_set_trans_block_group(trans, new_dir);
  4087. btrfs_inc_nlink(old_dentry->d_inode);
  4088. old_dir->i_ctime = old_dir->i_mtime = ctime;
  4089. new_dir->i_ctime = new_dir->i_mtime = ctime;
  4090. old_inode->i_ctime = ctime;
  4091. ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
  4092. old_dentry->d_name.name,
  4093. old_dentry->d_name.len);
  4094. if (ret)
  4095. goto out_fail;
  4096. if (new_inode) {
  4097. new_inode->i_ctime = CURRENT_TIME;
  4098. ret = btrfs_unlink_inode(trans, root, new_dir,
  4099. new_dentry->d_inode,
  4100. new_dentry->d_name.name,
  4101. new_dentry->d_name.len);
  4102. if (ret)
  4103. goto out_fail;
  4104. if (new_inode->i_nlink == 0) {
  4105. ret = btrfs_orphan_add(trans, new_dentry->d_inode);
  4106. if (ret)
  4107. goto out_fail;
  4108. }
  4109. }
  4110. ret = btrfs_set_inode_index(new_dir, &index);
  4111. if (ret)
  4112. goto out_fail;
  4113. ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
  4114. old_inode, new_dentry->d_name.name,
  4115. new_dentry->d_name.len, 1, index);
  4116. if (ret)
  4117. goto out_fail;
  4118. out_fail:
  4119. btrfs_end_transaction_throttle(trans, root);
  4120. out_unlock:
  4121. return ret;
  4122. }
  4123. /*
  4124. * some fairly slow code that needs optimization. This walks the list
  4125. * of all the inodes with pending delalloc and forces them to disk.
  4126. */
  4127. int btrfs_start_delalloc_inodes(struct btrfs_root *root)
  4128. {
  4129. struct list_head *head = &root->fs_info->delalloc_inodes;
  4130. struct btrfs_inode *binode;
  4131. struct inode *inode;
  4132. if (root->fs_info->sb->s_flags & MS_RDONLY)
  4133. return -EROFS;
  4134. spin_lock(&root->fs_info->delalloc_lock);
  4135. while (!list_empty(head)) {
  4136. binode = list_entry(head->next, struct btrfs_inode,
  4137. delalloc_inodes);
  4138. inode = igrab(&binode->vfs_inode);
  4139. if (!inode)
  4140. list_del_init(&binode->delalloc_inodes);
  4141. spin_unlock(&root->fs_info->delalloc_lock);
  4142. if (inode) {
  4143. filemap_flush(inode->i_mapping);
  4144. iput(inode);
  4145. }
  4146. cond_resched();
  4147. spin_lock(&root->fs_info->delalloc_lock);
  4148. }
  4149. spin_unlock(&root->fs_info->delalloc_lock);
  4150. /* the filemap_flush will queue IO into the worker threads, but
  4151. * we have to make sure the IO is actually started and that
  4152. * ordered extents get created before we return
  4153. */
  4154. atomic_inc(&root->fs_info->async_submit_draining);
  4155. while (atomic_read(&root->fs_info->nr_async_submits) ||
  4156. atomic_read(&root->fs_info->async_delalloc_pages)) {
  4157. wait_event(root->fs_info->async_submit_wait,
  4158. (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
  4159. atomic_read(&root->fs_info->async_delalloc_pages) == 0));
  4160. }
  4161. atomic_dec(&root->fs_info->async_submit_draining);
  4162. return 0;
  4163. }
  4164. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  4165. const char *symname)
  4166. {
  4167. struct btrfs_trans_handle *trans;
  4168. struct btrfs_root *root = BTRFS_I(dir)->root;
  4169. struct btrfs_path *path;
  4170. struct btrfs_key key;
  4171. struct inode *inode = NULL;
  4172. int err;
  4173. int drop_inode = 0;
  4174. u64 objectid;
  4175. u64 index = 0 ;
  4176. int name_len;
  4177. int datasize;
  4178. unsigned long ptr;
  4179. struct btrfs_file_extent_item *ei;
  4180. struct extent_buffer *leaf;
  4181. unsigned long nr = 0;
  4182. name_len = strlen(symname) + 1;
  4183. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  4184. return -ENAMETOOLONG;
  4185. err = btrfs_check_metadata_free_space(root);
  4186. if (err)
  4187. goto out_fail;
  4188. trans = btrfs_start_transaction(root, 1);
  4189. btrfs_set_trans_block_group(trans, dir);
  4190. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  4191. if (err) {
  4192. err = -ENOSPC;
  4193. goto out_unlock;
  4194. }
  4195. inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
  4196. dentry->d_name.len,
  4197. dentry->d_parent->d_inode->i_ino, objectid,
  4198. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
  4199. &index);
  4200. err = PTR_ERR(inode);
  4201. if (IS_ERR(inode))
  4202. goto out_unlock;
  4203. err = btrfs_init_inode_security(inode, dir);
  4204. if (err) {
  4205. drop_inode = 1;
  4206. goto out_unlock;
  4207. }
  4208. btrfs_set_trans_block_group(trans, inode);
  4209. err = btrfs_add_nondir(trans, dentry, inode, 0, index);
  4210. if (err)
  4211. drop_inode = 1;
  4212. else {
  4213. inode->i_mapping->a_ops = &btrfs_aops;
  4214. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4215. inode->i_fop = &btrfs_file_operations;
  4216. inode->i_op = &btrfs_file_inode_operations;
  4217. BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
  4218. }
  4219. dir->i_sb->s_dirt = 1;
  4220. btrfs_update_inode_block_group(trans, inode);
  4221. btrfs_update_inode_block_group(trans, dir);
  4222. if (drop_inode)
  4223. goto out_unlock;
  4224. path = btrfs_alloc_path();
  4225. BUG_ON(!path);
  4226. key.objectid = inode->i_ino;
  4227. key.offset = 0;
  4228. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  4229. datasize = btrfs_file_extent_calc_inline_size(name_len);
  4230. err = btrfs_insert_empty_item(trans, root, path, &key,
  4231. datasize);
  4232. if (err) {
  4233. drop_inode = 1;
  4234. goto out_unlock;
  4235. }
  4236. leaf = path->nodes[0];
  4237. ei = btrfs_item_ptr(leaf, path->slots[0],
  4238. struct btrfs_file_extent_item);
  4239. btrfs_set_file_extent_generation(leaf, ei, trans->transid);
  4240. btrfs_set_file_extent_type(leaf, ei,
  4241. BTRFS_FILE_EXTENT_INLINE);
  4242. btrfs_set_file_extent_encryption(leaf, ei, 0);
  4243. btrfs_set_file_extent_compression(leaf, ei, 0);
  4244. btrfs_set_file_extent_other_encoding(leaf, ei, 0);
  4245. btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
  4246. ptr = btrfs_file_extent_inline_start(ei);
  4247. write_extent_buffer(leaf, symname, ptr, name_len);
  4248. btrfs_mark_buffer_dirty(leaf);
  4249. btrfs_free_path(path);
  4250. inode->i_op = &btrfs_symlink_inode_operations;
  4251. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  4252. inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
  4253. inode_set_bytes(inode, name_len);
  4254. btrfs_i_size_write(inode, name_len - 1);
  4255. err = btrfs_update_inode(trans, root, inode);
  4256. if (err)
  4257. drop_inode = 1;
  4258. out_unlock:
  4259. nr = trans->blocks_used;
  4260. btrfs_end_transaction_throttle(trans, root);
  4261. out_fail:
  4262. if (drop_inode) {
  4263. inode_dec_link_count(inode);
  4264. iput(inode);
  4265. }
  4266. btrfs_btree_balance_dirty(root, nr);
  4267. return err;
  4268. }
  4269. static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
  4270. u64 alloc_hint, int mode)
  4271. {
  4272. struct btrfs_trans_handle *trans;
  4273. struct btrfs_root *root = BTRFS_I(inode)->root;
  4274. struct btrfs_key ins;
  4275. u64 alloc_size;
  4276. u64 cur_offset = start;
  4277. u64 num_bytes = end - start;
  4278. int ret = 0;
  4279. trans = btrfs_join_transaction(root, 1);
  4280. BUG_ON(!trans);
  4281. btrfs_set_trans_block_group(trans, inode);
  4282. while (num_bytes > 0) {
  4283. alloc_size = min(num_bytes, root->fs_info->max_extent);
  4284. ret = btrfs_reserve_extent(trans, root, alloc_size,
  4285. root->sectorsize, 0, alloc_hint,
  4286. (u64)-1, &ins, 1);
  4287. if (ret) {
  4288. WARN_ON(1);
  4289. goto out;
  4290. }
  4291. ret = insert_reserved_file_extent(trans, inode,
  4292. cur_offset, ins.objectid,
  4293. ins.offset, ins.offset,
  4294. ins.offset, 0, 0, 0,
  4295. BTRFS_FILE_EXTENT_PREALLOC);
  4296. BUG_ON(ret);
  4297. num_bytes -= ins.offset;
  4298. cur_offset += ins.offset;
  4299. alloc_hint = ins.objectid + ins.offset;
  4300. }
  4301. out:
  4302. if (cur_offset > start) {
  4303. inode->i_ctime = CURRENT_TIME;
  4304. btrfs_set_flag(inode, PREALLOC);
  4305. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  4306. cur_offset > i_size_read(inode))
  4307. btrfs_i_size_write(inode, cur_offset);
  4308. ret = btrfs_update_inode(trans, root, inode);
  4309. BUG_ON(ret);
  4310. }
  4311. btrfs_end_transaction(trans, root);
  4312. return ret;
  4313. }
  4314. static long btrfs_fallocate(struct inode *inode, int mode,
  4315. loff_t offset, loff_t len)
  4316. {
  4317. u64 cur_offset;
  4318. u64 last_byte;
  4319. u64 alloc_start;
  4320. u64 alloc_end;
  4321. u64 alloc_hint = 0;
  4322. u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
  4323. struct extent_map *em;
  4324. int ret;
  4325. alloc_start = offset & ~mask;
  4326. alloc_end = (offset + len + mask) & ~mask;
  4327. mutex_lock(&inode->i_mutex);
  4328. if (alloc_start > inode->i_size) {
  4329. ret = btrfs_cont_expand(inode, alloc_start);
  4330. if (ret)
  4331. goto out;
  4332. }
  4333. while (1) {
  4334. struct btrfs_ordered_extent *ordered;
  4335. lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
  4336. alloc_end - 1, GFP_NOFS);
  4337. ordered = btrfs_lookup_first_ordered_extent(inode,
  4338. alloc_end - 1);
  4339. if (ordered &&
  4340. ordered->file_offset + ordered->len > alloc_start &&
  4341. ordered->file_offset < alloc_end) {
  4342. btrfs_put_ordered_extent(ordered);
  4343. unlock_extent(&BTRFS_I(inode)->io_tree,
  4344. alloc_start, alloc_end - 1, GFP_NOFS);
  4345. btrfs_wait_ordered_range(inode, alloc_start,
  4346. alloc_end - alloc_start);
  4347. } else {
  4348. if (ordered)
  4349. btrfs_put_ordered_extent(ordered);
  4350. break;
  4351. }
  4352. }
  4353. cur_offset = alloc_start;
  4354. while (1) {
  4355. em = btrfs_get_extent(inode, NULL, 0, cur_offset,
  4356. alloc_end - cur_offset, 0);
  4357. BUG_ON(IS_ERR(em) || !em);
  4358. last_byte = min(extent_map_end(em), alloc_end);
  4359. last_byte = (last_byte + mask) & ~mask;
  4360. if (em->block_start == EXTENT_MAP_HOLE) {
  4361. ret = prealloc_file_range(inode, cur_offset,
  4362. last_byte, alloc_hint, mode);
  4363. if (ret < 0) {
  4364. free_extent_map(em);
  4365. break;
  4366. }
  4367. }
  4368. if (em->block_start <= EXTENT_MAP_LAST_BYTE)
  4369. alloc_hint = em->block_start;
  4370. free_extent_map(em);
  4371. cur_offset = last_byte;
  4372. if (cur_offset >= alloc_end) {
  4373. ret = 0;
  4374. break;
  4375. }
  4376. }
  4377. unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
  4378. GFP_NOFS);
  4379. out:
  4380. mutex_unlock(&inode->i_mutex);
  4381. return ret;
  4382. }
  4383. static int btrfs_set_page_dirty(struct page *page)
  4384. {
  4385. return __set_page_dirty_nobuffers(page);
  4386. }
  4387. static int btrfs_permission(struct inode *inode, int mask)
  4388. {
  4389. if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
  4390. return -EACCES;
  4391. return generic_permission(inode, mask, btrfs_check_acl);
  4392. }
  4393. static struct inode_operations btrfs_dir_inode_operations = {
  4394. .getattr = btrfs_getattr,
  4395. .lookup = btrfs_lookup,
  4396. .create = btrfs_create,
  4397. .unlink = btrfs_unlink,
  4398. .link = btrfs_link,
  4399. .mkdir = btrfs_mkdir,
  4400. .rmdir = btrfs_rmdir,
  4401. .rename = btrfs_rename,
  4402. .symlink = btrfs_symlink,
  4403. .setattr = btrfs_setattr,
  4404. .mknod = btrfs_mknod,
  4405. .setxattr = btrfs_setxattr,
  4406. .getxattr = btrfs_getxattr,
  4407. .listxattr = btrfs_listxattr,
  4408. .removexattr = btrfs_removexattr,
  4409. .permission = btrfs_permission,
  4410. };
  4411. static struct inode_operations btrfs_dir_ro_inode_operations = {
  4412. .lookup = btrfs_lookup,
  4413. .permission = btrfs_permission,
  4414. };
  4415. static struct file_operations btrfs_dir_file_operations = {
  4416. .llseek = generic_file_llseek,
  4417. .read = generic_read_dir,
  4418. .readdir = btrfs_real_readdir,
  4419. .unlocked_ioctl = btrfs_ioctl,
  4420. #ifdef CONFIG_COMPAT
  4421. .compat_ioctl = btrfs_ioctl,
  4422. #endif
  4423. .release = btrfs_release_file,
  4424. .fsync = btrfs_sync_file,
  4425. };
  4426. static struct extent_io_ops btrfs_extent_io_ops = {
  4427. .fill_delalloc = run_delalloc_range,
  4428. .submit_bio_hook = btrfs_submit_bio_hook,
  4429. .merge_bio_hook = btrfs_merge_bio_hook,
  4430. .readpage_end_io_hook = btrfs_readpage_end_io_hook,
  4431. .writepage_end_io_hook = btrfs_writepage_end_io_hook,
  4432. .writepage_start_hook = btrfs_writepage_start_hook,
  4433. .readpage_io_failed_hook = btrfs_io_failed_hook,
  4434. .set_bit_hook = btrfs_set_bit_hook,
  4435. .clear_bit_hook = btrfs_clear_bit_hook,
  4436. };
  4437. /*
  4438. * btrfs doesn't support the bmap operation because swapfiles
  4439. * use bmap to make a mapping of extents in the file. They assume
  4440. * these extents won't change over the life of the file and they
  4441. * use the bmap result to do IO directly to the drive.
  4442. *
  4443. * the btrfs bmap call would return logical addresses that aren't
  4444. * suitable for IO and they also will change frequently as COW
  4445. * operations happen. So, swapfile + btrfs == corruption.
  4446. *
  4447. * For now we're avoiding this by dropping bmap.
  4448. */
  4449. static struct address_space_operations btrfs_aops = {
  4450. .readpage = btrfs_readpage,
  4451. .writepage = btrfs_writepage,
  4452. .writepages = btrfs_writepages,
  4453. .readpages = btrfs_readpages,
  4454. .sync_page = block_sync_page,
  4455. .direct_IO = btrfs_direct_IO,
  4456. .invalidatepage = btrfs_invalidatepage,
  4457. .releasepage = btrfs_releasepage,
  4458. .set_page_dirty = btrfs_set_page_dirty,
  4459. };
  4460. static struct address_space_operations btrfs_symlink_aops = {
  4461. .readpage = btrfs_readpage,
  4462. .writepage = btrfs_writepage,
  4463. .invalidatepage = btrfs_invalidatepage,
  4464. .releasepage = btrfs_releasepage,
  4465. };
  4466. static struct inode_operations btrfs_file_inode_operations = {
  4467. .truncate = btrfs_truncate,
  4468. .getattr = btrfs_getattr,
  4469. .setattr = btrfs_setattr,
  4470. .setxattr = btrfs_setxattr,
  4471. .getxattr = btrfs_getxattr,
  4472. .listxattr = btrfs_listxattr,
  4473. .removexattr = btrfs_removexattr,
  4474. .permission = btrfs_permission,
  4475. .fallocate = btrfs_fallocate,
  4476. .fiemap = btrfs_fiemap,
  4477. };
  4478. static struct inode_operations btrfs_special_inode_operations = {
  4479. .getattr = btrfs_getattr,
  4480. .setattr = btrfs_setattr,
  4481. .permission = btrfs_permission,
  4482. .setxattr = btrfs_setxattr,
  4483. .getxattr = btrfs_getxattr,
  4484. .listxattr = btrfs_listxattr,
  4485. .removexattr = btrfs_removexattr,
  4486. };
  4487. static struct inode_operations btrfs_symlink_inode_operations = {
  4488. .readlink = generic_readlink,
  4489. .follow_link = page_follow_link_light,
  4490. .put_link = page_put_link,
  4491. .permission = btrfs_permission,
  4492. .setxattr = btrfs_setxattr,
  4493. .getxattr = btrfs_getxattr,
  4494. .listxattr = btrfs_listxattr,
  4495. .removexattr = btrfs_removexattr,
  4496. };