inode.c 122 KB

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