inode.c 116 KB

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