inode.c 95 KB

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