inode.c 93 KB

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