inode.c 75 KB

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