inode.c 78 KB

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