inode.c 85 KB

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