inode.c 133 KB

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