inode.c 98 KB

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