inode.c 97 KB

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