inode.c 97 KB

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