inode.c 95 KB

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