inode.c 102 KB

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