inode.c 134 KB

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