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