inode.c 76 KB

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