inode.c 134 KB

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