inode.c 94 KB

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