inode.c 96 KB

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