inode.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629
  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 "ctree.h"
  34. #include "disk-io.h"
  35. #include "transaction.h"
  36. #include "btrfs_inode.h"
  37. #include "ioctl.h"
  38. #include "print-tree.h"
  39. struct btrfs_iget_args {
  40. u64 ino;
  41. struct btrfs_root *root;
  42. };
  43. static struct inode_operations btrfs_dir_inode_operations;
  44. static struct inode_operations btrfs_symlink_inode_operations;
  45. static struct inode_operations btrfs_dir_ro_inode_operations;
  46. static struct inode_operations btrfs_special_inode_operations;
  47. static struct inode_operations btrfs_file_inode_operations;
  48. static struct address_space_operations btrfs_aops;
  49. static struct address_space_operations btrfs_symlink_aops;
  50. static struct file_operations btrfs_dir_file_operations;
  51. static struct kmem_cache *btrfs_inode_cachep;
  52. struct kmem_cache *btrfs_trans_handle_cachep;
  53. struct kmem_cache *btrfs_transaction_cachep;
  54. struct kmem_cache *btrfs_bit_radix_cachep;
  55. struct kmem_cache *btrfs_path_cachep;
  56. #define S_SHIFT 12
  57. static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
  58. [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
  59. [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
  60. [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
  61. [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
  62. [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
  63. [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
  64. [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
  65. };
  66. void btrfs_read_locked_inode(struct inode *inode)
  67. {
  68. struct btrfs_path *path;
  69. struct btrfs_inode_item *inode_item;
  70. struct btrfs_root *root = BTRFS_I(inode)->root;
  71. struct btrfs_key location;
  72. u64 alloc_group_block;
  73. u32 rdev;
  74. int ret;
  75. path = btrfs_alloc_path();
  76. BUG_ON(!path);
  77. mutex_lock(&root->fs_info->fs_mutex);
  78. memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
  79. ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
  80. if (ret) {
  81. btrfs_free_path(path);
  82. goto make_bad;
  83. }
  84. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  85. path->slots[0],
  86. struct btrfs_inode_item);
  87. inode->i_mode = btrfs_inode_mode(inode_item);
  88. inode->i_nlink = btrfs_inode_nlink(inode_item);
  89. inode->i_uid = btrfs_inode_uid(inode_item);
  90. inode->i_gid = btrfs_inode_gid(inode_item);
  91. inode->i_size = btrfs_inode_size(inode_item);
  92. inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
  93. inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
  94. inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
  95. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
  96. inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
  97. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
  98. inode->i_blocks = btrfs_inode_nblocks(inode_item);
  99. inode->i_generation = btrfs_inode_generation(inode_item);
  100. inode->i_rdev = 0;
  101. rdev = btrfs_inode_rdev(inode_item);
  102. alloc_group_block = btrfs_inode_block_group(inode_item);
  103. BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
  104. alloc_group_block);
  105. btrfs_free_path(path);
  106. inode_item = NULL;
  107. mutex_unlock(&root->fs_info->fs_mutex);
  108. switch (inode->i_mode & S_IFMT) {
  109. case S_IFREG:
  110. inode->i_mapping->a_ops = &btrfs_aops;
  111. inode->i_fop = &btrfs_file_operations;
  112. inode->i_op = &btrfs_file_inode_operations;
  113. break;
  114. case S_IFDIR:
  115. inode->i_fop = &btrfs_dir_file_operations;
  116. if (root == root->fs_info->tree_root)
  117. inode->i_op = &btrfs_dir_ro_inode_operations;
  118. else
  119. inode->i_op = &btrfs_dir_inode_operations;
  120. break;
  121. case S_IFLNK:
  122. inode->i_op = &btrfs_symlink_inode_operations;
  123. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  124. break;
  125. default:
  126. init_special_inode(inode, inode->i_mode, rdev);
  127. break;
  128. }
  129. return;
  130. make_bad:
  131. btrfs_release_path(root, path);
  132. btrfs_free_path(path);
  133. mutex_unlock(&root->fs_info->fs_mutex);
  134. make_bad_inode(inode);
  135. }
  136. static void fill_inode_item(struct btrfs_inode_item *item,
  137. struct inode *inode)
  138. {
  139. btrfs_set_inode_uid(item, inode->i_uid);
  140. btrfs_set_inode_gid(item, inode->i_gid);
  141. btrfs_set_inode_size(item, inode->i_size);
  142. btrfs_set_inode_mode(item, inode->i_mode);
  143. btrfs_set_inode_nlink(item, inode->i_nlink);
  144. btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
  145. btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
  146. btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
  147. btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
  148. btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
  149. btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
  150. btrfs_set_inode_nblocks(item, inode->i_blocks);
  151. btrfs_set_inode_generation(item, inode->i_generation);
  152. btrfs_set_inode_rdev(item, inode->i_rdev);
  153. btrfs_set_inode_block_group(item,
  154. BTRFS_I(inode)->block_group->key.objectid);
  155. }
  156. static int btrfs_update_inode(struct btrfs_trans_handle *trans,
  157. struct btrfs_root *root,
  158. struct inode *inode)
  159. {
  160. struct btrfs_inode_item *inode_item;
  161. struct btrfs_path *path;
  162. int ret;
  163. path = btrfs_alloc_path();
  164. BUG_ON(!path);
  165. ret = btrfs_lookup_inode(trans, root, path,
  166. &BTRFS_I(inode)->location, 1);
  167. if (ret) {
  168. if (ret > 0)
  169. ret = -ENOENT;
  170. goto failed;
  171. }
  172. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  173. path->slots[0],
  174. struct btrfs_inode_item);
  175. fill_inode_item(inode_item, inode);
  176. btrfs_mark_buffer_dirty(path->nodes[0]);
  177. ret = 0;
  178. failed:
  179. btrfs_release_path(root, path);
  180. btrfs_free_path(path);
  181. return ret;
  182. }
  183. static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
  184. struct btrfs_root *root,
  185. struct inode *dir,
  186. struct dentry *dentry)
  187. {
  188. struct btrfs_path *path;
  189. const char *name = dentry->d_name.name;
  190. int name_len = dentry->d_name.len;
  191. int ret = 0;
  192. u64 objectid;
  193. struct btrfs_dir_item *di;
  194. path = btrfs_alloc_path();
  195. if (!path) {
  196. ret = -ENOMEM;
  197. goto err;
  198. }
  199. di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  200. name, name_len, -1);
  201. if (IS_ERR(di)) {
  202. ret = PTR_ERR(di);
  203. goto err;
  204. }
  205. if (!di) {
  206. ret = -ENOENT;
  207. goto err;
  208. }
  209. objectid = btrfs_disk_key_objectid(&di->location);
  210. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  211. if (ret)
  212. goto err;
  213. btrfs_release_path(root, path);
  214. di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  215. objectid, name, name_len, -1);
  216. if (IS_ERR(di)) {
  217. ret = PTR_ERR(di);
  218. goto err;
  219. }
  220. if (!di) {
  221. ret = -ENOENT;
  222. goto err;
  223. }
  224. ret = btrfs_delete_one_dir_name(trans, root, path, di);
  225. dentry->d_inode->i_ctime = dir->i_ctime;
  226. err:
  227. btrfs_free_path(path);
  228. if (!ret) {
  229. dir->i_size -= name_len * 2;
  230. dir->i_mtime = dir->i_ctime = CURRENT_TIME;
  231. btrfs_update_inode(trans, root, dir);
  232. drop_nlink(dentry->d_inode);
  233. ret = btrfs_update_inode(trans, root, dentry->d_inode);
  234. dir->i_sb->s_dirt = 1;
  235. }
  236. return ret;
  237. }
  238. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  239. {
  240. struct btrfs_root *root;
  241. struct btrfs_trans_handle *trans;
  242. int ret;
  243. root = BTRFS_I(dir)->root;
  244. mutex_lock(&root->fs_info->fs_mutex);
  245. trans = btrfs_start_transaction(root, 1);
  246. btrfs_set_trans_block_group(trans, dir);
  247. ret = btrfs_unlink_trans(trans, root, dir, dentry);
  248. btrfs_end_transaction(trans, root);
  249. mutex_unlock(&root->fs_info->fs_mutex);
  250. btrfs_btree_balance_dirty(root);
  251. return ret;
  252. }
  253. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  254. {
  255. struct inode *inode = dentry->d_inode;
  256. int err;
  257. int ret;
  258. struct btrfs_root *root = BTRFS_I(dir)->root;
  259. struct btrfs_path *path;
  260. struct btrfs_key key;
  261. struct btrfs_trans_handle *trans;
  262. struct btrfs_key found_key;
  263. int found_type;
  264. struct btrfs_leaf *leaf;
  265. char *goodnames = "..";
  266. path = btrfs_alloc_path();
  267. BUG_ON(!path);
  268. mutex_lock(&root->fs_info->fs_mutex);
  269. trans = btrfs_start_transaction(root, 1);
  270. btrfs_set_trans_block_group(trans, dir);
  271. key.objectid = inode->i_ino;
  272. key.offset = (u64)-1;
  273. key.flags = (u32)-1;
  274. while(1) {
  275. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  276. if (ret < 0) {
  277. err = ret;
  278. goto out;
  279. }
  280. BUG_ON(ret == 0);
  281. if (path->slots[0] == 0) {
  282. err = -ENOENT;
  283. goto out;
  284. }
  285. path->slots[0]--;
  286. leaf = btrfs_buffer_leaf(path->nodes[0]);
  287. btrfs_disk_key_to_cpu(&found_key,
  288. &leaf->items[path->slots[0]].key);
  289. found_type = btrfs_key_type(&found_key);
  290. if (found_key.objectid != inode->i_ino) {
  291. err = -ENOENT;
  292. goto out;
  293. }
  294. if ((found_type != BTRFS_DIR_ITEM_KEY &&
  295. found_type != BTRFS_DIR_INDEX_KEY) ||
  296. (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
  297. !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
  298. err = -ENOTEMPTY;
  299. goto out;
  300. }
  301. ret = btrfs_del_item(trans, root, path);
  302. BUG_ON(ret);
  303. if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
  304. break;
  305. btrfs_release_path(root, path);
  306. }
  307. ret = 0;
  308. btrfs_release_path(root, path);
  309. /* now the directory is empty */
  310. err = btrfs_unlink_trans(trans, root, dir, dentry);
  311. if (!err) {
  312. inode->i_size = 0;
  313. }
  314. out:
  315. btrfs_release_path(root, path);
  316. btrfs_free_path(path);
  317. mutex_unlock(&root->fs_info->fs_mutex);
  318. ret = btrfs_end_transaction(trans, root);
  319. btrfs_btree_balance_dirty(root);
  320. if (ret && !err)
  321. err = ret;
  322. return err;
  323. }
  324. static int btrfs_free_inode(struct btrfs_trans_handle *trans,
  325. struct btrfs_root *root,
  326. struct inode *inode)
  327. {
  328. struct btrfs_path *path;
  329. int ret;
  330. clear_inode(inode);
  331. path = btrfs_alloc_path();
  332. BUG_ON(!path);
  333. ret = btrfs_lookup_inode(trans, root, path,
  334. &BTRFS_I(inode)->location, -1);
  335. if (ret > 0)
  336. ret = -ENOENT;
  337. if (!ret)
  338. ret = btrfs_del_item(trans, root, path);
  339. btrfs_free_path(path);
  340. return ret;
  341. }
  342. /*
  343. * truncates go from a high offset to a low offset. So, walk
  344. * from hi to lo in the node and issue readas. Stop when you find
  345. * keys from a different objectid
  346. */
  347. static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path,
  348. u64 objectid)
  349. {
  350. struct btrfs_node *node;
  351. int i;
  352. int nritems;
  353. u64 item_objectid;
  354. u64 blocknr;
  355. int slot;
  356. int ret;
  357. if (!path->nodes[1])
  358. return;
  359. node = btrfs_buffer_node(path->nodes[1]);
  360. slot = path->slots[1];
  361. if (slot == 0)
  362. return;
  363. nritems = btrfs_header_nritems(&node->header);
  364. for (i = slot - 1; i >= 0; i--) {
  365. item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
  366. if (item_objectid != objectid)
  367. break;
  368. blocknr = btrfs_node_blockptr(node, i);
  369. ret = readahead_tree_block(root, blocknr);
  370. if (ret)
  371. break;
  372. }
  373. }
  374. /*
  375. * this can truncate away extent items, csum items and directory items.
  376. * It starts at a high offset and removes keys until it can't find
  377. * any higher than i_size.
  378. *
  379. * csum items that cross the new i_size are truncated to the new size
  380. * as well.
  381. */
  382. static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
  383. struct btrfs_root *root,
  384. struct inode *inode)
  385. {
  386. int ret;
  387. struct btrfs_path *path;
  388. struct btrfs_key key;
  389. struct btrfs_disk_key *found_key;
  390. u32 found_type;
  391. struct btrfs_leaf *leaf;
  392. struct btrfs_file_extent_item *fi;
  393. u64 extent_start = 0;
  394. u64 extent_num_blocks = 0;
  395. u64 item_end = 0;
  396. int found_extent;
  397. int del_item;
  398. path = btrfs_alloc_path();
  399. BUG_ON(!path);
  400. /* FIXME, add redo link to tree so we don't leak on crash */
  401. key.objectid = inode->i_ino;
  402. key.offset = (u64)-1;
  403. key.flags = (u32)-1;
  404. while(1) {
  405. btrfs_init_path(path);
  406. fi = NULL;
  407. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  408. if (ret < 0) {
  409. goto error;
  410. }
  411. if (ret > 0) {
  412. BUG_ON(path->slots[0] == 0);
  413. path->slots[0]--;
  414. }
  415. reada_truncate(root, path, inode->i_ino);
  416. leaf = btrfs_buffer_leaf(path->nodes[0]);
  417. found_key = &leaf->items[path->slots[0]].key;
  418. found_type = btrfs_disk_key_type(found_key);
  419. if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
  420. break;
  421. if (found_type != BTRFS_CSUM_ITEM_KEY &&
  422. found_type != BTRFS_DIR_ITEM_KEY &&
  423. found_type != BTRFS_DIR_INDEX_KEY &&
  424. found_type != BTRFS_EXTENT_DATA_KEY)
  425. break;
  426. item_end = btrfs_disk_key_offset(found_key);
  427. if (found_type == BTRFS_EXTENT_DATA_KEY) {
  428. fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  429. path->slots[0],
  430. struct btrfs_file_extent_item);
  431. if (btrfs_file_extent_type(fi) !=
  432. BTRFS_FILE_EXTENT_INLINE) {
  433. item_end += btrfs_file_extent_num_blocks(fi) <<
  434. inode->i_blkbits;
  435. }
  436. }
  437. if (found_type == BTRFS_CSUM_ITEM_KEY) {
  438. ret = btrfs_csum_truncate(trans, root, path,
  439. inode->i_size);
  440. BUG_ON(ret);
  441. }
  442. if (item_end < inode->i_size) {
  443. if (found_type) {
  444. btrfs_set_key_type(&key, found_type - 1);
  445. continue;
  446. }
  447. break;
  448. }
  449. if (btrfs_disk_key_offset(found_key) >= inode->i_size)
  450. del_item = 1;
  451. else
  452. del_item = 0;
  453. found_extent = 0;
  454. /* FIXME, shrink the extent if the ref count is only 1 */
  455. if (found_type == BTRFS_EXTENT_DATA_KEY &&
  456. btrfs_file_extent_type(fi) !=
  457. BTRFS_FILE_EXTENT_INLINE) {
  458. u64 num_dec;
  459. if (!del_item) {
  460. u64 orig_num_blocks =
  461. btrfs_file_extent_num_blocks(fi);
  462. extent_num_blocks = inode->i_size -
  463. btrfs_disk_key_offset(found_key) +
  464. root->blocksize - 1;
  465. extent_num_blocks >>= inode->i_blkbits;
  466. btrfs_set_file_extent_num_blocks(fi,
  467. extent_num_blocks);
  468. inode->i_blocks -= (orig_num_blocks -
  469. extent_num_blocks) << 3;
  470. btrfs_mark_buffer_dirty(path->nodes[0]);
  471. } else {
  472. extent_start =
  473. btrfs_file_extent_disk_blocknr(fi);
  474. extent_num_blocks =
  475. btrfs_file_extent_disk_num_blocks(fi);
  476. /* FIXME blocksize != 4096 */
  477. num_dec = btrfs_file_extent_num_blocks(fi) << 3;
  478. if (extent_start != 0) {
  479. found_extent = 1;
  480. inode->i_blocks -= num_dec;
  481. }
  482. }
  483. }
  484. if (del_item) {
  485. ret = btrfs_del_item(trans, root, path);
  486. if (ret)
  487. goto error;
  488. } else {
  489. break;
  490. }
  491. btrfs_release_path(root, path);
  492. if (found_extent) {
  493. ret = btrfs_free_extent(trans, root, extent_start,
  494. extent_num_blocks, 0);
  495. BUG_ON(ret);
  496. }
  497. }
  498. ret = 0;
  499. error:
  500. btrfs_release_path(root, path);
  501. btrfs_free_path(path);
  502. inode->i_sb->s_dirt = 1;
  503. return ret;
  504. }
  505. /*
  506. * taken from block_truncate_page, but does cow as it zeros out
  507. * any bytes left in the last page in the file.
  508. */
  509. static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
  510. {
  511. struct inode *inode = mapping->host;
  512. unsigned blocksize = 1 << inode->i_blkbits;
  513. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  514. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  515. struct page *page;
  516. char *kaddr;
  517. int ret = 0;
  518. struct btrfs_root *root = BTRFS_I(inode)->root;
  519. u64 alloc_hint = 0;
  520. struct btrfs_key ins;
  521. struct btrfs_trans_handle *trans;
  522. if ((offset & (blocksize - 1)) == 0)
  523. goto out;
  524. ret = -ENOMEM;
  525. page = grab_cache_page(mapping, index);
  526. if (!page)
  527. goto out;
  528. if (!PageUptodate(page)) {
  529. ret = btrfs_readpage(NULL, page);
  530. lock_page(page);
  531. if (!PageUptodate(page)) {
  532. ret = -EIO;
  533. goto out;
  534. }
  535. }
  536. mutex_lock(&root->fs_info->fs_mutex);
  537. trans = btrfs_start_transaction(root, 1);
  538. btrfs_set_trans_block_group(trans, inode);
  539. ret = btrfs_drop_extents(trans, root, inode,
  540. page->index << PAGE_CACHE_SHIFT,
  541. (page->index + 1) << PAGE_CACHE_SHIFT,
  542. &alloc_hint);
  543. if (ret)
  544. goto out;
  545. ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1,
  546. alloc_hint, (u64)-1, &ins, 1);
  547. if (ret)
  548. goto out;
  549. ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
  550. page->index << PAGE_CACHE_SHIFT,
  551. ins.objectid, 1, 1);
  552. if (ret)
  553. goto out;
  554. SetPageChecked(page);
  555. kaddr = kmap(page);
  556. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  557. flush_dcache_page(page);
  558. ret = btrfs_csum_file_block(trans, root, inode->i_ino,
  559. page->index << PAGE_CACHE_SHIFT,
  560. kaddr, PAGE_CACHE_SIZE);
  561. kunmap(page);
  562. btrfs_end_transaction(trans, root);
  563. mutex_unlock(&root->fs_info->fs_mutex);
  564. set_page_dirty(page);
  565. unlock_page(page);
  566. page_cache_release(page);
  567. out:
  568. return ret;
  569. }
  570. static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
  571. {
  572. struct inode *inode = dentry->d_inode;
  573. int err;
  574. err = inode_change_ok(inode, attr);
  575. if (err)
  576. return err;
  577. if (S_ISREG(inode->i_mode) &&
  578. attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
  579. struct btrfs_trans_handle *trans;
  580. struct btrfs_root *root = BTRFS_I(inode)->root;
  581. u64 mask = root->blocksize - 1;
  582. u64 pos = (inode->i_size + mask) & ~mask;
  583. u64 hole_size;
  584. if (attr->ia_size <= pos)
  585. goto out;
  586. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  587. hole_size = (attr->ia_size - pos + mask) & ~mask;
  588. hole_size >>= inode->i_blkbits;
  589. mutex_lock(&root->fs_info->fs_mutex);
  590. trans = btrfs_start_transaction(root, 1);
  591. btrfs_set_trans_block_group(trans, inode);
  592. err = btrfs_insert_file_extent(trans, root, inode->i_ino,
  593. pos, 0, 0, hole_size);
  594. btrfs_end_transaction(trans, root);
  595. mutex_unlock(&root->fs_info->fs_mutex);
  596. if (err)
  597. return err;
  598. }
  599. out:
  600. err = inode_setattr(inode, attr);
  601. return err;
  602. }
  603. void btrfs_delete_inode(struct inode *inode)
  604. {
  605. struct btrfs_trans_handle *trans;
  606. struct btrfs_root *root = BTRFS_I(inode)->root;
  607. int ret;
  608. truncate_inode_pages(&inode->i_data, 0);
  609. if (is_bad_inode(inode)) {
  610. goto no_delete;
  611. }
  612. inode->i_size = 0;
  613. mutex_lock(&root->fs_info->fs_mutex);
  614. trans = btrfs_start_transaction(root, 1);
  615. btrfs_set_trans_block_group(trans, inode);
  616. ret = btrfs_truncate_in_trans(trans, root, inode);
  617. if (ret)
  618. goto no_delete_lock;
  619. ret = btrfs_free_inode(trans, root, inode);
  620. if (ret)
  621. goto no_delete_lock;
  622. btrfs_end_transaction(trans, root);
  623. mutex_unlock(&root->fs_info->fs_mutex);
  624. btrfs_btree_balance_dirty(root);
  625. return;
  626. no_delete_lock:
  627. btrfs_end_transaction(trans, root);
  628. mutex_unlock(&root->fs_info->fs_mutex);
  629. btrfs_btree_balance_dirty(root);
  630. no_delete:
  631. clear_inode(inode);
  632. }
  633. /*
  634. * this returns the key found in the dir entry in the location pointer.
  635. * If no dir entries were found, location->objectid is 0.
  636. */
  637. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  638. struct btrfs_key *location)
  639. {
  640. const char *name = dentry->d_name.name;
  641. int namelen = dentry->d_name.len;
  642. struct btrfs_dir_item *di;
  643. struct btrfs_path *path;
  644. struct btrfs_root *root = BTRFS_I(dir)->root;
  645. int ret;
  646. path = btrfs_alloc_path();
  647. BUG_ON(!path);
  648. di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  649. namelen, 0);
  650. if (!di || IS_ERR(di)) {
  651. location->objectid = 0;
  652. ret = 0;
  653. goto out;
  654. }
  655. btrfs_disk_key_to_cpu(location, &di->location);
  656. out:
  657. btrfs_release_path(root, path);
  658. btrfs_free_path(path);
  659. return ret;
  660. }
  661. /*
  662. * when we hit a tree root in a directory, the btrfs part of the inode
  663. * needs to be changed to reflect the root directory of the tree root. This
  664. * is kind of like crossing a mount point.
  665. */
  666. static int fixup_tree_root_location(struct btrfs_root *root,
  667. struct btrfs_key *location,
  668. struct btrfs_root **sub_root)
  669. {
  670. struct btrfs_path *path;
  671. struct btrfs_root_item *ri;
  672. if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
  673. return 0;
  674. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  675. return 0;
  676. path = btrfs_alloc_path();
  677. BUG_ON(!path);
  678. mutex_lock(&root->fs_info->fs_mutex);
  679. *sub_root = btrfs_read_fs_root(root->fs_info, location);
  680. if (IS_ERR(*sub_root))
  681. return PTR_ERR(*sub_root);
  682. ri = &(*sub_root)->root_item;
  683. location->objectid = btrfs_root_dirid(ri);
  684. location->flags = 0;
  685. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  686. location->offset = 0;
  687. btrfs_free_path(path);
  688. mutex_unlock(&root->fs_info->fs_mutex);
  689. return 0;
  690. }
  691. static int btrfs_init_locked_inode(struct inode *inode, void *p)
  692. {
  693. struct btrfs_iget_args *args = p;
  694. inode->i_ino = args->ino;
  695. BTRFS_I(inode)->root = args->root;
  696. return 0;
  697. }
  698. static int btrfs_find_actor(struct inode *inode, void *opaque)
  699. {
  700. struct btrfs_iget_args *args = opaque;
  701. return (args->ino == inode->i_ino &&
  702. args->root == BTRFS_I(inode)->root);
  703. }
  704. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  705. struct btrfs_root *root)
  706. {
  707. struct inode *inode;
  708. struct btrfs_iget_args args;
  709. args.ino = objectid;
  710. args.root = root;
  711. inode = iget5_locked(s, objectid, btrfs_find_actor,
  712. btrfs_init_locked_inode,
  713. (void *)&args);
  714. return inode;
  715. }
  716. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  717. struct nameidata *nd)
  718. {
  719. struct inode * inode;
  720. struct btrfs_inode *bi = BTRFS_I(dir);
  721. struct btrfs_root *root = bi->root;
  722. struct btrfs_root *sub_root = root;
  723. struct btrfs_key location;
  724. int ret;
  725. if (dentry->d_name.len > BTRFS_NAME_LEN)
  726. return ERR_PTR(-ENAMETOOLONG);
  727. mutex_lock(&root->fs_info->fs_mutex);
  728. ret = btrfs_inode_by_name(dir, dentry, &location);
  729. mutex_unlock(&root->fs_info->fs_mutex);
  730. if (ret < 0)
  731. return ERR_PTR(ret);
  732. inode = NULL;
  733. if (location.objectid) {
  734. ret = fixup_tree_root_location(root, &location, &sub_root);
  735. if (ret < 0)
  736. return ERR_PTR(ret);
  737. if (ret > 0)
  738. return ERR_PTR(-ENOENT);
  739. inode = btrfs_iget_locked(dir->i_sb, location.objectid,
  740. sub_root);
  741. if (!inode)
  742. return ERR_PTR(-EACCES);
  743. if (inode->i_state & I_NEW) {
  744. /* the inode and parent dir are two different roots */
  745. if (sub_root != root) {
  746. igrab(inode);
  747. sub_root->inode = inode;
  748. }
  749. BTRFS_I(inode)->root = sub_root;
  750. memcpy(&BTRFS_I(inode)->location, &location,
  751. sizeof(location));
  752. btrfs_read_locked_inode(inode);
  753. unlock_new_inode(inode);
  754. }
  755. }
  756. return d_splice_alias(inode, dentry);
  757. }
  758. /*
  759. * readahead one full node of leaves as long as their keys include
  760. * the objectid supplied
  761. */
  762. static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path,
  763. u64 objectid)
  764. {
  765. struct btrfs_node *node;
  766. int i;
  767. u32 nritems;
  768. u64 item_objectid;
  769. u64 blocknr;
  770. int slot;
  771. int ret;
  772. if (!path->nodes[1])
  773. return;
  774. node = btrfs_buffer_node(path->nodes[1]);
  775. slot = path->slots[1];
  776. nritems = btrfs_header_nritems(&node->header);
  777. for (i = slot + 1; i < nritems; i++) {
  778. item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
  779. if (item_objectid != objectid)
  780. break;
  781. blocknr = btrfs_node_blockptr(node, i);
  782. ret = readahead_tree_block(root, blocknr);
  783. if (ret)
  784. break;
  785. }
  786. }
  787. static unsigned char btrfs_filetype_table[] = {
  788. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  789. };
  790. static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  791. {
  792. struct inode *inode = filp->f_path.dentry->d_inode;
  793. struct btrfs_root *root = BTRFS_I(inode)->root;
  794. struct btrfs_item *item;
  795. struct btrfs_dir_item *di;
  796. struct btrfs_key key;
  797. struct btrfs_path *path;
  798. int ret;
  799. u32 nritems;
  800. struct btrfs_leaf *leaf;
  801. int slot;
  802. int advance;
  803. unsigned char d_type;
  804. int over = 0;
  805. u32 di_cur;
  806. u32 di_total;
  807. u32 di_len;
  808. int key_type = BTRFS_DIR_INDEX_KEY;
  809. /* FIXME, use a real flag for deciding about the key type */
  810. if (root->fs_info->tree_root == root)
  811. key_type = BTRFS_DIR_ITEM_KEY;
  812. mutex_lock(&root->fs_info->fs_mutex);
  813. key.objectid = inode->i_ino;
  814. key.flags = 0;
  815. btrfs_set_key_type(&key, key_type);
  816. key.offset = filp->f_pos;
  817. path = btrfs_alloc_path();
  818. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  819. if (ret < 0)
  820. goto err;
  821. advance = 0;
  822. reada_leaves(root, path, inode->i_ino);
  823. while(1) {
  824. leaf = btrfs_buffer_leaf(path->nodes[0]);
  825. nritems = btrfs_header_nritems(&leaf->header);
  826. slot = path->slots[0];
  827. if (advance || slot >= nritems) {
  828. if (slot >= nritems -1) {
  829. reada_leaves(root, path, inode->i_ino);
  830. ret = btrfs_next_leaf(root, path);
  831. if (ret)
  832. break;
  833. leaf = btrfs_buffer_leaf(path->nodes[0]);
  834. nritems = btrfs_header_nritems(&leaf->header);
  835. slot = path->slots[0];
  836. } else {
  837. slot++;
  838. path->slots[0]++;
  839. }
  840. }
  841. advance = 1;
  842. item = leaf->items + slot;
  843. if (btrfs_disk_key_objectid(&item->key) != key.objectid)
  844. break;
  845. if (btrfs_disk_key_type(&item->key) != key_type)
  846. break;
  847. if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
  848. continue;
  849. filp->f_pos = btrfs_disk_key_offset(&item->key);
  850. advance = 1;
  851. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  852. di_cur = 0;
  853. di_total = btrfs_item_size(leaf->items + slot);
  854. while(di_cur < di_total) {
  855. d_type = btrfs_filetype_table[btrfs_dir_type(di)];
  856. over = filldir(dirent, (const char *)(di + 1),
  857. btrfs_dir_name_len(di),
  858. btrfs_disk_key_offset(&item->key),
  859. btrfs_disk_key_objectid(&di->location),
  860. d_type);
  861. if (over)
  862. goto nopos;
  863. di_len = btrfs_dir_name_len(di) + sizeof(*di);
  864. di_cur += di_len;
  865. di = (struct btrfs_dir_item *)((char *)di + di_len);
  866. }
  867. }
  868. filp->f_pos++;
  869. nopos:
  870. ret = 0;
  871. err:
  872. btrfs_release_path(root, path);
  873. btrfs_free_path(path);
  874. mutex_unlock(&root->fs_info->fs_mutex);
  875. return ret;
  876. }
  877. int btrfs_write_inode(struct inode *inode, int wait)
  878. {
  879. struct btrfs_root *root = BTRFS_I(inode)->root;
  880. struct btrfs_trans_handle *trans;
  881. int ret = 0;
  882. if (wait) {
  883. mutex_lock(&root->fs_info->fs_mutex);
  884. trans = btrfs_start_transaction(root, 1);
  885. btrfs_set_trans_block_group(trans, inode);
  886. ret = btrfs_commit_transaction(trans, root);
  887. mutex_unlock(&root->fs_info->fs_mutex);
  888. }
  889. return ret;
  890. }
  891. /*
  892. * This is somewhat expensive, updating the tree every time the
  893. * inode changes. But, it is most likely to find the inode in cache.
  894. * FIXME, needs more benchmarking...there are no reasons other than performance
  895. * to keep or drop this code.
  896. */
  897. void btrfs_dirty_inode(struct inode *inode)
  898. {
  899. struct btrfs_root *root = BTRFS_I(inode)->root;
  900. struct btrfs_trans_handle *trans;
  901. mutex_lock(&root->fs_info->fs_mutex);
  902. trans = btrfs_start_transaction(root, 1);
  903. btrfs_set_trans_block_group(trans, inode);
  904. btrfs_update_inode(trans, root, inode);
  905. btrfs_end_transaction(trans, root);
  906. mutex_unlock(&root->fs_info->fs_mutex);
  907. }
  908. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  909. struct btrfs_root *root,
  910. u64 objectid,
  911. struct btrfs_block_group_cache *group,
  912. int mode)
  913. {
  914. struct inode *inode;
  915. struct btrfs_inode_item inode_item;
  916. struct btrfs_key *location;
  917. int ret;
  918. int owner;
  919. inode = new_inode(root->fs_info->sb);
  920. if (!inode)
  921. return ERR_PTR(-ENOMEM);
  922. BTRFS_I(inode)->root = root;
  923. if (mode & S_IFDIR)
  924. owner = 0;
  925. else
  926. owner = 1;
  927. group = btrfs_find_block_group(root, group, 0, 0, owner);
  928. BTRFS_I(inode)->block_group = group;
  929. inode->i_uid = current->fsuid;
  930. inode->i_gid = current->fsgid;
  931. inode->i_mode = mode;
  932. inode->i_ino = objectid;
  933. inode->i_blocks = 0;
  934. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  935. fill_inode_item(&inode_item, inode);
  936. location = &BTRFS_I(inode)->location;
  937. location->objectid = objectid;
  938. location->flags = 0;
  939. location->offset = 0;
  940. btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
  941. ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
  942. if (ret)
  943. return ERR_PTR(ret);
  944. insert_inode_hash(inode);
  945. return inode;
  946. }
  947. static inline u8 btrfs_inode_type(struct inode *inode)
  948. {
  949. return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
  950. }
  951. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  952. struct dentry *dentry, struct inode *inode)
  953. {
  954. int ret;
  955. struct btrfs_key key;
  956. struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
  957. struct inode *parent_inode;
  958. key.objectid = inode->i_ino;
  959. key.flags = 0;
  960. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  961. key.offset = 0;
  962. ret = btrfs_insert_dir_item(trans, root,
  963. dentry->d_name.name, dentry->d_name.len,
  964. dentry->d_parent->d_inode->i_ino,
  965. &key, btrfs_inode_type(inode));
  966. if (ret == 0) {
  967. parent_inode = dentry->d_parent->d_inode;
  968. parent_inode->i_size += dentry->d_name.len * 2;
  969. parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
  970. ret = btrfs_update_inode(trans, root,
  971. dentry->d_parent->d_inode);
  972. }
  973. return ret;
  974. }
  975. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  976. struct dentry *dentry, struct inode *inode)
  977. {
  978. int err = btrfs_add_link(trans, dentry, inode);
  979. if (!err) {
  980. d_instantiate(dentry, inode);
  981. return 0;
  982. }
  983. if (err > 0)
  984. err = -EEXIST;
  985. return err;
  986. }
  987. static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
  988. int mode, dev_t rdev)
  989. {
  990. struct btrfs_trans_handle *trans;
  991. struct btrfs_root *root = BTRFS_I(dir)->root;
  992. struct inode *inode;
  993. int err;
  994. int drop_inode = 0;
  995. u64 objectid;
  996. if (!new_valid_dev(rdev))
  997. return -EINVAL;
  998. mutex_lock(&root->fs_info->fs_mutex);
  999. trans = btrfs_start_transaction(root, 1);
  1000. btrfs_set_trans_block_group(trans, dir);
  1001. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  1002. if (err) {
  1003. err = -ENOSPC;
  1004. goto out_unlock;
  1005. }
  1006. inode = btrfs_new_inode(trans, root, objectid,
  1007. BTRFS_I(dir)->block_group, mode);
  1008. err = PTR_ERR(inode);
  1009. if (IS_ERR(inode))
  1010. goto out_unlock;
  1011. btrfs_set_trans_block_group(trans, inode);
  1012. err = btrfs_add_nondir(trans, dentry, inode);
  1013. if (err)
  1014. drop_inode = 1;
  1015. else {
  1016. inode->i_op = &btrfs_special_inode_operations;
  1017. init_special_inode(inode, inode->i_mode, rdev);
  1018. }
  1019. dir->i_sb->s_dirt = 1;
  1020. btrfs_update_inode_block_group(trans, inode);
  1021. btrfs_update_inode_block_group(trans, dir);
  1022. out_unlock:
  1023. btrfs_end_transaction(trans, root);
  1024. mutex_unlock(&root->fs_info->fs_mutex);
  1025. if (drop_inode) {
  1026. inode_dec_link_count(inode);
  1027. iput(inode);
  1028. }
  1029. btrfs_btree_balance_dirty(root);
  1030. return err;
  1031. }
  1032. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  1033. int mode, struct nameidata *nd)
  1034. {
  1035. struct btrfs_trans_handle *trans;
  1036. struct btrfs_root *root = BTRFS_I(dir)->root;
  1037. struct inode *inode;
  1038. int err;
  1039. int drop_inode = 0;
  1040. u64 objectid;
  1041. mutex_lock(&root->fs_info->fs_mutex);
  1042. trans = btrfs_start_transaction(root, 1);
  1043. btrfs_set_trans_block_group(trans, dir);
  1044. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  1045. if (err) {
  1046. err = -ENOSPC;
  1047. goto out_unlock;
  1048. }
  1049. inode = btrfs_new_inode(trans, root, objectid,
  1050. BTRFS_I(dir)->block_group, mode);
  1051. err = PTR_ERR(inode);
  1052. if (IS_ERR(inode))
  1053. goto out_unlock;
  1054. btrfs_set_trans_block_group(trans, inode);
  1055. err = btrfs_add_nondir(trans, dentry, inode);
  1056. if (err)
  1057. drop_inode = 1;
  1058. else {
  1059. inode->i_mapping->a_ops = &btrfs_aops;
  1060. inode->i_fop = &btrfs_file_operations;
  1061. inode->i_op = &btrfs_file_inode_operations;
  1062. }
  1063. dir->i_sb->s_dirt = 1;
  1064. btrfs_update_inode_block_group(trans, inode);
  1065. btrfs_update_inode_block_group(trans, dir);
  1066. out_unlock:
  1067. btrfs_end_transaction(trans, root);
  1068. mutex_unlock(&root->fs_info->fs_mutex);
  1069. if (drop_inode) {
  1070. inode_dec_link_count(inode);
  1071. iput(inode);
  1072. }
  1073. btrfs_btree_balance_dirty(root);
  1074. return err;
  1075. }
  1076. static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
  1077. struct dentry *dentry)
  1078. {
  1079. struct btrfs_trans_handle *trans;
  1080. struct btrfs_root *root = BTRFS_I(dir)->root;
  1081. struct inode *inode = old_dentry->d_inode;
  1082. int err;
  1083. int drop_inode = 0;
  1084. if (inode->i_nlink == 0)
  1085. return -ENOENT;
  1086. inc_nlink(inode);
  1087. mutex_lock(&root->fs_info->fs_mutex);
  1088. trans = btrfs_start_transaction(root, 1);
  1089. btrfs_set_trans_block_group(trans, dir);
  1090. atomic_inc(&inode->i_count);
  1091. err = btrfs_add_nondir(trans, dentry, inode);
  1092. if (err)
  1093. drop_inode = 1;
  1094. dir->i_sb->s_dirt = 1;
  1095. btrfs_update_inode_block_group(trans, dir);
  1096. err = btrfs_update_inode(trans, root, inode);
  1097. if (err)
  1098. drop_inode = 1;
  1099. btrfs_end_transaction(trans, root);
  1100. mutex_unlock(&root->fs_info->fs_mutex);
  1101. if (drop_inode) {
  1102. inode_dec_link_count(inode);
  1103. iput(inode);
  1104. }
  1105. btrfs_btree_balance_dirty(root);
  1106. return err;
  1107. }
  1108. static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
  1109. struct btrfs_root *root,
  1110. u64 objectid, u64 dirid)
  1111. {
  1112. int ret;
  1113. char buf[2];
  1114. struct btrfs_key key;
  1115. buf[0] = '.';
  1116. buf[1] = '.';
  1117. key.objectid = objectid;
  1118. key.offset = 0;
  1119. key.flags = 0;
  1120. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  1121. ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
  1122. &key, BTRFS_FT_DIR);
  1123. if (ret)
  1124. goto error;
  1125. key.objectid = dirid;
  1126. ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
  1127. &key, BTRFS_FT_DIR);
  1128. if (ret)
  1129. goto error;
  1130. error:
  1131. return ret;
  1132. }
  1133. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1134. {
  1135. struct inode *inode;
  1136. struct btrfs_trans_handle *trans;
  1137. struct btrfs_root *root = BTRFS_I(dir)->root;
  1138. int err = 0;
  1139. int drop_on_err = 0;
  1140. u64 objectid;
  1141. mutex_lock(&root->fs_info->fs_mutex);
  1142. trans = btrfs_start_transaction(root, 1);
  1143. btrfs_set_trans_block_group(trans, dir);
  1144. if (IS_ERR(trans)) {
  1145. err = PTR_ERR(trans);
  1146. goto out_unlock;
  1147. }
  1148. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  1149. if (err) {
  1150. err = -ENOSPC;
  1151. goto out_unlock;
  1152. }
  1153. inode = btrfs_new_inode(trans, root, objectid,
  1154. BTRFS_I(dir)->block_group, S_IFDIR | mode);
  1155. if (IS_ERR(inode)) {
  1156. err = PTR_ERR(inode);
  1157. goto out_fail;
  1158. }
  1159. drop_on_err = 1;
  1160. inode->i_op = &btrfs_dir_inode_operations;
  1161. inode->i_fop = &btrfs_dir_file_operations;
  1162. btrfs_set_trans_block_group(trans, inode);
  1163. err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
  1164. if (err)
  1165. goto out_fail;
  1166. inode->i_size = 6;
  1167. err = btrfs_update_inode(trans, root, inode);
  1168. if (err)
  1169. goto out_fail;
  1170. err = btrfs_add_link(trans, dentry, inode);
  1171. if (err)
  1172. goto out_fail;
  1173. d_instantiate(dentry, inode);
  1174. drop_on_err = 0;
  1175. dir->i_sb->s_dirt = 1;
  1176. btrfs_update_inode_block_group(trans, inode);
  1177. btrfs_update_inode_block_group(trans, dir);
  1178. out_fail:
  1179. btrfs_end_transaction(trans, root);
  1180. out_unlock:
  1181. mutex_unlock(&root->fs_info->fs_mutex);
  1182. if (drop_on_err)
  1183. iput(inode);
  1184. btrfs_btree_balance_dirty(root);
  1185. return err;
  1186. }
  1187. /*
  1188. * FIBMAP and others want to pass in a fake buffer head. They need to
  1189. * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy
  1190. * any packed file data into the fake bh
  1191. */
  1192. #define BTRFS_GET_BLOCK_NO_CREATE 0
  1193. #define BTRFS_GET_BLOCK_CREATE 1
  1194. #define BTRFS_GET_BLOCK_NO_DIRECT 2
  1195. /*
  1196. * FIXME create==1 doe not work.
  1197. */
  1198. static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
  1199. struct buffer_head *result, int create)
  1200. {
  1201. int ret;
  1202. int err = 0;
  1203. u64 blocknr;
  1204. u64 extent_start = 0;
  1205. u64 extent_end = 0;
  1206. u64 objectid = inode->i_ino;
  1207. u32 found_type;
  1208. u64 alloc_hint = 0;
  1209. struct btrfs_path *path;
  1210. struct btrfs_root *root = BTRFS_I(inode)->root;
  1211. struct btrfs_file_extent_item *item;
  1212. struct btrfs_leaf *leaf;
  1213. struct btrfs_disk_key *found_key;
  1214. struct btrfs_trans_handle *trans = NULL;
  1215. path = btrfs_alloc_path();
  1216. BUG_ON(!path);
  1217. if (create & BTRFS_GET_BLOCK_CREATE) {
  1218. /*
  1219. * danger!, this only works if the page is properly up
  1220. * to date somehow
  1221. */
  1222. trans = btrfs_start_transaction(root, 1);
  1223. if (!trans) {
  1224. err = -ENOMEM;
  1225. goto out;
  1226. }
  1227. ret = btrfs_drop_extents(trans, root, inode,
  1228. iblock << inode->i_blkbits,
  1229. (iblock + 1) << inode->i_blkbits,
  1230. &alloc_hint);
  1231. BUG_ON(ret);
  1232. }
  1233. ret = btrfs_lookup_file_extent(NULL, root, path,
  1234. objectid,
  1235. iblock << inode->i_blkbits, 0);
  1236. if (ret < 0) {
  1237. err = ret;
  1238. goto out;
  1239. }
  1240. if (ret != 0) {
  1241. if (path->slots[0] == 0) {
  1242. btrfs_release_path(root, path);
  1243. goto not_found;
  1244. }
  1245. path->slots[0]--;
  1246. }
  1247. item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  1248. struct btrfs_file_extent_item);
  1249. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1250. blocknr = btrfs_file_extent_disk_blocknr(item);
  1251. blocknr += btrfs_file_extent_offset(item);
  1252. /* are we inside the extent that was found? */
  1253. found_key = &leaf->items[path->slots[0]].key;
  1254. found_type = btrfs_disk_key_type(found_key);
  1255. if (btrfs_disk_key_objectid(found_key) != objectid ||
  1256. found_type != BTRFS_EXTENT_DATA_KEY) {
  1257. extent_end = 0;
  1258. extent_start = 0;
  1259. goto not_found;
  1260. }
  1261. found_type = btrfs_file_extent_type(item);
  1262. extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
  1263. if (found_type == BTRFS_FILE_EXTENT_REG) {
  1264. extent_start = extent_start >> inode->i_blkbits;
  1265. extent_end = extent_start + btrfs_file_extent_num_blocks(item);
  1266. err = 0;
  1267. if (btrfs_file_extent_disk_blocknr(item) == 0)
  1268. goto out;
  1269. if (iblock >= extent_start && iblock < extent_end) {
  1270. btrfs_map_bh_to_logical(root, result, blocknr +
  1271. iblock - extent_start);
  1272. goto out;
  1273. }
  1274. } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
  1275. char *ptr;
  1276. char *map;
  1277. u32 size;
  1278. if (create & BTRFS_GET_BLOCK_NO_DIRECT) {
  1279. err = -EINVAL;
  1280. goto out;
  1281. }
  1282. size = btrfs_file_extent_inline_len(leaf->items +
  1283. path->slots[0]);
  1284. extent_end = (extent_start + size) >> inode->i_blkbits;
  1285. extent_start >>= inode->i_blkbits;
  1286. if (iblock < extent_start || iblock > extent_end) {
  1287. goto not_found;
  1288. }
  1289. ptr = btrfs_file_extent_inline_start(item);
  1290. map = kmap(result->b_page);
  1291. memcpy(map, ptr, size);
  1292. memset(map + size, 0, PAGE_CACHE_SIZE - size);
  1293. flush_dcache_page(result->b_page);
  1294. kunmap(result->b_page);
  1295. set_buffer_uptodate(result);
  1296. SetPageChecked(result->b_page);
  1297. btrfs_map_bh_to_logical(root, result, 0);
  1298. }
  1299. not_found:
  1300. if (create & BTRFS_GET_BLOCK_CREATE) {
  1301. struct btrfs_key ins;
  1302. ret = btrfs_alloc_extent(trans, root, inode->i_ino,
  1303. 1, alloc_hint, (u64)-1,
  1304. &ins, 1);
  1305. if (ret) {
  1306. err = ret;
  1307. goto out;
  1308. }
  1309. ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
  1310. iblock << inode->i_blkbits,
  1311. ins.objectid, ins.offset,
  1312. ins.offset);
  1313. if (ret) {
  1314. err = ret;
  1315. goto out;
  1316. }
  1317. btrfs_map_bh_to_logical(root, result, ins.objectid);
  1318. }
  1319. out:
  1320. if (trans) {
  1321. ret = btrfs_end_transaction(trans, root);
  1322. if (!err)
  1323. err = ret;
  1324. }
  1325. btrfs_free_path(path);
  1326. return err;
  1327. }
  1328. int btrfs_get_block(struct inode *inode, sector_t iblock,
  1329. struct buffer_head *result, int create)
  1330. {
  1331. int err;
  1332. struct btrfs_root *root = BTRFS_I(inode)->root;
  1333. mutex_lock(&root->fs_info->fs_mutex);
  1334. err = btrfs_get_block_lock(inode, iblock, result, create);
  1335. mutex_unlock(&root->fs_info->fs_mutex);
  1336. return err;
  1337. }
  1338. static int btrfs_get_block_csum(struct inode *inode, sector_t iblock,
  1339. struct buffer_head *result, int create)
  1340. {
  1341. int ret;
  1342. struct btrfs_root *root = BTRFS_I(inode)->root;
  1343. struct page *page = result->b_page;
  1344. u64 offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(result);
  1345. struct btrfs_csum_item *item;
  1346. struct btrfs_path *path = NULL;
  1347. mutex_lock(&root->fs_info->fs_mutex);
  1348. ret = btrfs_get_block_lock(inode, iblock, result, create);
  1349. if (ret)
  1350. goto out;
  1351. path = btrfs_alloc_path();
  1352. item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, offset, 0);
  1353. if (IS_ERR(item)) {
  1354. ret = PTR_ERR(item);
  1355. /* a csum that isn't present is a preallocated region. */
  1356. if (ret == -ENOENT || ret == -EFBIG)
  1357. ret = 0;
  1358. result->b_private = NULL;
  1359. goto out;
  1360. }
  1361. memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE);
  1362. out:
  1363. if (path)
  1364. btrfs_free_path(path);
  1365. mutex_unlock(&root->fs_info->fs_mutex);
  1366. return ret;
  1367. }
  1368. static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
  1369. struct buffer_head *result, int create)
  1370. {
  1371. struct btrfs_root *root = BTRFS_I(inode)->root;
  1372. mutex_lock(&root->fs_info->fs_mutex);
  1373. btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT);
  1374. mutex_unlock(&root->fs_info->fs_mutex);
  1375. return 0;
  1376. }
  1377. static sector_t btrfs_bmap(struct address_space *as, sector_t block)
  1378. {
  1379. return generic_block_bmap(as, block, btrfs_get_block_bmap);
  1380. }
  1381. static int btrfs_prepare_write(struct file *file, struct page *page,
  1382. unsigned from, unsigned to)
  1383. {
  1384. return block_prepare_write(page, from, to, btrfs_get_block);
  1385. }
  1386. static void buffer_io_error(struct buffer_head *bh)
  1387. {
  1388. char b[BDEVNAME_SIZE];
  1389. printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
  1390. bdevname(bh->b_bdev, b),
  1391. (unsigned long long)bh->b_blocknr);
  1392. }
  1393. /*
  1394. * I/O completion handler for block_read_full_page() - pages
  1395. * which come unlocked at the end of I/O.
  1396. */
  1397. static void btrfs_end_buffer_async_read(struct buffer_head *bh, int uptodate)
  1398. {
  1399. unsigned long flags;
  1400. struct buffer_head *first;
  1401. struct buffer_head *tmp;
  1402. struct page *page;
  1403. int page_uptodate = 1;
  1404. struct inode *inode;
  1405. int ret;
  1406. BUG_ON(!buffer_async_read(bh));
  1407. page = bh->b_page;
  1408. inode = page->mapping->host;
  1409. if (uptodate) {
  1410. void *kaddr;
  1411. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  1412. if (bh->b_private) {
  1413. char csum[BTRFS_CRC32_SIZE];
  1414. kaddr = kmap_atomic(page, KM_IRQ0);
  1415. ret = btrfs_csum_data(root, kaddr + bh_offset(bh),
  1416. bh->b_size, csum);
  1417. BUG_ON(ret);
  1418. if (memcmp(csum, &bh->b_private, BTRFS_CRC32_SIZE)) {
  1419. u64 offset;
  1420. offset = (page->index << PAGE_CACHE_SHIFT) +
  1421. bh_offset(bh);
  1422. printk("btrfs csum failed ino %lu off %llu\n",
  1423. page->mapping->host->i_ino,
  1424. (unsigned long long)offset);
  1425. memset(kaddr + bh_offset(bh), 1, bh->b_size);
  1426. flush_dcache_page(page);
  1427. }
  1428. kunmap_atomic(kaddr, KM_IRQ0);
  1429. }
  1430. set_buffer_uptodate(bh);
  1431. } else {
  1432. clear_buffer_uptodate(bh);
  1433. if (printk_ratelimit())
  1434. buffer_io_error(bh);
  1435. SetPageError(page);
  1436. }
  1437. /*
  1438. * Be _very_ careful from here on. Bad things can happen if
  1439. * two buffer heads end IO at almost the same time and both
  1440. * decide that the page is now completely done.
  1441. */
  1442. first = page_buffers(page);
  1443. local_irq_save(flags);
  1444. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  1445. clear_buffer_async_read(bh);
  1446. unlock_buffer(bh);
  1447. tmp = bh;
  1448. do {
  1449. if (!buffer_uptodate(tmp))
  1450. page_uptodate = 0;
  1451. if (buffer_async_read(tmp)) {
  1452. BUG_ON(!buffer_locked(tmp));
  1453. goto still_busy;
  1454. }
  1455. tmp = tmp->b_this_page;
  1456. } while (tmp != bh);
  1457. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  1458. local_irq_restore(flags);
  1459. /*
  1460. * If none of the buffers had errors and they are all
  1461. * uptodate then we can set the page uptodate.
  1462. */
  1463. if (page_uptodate && !PageError(page))
  1464. SetPageUptodate(page);
  1465. unlock_page(page);
  1466. return;
  1467. still_busy:
  1468. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  1469. local_irq_restore(flags);
  1470. return;
  1471. }
  1472. /*
  1473. * Generic "read page" function for block devices that have the normal
  1474. * get_block functionality. This is most of the block device filesystems.
  1475. * Reads the page asynchronously --- the unlock_buffer() and
  1476. * set/clear_buffer_uptodate() functions propagate buffer state into the
  1477. * page struct once IO has completed.
  1478. */
  1479. int btrfs_readpage(struct file *file, struct page *page)
  1480. {
  1481. struct inode *inode = page->mapping->host;
  1482. sector_t iblock, lblock;
  1483. struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
  1484. unsigned int blocksize;
  1485. int nr, i;
  1486. int fully_mapped = 1;
  1487. BUG_ON(!PageLocked(page));
  1488. blocksize = 1 << inode->i_blkbits;
  1489. if (!page_has_buffers(page))
  1490. create_empty_buffers(page, blocksize, 0);
  1491. head = page_buffers(page);
  1492. iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1493. lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
  1494. bh = head;
  1495. nr = 0;
  1496. i = 0;
  1497. do {
  1498. if (buffer_uptodate(bh))
  1499. continue;
  1500. if (!buffer_mapped(bh)) {
  1501. int err = 0;
  1502. fully_mapped = 0;
  1503. if (iblock < lblock) {
  1504. WARN_ON(bh->b_size != blocksize);
  1505. err = btrfs_get_block_csum(inode, iblock,
  1506. bh, 0);
  1507. if (err)
  1508. SetPageError(page);
  1509. }
  1510. if (!buffer_mapped(bh)) {
  1511. void *kaddr = kmap_atomic(page, KM_USER0);
  1512. memset(kaddr + i * blocksize, 0, blocksize);
  1513. flush_dcache_page(page);
  1514. kunmap_atomic(kaddr, KM_USER0);
  1515. if (!err)
  1516. set_buffer_uptodate(bh);
  1517. continue;
  1518. }
  1519. /*
  1520. * get_block() might have updated the buffer
  1521. * synchronously
  1522. */
  1523. if (buffer_uptodate(bh))
  1524. continue;
  1525. }
  1526. arr[nr++] = bh;
  1527. } while (i++, iblock++, (bh = bh->b_this_page) != head);
  1528. if (fully_mapped)
  1529. SetPageMappedToDisk(page);
  1530. if (!nr) {
  1531. /*
  1532. * All buffers are uptodate - we can set the page uptodate
  1533. * as well. But not if get_block() returned an error.
  1534. */
  1535. if (!PageError(page))
  1536. SetPageUptodate(page);
  1537. unlock_page(page);
  1538. return 0;
  1539. }
  1540. /* Stage two: lock the buffers */
  1541. for (i = 0; i < nr; i++) {
  1542. bh = arr[i];
  1543. lock_buffer(bh);
  1544. bh->b_end_io = btrfs_end_buffer_async_read;
  1545. set_buffer_async_read(bh);
  1546. }
  1547. /*
  1548. * Stage 3: start the IO. Check for uptodateness
  1549. * inside the buffer lock in case another process reading
  1550. * the underlying blockdev brought it uptodate (the sct fix).
  1551. */
  1552. for (i = 0; i < nr; i++) {
  1553. bh = arr[i];
  1554. if (buffer_uptodate(bh))
  1555. btrfs_end_buffer_async_read(bh, 1);
  1556. else
  1557. submit_bh(READ, bh);
  1558. }
  1559. return 0;
  1560. }
  1561. /*
  1562. * Aside from a tiny bit of packed file data handling, this is the
  1563. * same as the generic code.
  1564. *
  1565. * While block_write_full_page is writing back the dirty buffers under
  1566. * the page lock, whoever dirtied the buffers may decide to clean them
  1567. * again at any time. We handle that by only looking at the buffer
  1568. * state inside lock_buffer().
  1569. *
  1570. * If block_write_full_page() is called for regular writeback
  1571. * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
  1572. * locked buffer. This only can happen if someone has written the buffer
  1573. * directly, with submit_bh(). At the address_space level PageWriteback
  1574. * prevents this contention from occurring.
  1575. */
  1576. static int __btrfs_write_full_page(struct inode *inode, struct page *page,
  1577. struct writeback_control *wbc)
  1578. {
  1579. int err;
  1580. sector_t block;
  1581. sector_t last_block;
  1582. struct buffer_head *bh, *head;
  1583. const unsigned blocksize = 1 << inode->i_blkbits;
  1584. int nr_underway = 0;
  1585. struct btrfs_root *root = BTRFS_I(inode)->root;
  1586. BUG_ON(!PageLocked(page));
  1587. last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
  1588. /* no csumming allowed when from PF_MEMALLOC */
  1589. if (current->flags & PF_MEMALLOC) {
  1590. redirty_page_for_writepage(wbc, page);
  1591. unlock_page(page);
  1592. return 0;
  1593. }
  1594. if (!page_has_buffers(page)) {
  1595. create_empty_buffers(page, blocksize,
  1596. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1597. }
  1598. /*
  1599. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  1600. * here, and the (potentially unmapped) buffers may become dirty at
  1601. * any time. If a buffer becomes dirty here after we've inspected it
  1602. * then we just miss that fact, and the page stays dirty.
  1603. *
  1604. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  1605. * handle that here by just cleaning them.
  1606. */
  1607. block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1608. head = page_buffers(page);
  1609. bh = head;
  1610. /*
  1611. * Get all the dirty buffers mapped to disk addresses and
  1612. * handle any aliases from the underlying blockdev's mapping.
  1613. */
  1614. do {
  1615. if (block > last_block) {
  1616. /*
  1617. * mapped buffers outside i_size will occur, because
  1618. * this page can be outside i_size when there is a
  1619. * truncate in progress.
  1620. */
  1621. /*
  1622. * The buffer was zeroed by block_write_full_page()
  1623. */
  1624. clear_buffer_dirty(bh);
  1625. set_buffer_uptodate(bh);
  1626. } else if (!buffer_mapped(bh) && buffer_dirty(bh)) {
  1627. WARN_ON(bh->b_size != blocksize);
  1628. err = btrfs_get_block(inode, block, bh, 0);
  1629. if (err) {
  1630. goto recover;
  1631. }
  1632. if (buffer_new(bh)) {
  1633. /* blockdev mappings never come here */
  1634. clear_buffer_new(bh);
  1635. }
  1636. }
  1637. bh = bh->b_this_page;
  1638. block++;
  1639. } while (bh != head);
  1640. do {
  1641. if (!buffer_mapped(bh))
  1642. continue;
  1643. /*
  1644. * If it's a fully non-blocking write attempt and we cannot
  1645. * lock the buffer then redirty the page. Note that this can
  1646. * potentially cause a busy-wait loop from pdflush and kswapd
  1647. * activity, but those code paths have their own higher-level
  1648. * throttling.
  1649. */
  1650. if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
  1651. lock_buffer(bh);
  1652. } else if (test_set_buffer_locked(bh)) {
  1653. redirty_page_for_writepage(wbc, page);
  1654. continue;
  1655. }
  1656. if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) {
  1657. struct btrfs_trans_handle *trans;
  1658. int ret;
  1659. u64 off = page->index << PAGE_CACHE_SHIFT;
  1660. char *kaddr;
  1661. off += bh_offset(bh);
  1662. mutex_lock(&root->fs_info->fs_mutex);
  1663. trans = btrfs_start_transaction(root, 1);
  1664. btrfs_set_trans_block_group(trans, inode);
  1665. kaddr = kmap(page);
  1666. btrfs_csum_file_block(trans, root, inode->i_ino,
  1667. off, kaddr + bh_offset(bh),
  1668. bh->b_size);
  1669. kunmap(page);
  1670. ret = btrfs_end_transaction(trans, root);
  1671. BUG_ON(ret);
  1672. mutex_unlock(&root->fs_info->fs_mutex);
  1673. mark_buffer_async_write(bh);
  1674. } else {
  1675. unlock_buffer(bh);
  1676. }
  1677. } while ((bh = bh->b_this_page) != head);
  1678. /*
  1679. * The page and its buffers are protected by PageWriteback(), so we can
  1680. * drop the bh refcounts early.
  1681. */
  1682. BUG_ON(PageWriteback(page));
  1683. set_page_writeback(page);
  1684. do {
  1685. struct buffer_head *next = bh->b_this_page;
  1686. if (buffer_async_write(bh)) {
  1687. submit_bh(WRITE, bh);
  1688. nr_underway++;
  1689. }
  1690. bh = next;
  1691. } while (bh != head);
  1692. unlock_page(page);
  1693. err = 0;
  1694. done:
  1695. if (nr_underway == 0) {
  1696. /*
  1697. * The page was marked dirty, but the buffers were
  1698. * clean. Someone wrote them back by hand with
  1699. * ll_rw_block/submit_bh. A rare case.
  1700. */
  1701. int uptodate = 1;
  1702. do {
  1703. if (!buffer_uptodate(bh)) {
  1704. uptodate = 0;
  1705. break;
  1706. }
  1707. bh = bh->b_this_page;
  1708. } while (bh != head);
  1709. if (uptodate)
  1710. SetPageUptodate(page);
  1711. end_page_writeback(page);
  1712. }
  1713. return err;
  1714. recover:
  1715. /*
  1716. * ENOSPC, or some other error. We may already have added some
  1717. * blocks to the file, so we need to write these out to avoid
  1718. * exposing stale data.
  1719. * The page is currently locked and not marked for writeback
  1720. */
  1721. bh = head;
  1722. /* Recovery: lock and submit the mapped buffers */
  1723. do {
  1724. if (buffer_mapped(bh) && buffer_dirty(bh)) {
  1725. lock_buffer(bh);
  1726. mark_buffer_async_write(bh);
  1727. } else {
  1728. /*
  1729. * The buffer may have been set dirty during
  1730. * attachment to a dirty page.
  1731. */
  1732. clear_buffer_dirty(bh);
  1733. }
  1734. } while ((bh = bh->b_this_page) != head);
  1735. SetPageError(page);
  1736. BUG_ON(PageWriteback(page));
  1737. set_page_writeback(page);
  1738. do {
  1739. struct buffer_head *next = bh->b_this_page;
  1740. if (buffer_async_write(bh)) {
  1741. clear_buffer_dirty(bh);
  1742. submit_bh(WRITE, bh);
  1743. nr_underway++;
  1744. }
  1745. bh = next;
  1746. } while (bh != head);
  1747. unlock_page(page);
  1748. goto done;
  1749. }
  1750. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  1751. {
  1752. struct inode * const inode = page->mapping->host;
  1753. loff_t i_size = i_size_read(inode);
  1754. const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  1755. unsigned offset;
  1756. void *kaddr;
  1757. /* Is the page fully inside i_size? */
  1758. if (page->index < end_index)
  1759. return __btrfs_write_full_page(inode, page, wbc);
  1760. /* Is the page fully outside i_size? (truncate in progress) */
  1761. offset = i_size & (PAGE_CACHE_SIZE-1);
  1762. if (page->index >= end_index+1 || !offset) {
  1763. /*
  1764. * The page may have dirty, unmapped buffers. For example,
  1765. * they may have been added in ext3_writepage(). Make them
  1766. * freeable here, so the page does not leak.
  1767. */
  1768. block_invalidatepage(page, 0);
  1769. unlock_page(page);
  1770. return 0; /* don't care */
  1771. }
  1772. /*
  1773. * The page straddles i_size. It must be zeroed out on each and every
  1774. * writepage invokation because it may be mmapped. "A file is mapped
  1775. * in multiples of the page size. For a file that is not a multiple of
  1776. * the page size, the remaining memory is zeroed when mapped, and
  1777. * writes to that region are not written out to the file."
  1778. */
  1779. kaddr = kmap_atomic(page, KM_USER0);
  1780. memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
  1781. flush_dcache_page(page);
  1782. kunmap_atomic(kaddr, KM_USER0);
  1783. return __btrfs_write_full_page(inode, page, wbc);
  1784. }
  1785. /*
  1786. * btrfs_page_mkwrite() is not allowed to change the file size as it gets
  1787. * called from a page fault handler when a page is first dirtied. Hence we must
  1788. * be careful to check for EOF conditions here. We set the page up correctly
  1789. * for a written page which means we get ENOSPC checking when writing into
  1790. * holes and correct delalloc and unwritten extent mapping on filesystems that
  1791. * support these features.
  1792. *
  1793. * We are not allowed to take the i_mutex here so we have to play games to
  1794. * protect against truncate races as the page could now be beyond EOF. Because
  1795. * vmtruncate() writes the inode size before removing pages, once we have the
  1796. * page lock we can determine safely if the page is beyond EOF. If it is not
  1797. * beyond EOF, then the page is guaranteed safe against truncation until we
  1798. * unlock the page.
  1799. */
  1800. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
  1801. {
  1802. struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
  1803. unsigned long end;
  1804. loff_t size;
  1805. int ret = -EINVAL;
  1806. lock_page(page);
  1807. wait_on_page_writeback(page);
  1808. size = i_size_read(inode);
  1809. if ((page->mapping != inode->i_mapping) ||
  1810. ((page->index << PAGE_CACHE_SHIFT) > size)) {
  1811. /* page got truncated out from underneath us */
  1812. goto out_unlock;
  1813. }
  1814. /* page is wholly or partially inside EOF */
  1815. if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
  1816. end = size & ~PAGE_CACHE_MASK;
  1817. else
  1818. end = PAGE_CACHE_SIZE;
  1819. ret = btrfs_prepare_write(NULL, page, 0, end);
  1820. if (!ret)
  1821. ret = btrfs_commit_write(NULL, page, 0, end);
  1822. out_unlock:
  1823. unlock_page(page);
  1824. return ret;
  1825. }
  1826. static void btrfs_truncate(struct inode *inode)
  1827. {
  1828. struct btrfs_root *root = BTRFS_I(inode)->root;
  1829. int ret;
  1830. struct btrfs_trans_handle *trans;
  1831. if (!S_ISREG(inode->i_mode))
  1832. return;
  1833. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  1834. return;
  1835. btrfs_truncate_page(inode->i_mapping, inode->i_size);
  1836. mutex_lock(&root->fs_info->fs_mutex);
  1837. trans = btrfs_start_transaction(root, 1);
  1838. btrfs_set_trans_block_group(trans, inode);
  1839. /* FIXME, add redo link to tree so we don't leak on crash */
  1840. ret = btrfs_truncate_in_trans(trans, root, inode);
  1841. btrfs_update_inode(trans, root, inode);
  1842. ret = btrfs_end_transaction(trans, root);
  1843. BUG_ON(ret);
  1844. mutex_unlock(&root->fs_info->fs_mutex);
  1845. btrfs_btree_balance_dirty(root);
  1846. }
  1847. int btrfs_commit_write(struct file *file, struct page *page,
  1848. unsigned from, unsigned to)
  1849. {
  1850. struct inode *inode = page->mapping->host;
  1851. struct buffer_head *bh;
  1852. loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
  1853. SetPageUptodate(page);
  1854. bh = page_buffers(page);
  1855. set_buffer_uptodate(bh);
  1856. if (buffer_mapped(bh) && bh->b_blocknr != 0) {
  1857. set_page_dirty(page);
  1858. }
  1859. if (pos > inode->i_size) {
  1860. i_size_write(inode, pos);
  1861. mark_inode_dirty(inode);
  1862. }
  1863. return 0;
  1864. }
  1865. static int create_subvol(struct btrfs_root *root, char *name, int namelen)
  1866. {
  1867. struct btrfs_trans_handle *trans;
  1868. struct btrfs_key key;
  1869. struct btrfs_root_item root_item;
  1870. struct btrfs_inode_item *inode_item;
  1871. struct buffer_head *subvol;
  1872. struct btrfs_leaf *leaf;
  1873. struct btrfs_root *new_root;
  1874. struct inode *inode;
  1875. struct inode *dir;
  1876. int ret;
  1877. int err;
  1878. u64 objectid;
  1879. u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
  1880. mutex_lock(&root->fs_info->fs_mutex);
  1881. trans = btrfs_start_transaction(root, 1);
  1882. BUG_ON(!trans);
  1883. subvol = btrfs_alloc_free_block(trans, root, 0);
  1884. if (IS_ERR(subvol))
  1885. return PTR_ERR(subvol);
  1886. leaf = btrfs_buffer_leaf(subvol);
  1887. btrfs_set_header_nritems(&leaf->header, 0);
  1888. btrfs_set_header_level(&leaf->header, 0);
  1889. btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
  1890. btrfs_set_header_generation(&leaf->header, trans->transid);
  1891. btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
  1892. memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
  1893. sizeof(leaf->header.fsid));
  1894. btrfs_mark_buffer_dirty(subvol);
  1895. inode_item = &root_item.inode;
  1896. memset(inode_item, 0, sizeof(*inode_item));
  1897. btrfs_set_inode_generation(inode_item, 1);
  1898. btrfs_set_inode_size(inode_item, 3);
  1899. btrfs_set_inode_nlink(inode_item, 1);
  1900. btrfs_set_inode_nblocks(inode_item, 1);
  1901. btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
  1902. btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
  1903. btrfs_set_root_refs(&root_item, 1);
  1904. memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
  1905. root_item.drop_level = 0;
  1906. brelse(subvol);
  1907. subvol = NULL;
  1908. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  1909. 0, &objectid);
  1910. if (ret)
  1911. goto fail;
  1912. btrfs_set_root_dirid(&root_item, new_dirid);
  1913. key.objectid = objectid;
  1914. key.offset = 1;
  1915. key.flags = 0;
  1916. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  1917. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  1918. &root_item);
  1919. if (ret)
  1920. goto fail;
  1921. /*
  1922. * insert the directory item
  1923. */
  1924. key.offset = (u64)-1;
  1925. dir = root->fs_info->sb->s_root->d_inode;
  1926. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  1927. name, namelen, dir->i_ino, &key,
  1928. BTRFS_FT_DIR);
  1929. if (ret)
  1930. goto fail;
  1931. ret = btrfs_commit_transaction(trans, root);
  1932. if (ret)
  1933. goto fail_commit;
  1934. new_root = btrfs_read_fs_root(root->fs_info, &key);
  1935. BUG_ON(!new_root);
  1936. trans = btrfs_start_transaction(new_root, 1);
  1937. BUG_ON(!trans);
  1938. inode = btrfs_new_inode(trans, new_root, new_dirid,
  1939. BTRFS_I(dir)->block_group, S_IFDIR | 0700);
  1940. if (IS_ERR(inode))
  1941. goto fail;
  1942. inode->i_op = &btrfs_dir_inode_operations;
  1943. inode->i_fop = &btrfs_dir_file_operations;
  1944. new_root->inode = inode;
  1945. ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
  1946. if (ret)
  1947. goto fail;
  1948. inode->i_nlink = 1;
  1949. inode->i_size = 6;
  1950. ret = btrfs_update_inode(trans, new_root, inode);
  1951. if (ret)
  1952. goto fail;
  1953. fail:
  1954. err = btrfs_commit_transaction(trans, root);
  1955. if (err && !ret)
  1956. ret = err;
  1957. fail_commit:
  1958. mutex_unlock(&root->fs_info->fs_mutex);
  1959. btrfs_btree_balance_dirty(root);
  1960. return ret;
  1961. }
  1962. static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
  1963. {
  1964. struct btrfs_trans_handle *trans;
  1965. struct btrfs_key key;
  1966. struct btrfs_root_item new_root_item;
  1967. int ret;
  1968. int err;
  1969. u64 objectid;
  1970. if (!root->ref_cows)
  1971. return -EINVAL;
  1972. mutex_lock(&root->fs_info->fs_mutex);
  1973. trans = btrfs_start_transaction(root, 1);
  1974. BUG_ON(!trans);
  1975. ret = btrfs_update_inode(trans, root, root->inode);
  1976. if (ret)
  1977. goto fail;
  1978. ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
  1979. 0, &objectid);
  1980. if (ret)
  1981. goto fail;
  1982. memcpy(&new_root_item, &root->root_item,
  1983. sizeof(new_root_item));
  1984. key.objectid = objectid;
  1985. key.offset = 1;
  1986. key.flags = 0;
  1987. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  1988. btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
  1989. ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
  1990. &new_root_item);
  1991. if (ret)
  1992. goto fail;
  1993. /*
  1994. * insert the directory item
  1995. */
  1996. key.offset = (u64)-1;
  1997. ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
  1998. name, namelen,
  1999. root->fs_info->sb->s_root->d_inode->i_ino,
  2000. &key, BTRFS_FT_DIR);
  2001. if (ret)
  2002. goto fail;
  2003. ret = btrfs_inc_root_ref(trans, root);
  2004. if (ret)
  2005. goto fail;
  2006. fail:
  2007. err = btrfs_commit_transaction(trans, root);
  2008. if (err && !ret)
  2009. ret = err;
  2010. mutex_unlock(&root->fs_info->fs_mutex);
  2011. btrfs_btree_balance_dirty(root);
  2012. return ret;
  2013. }
  2014. int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
  2015. cmd, unsigned long arg)
  2016. {
  2017. struct btrfs_root *root = BTRFS_I(inode)->root;
  2018. struct btrfs_ioctl_vol_args vol_args;
  2019. int ret = 0;
  2020. struct btrfs_dir_item *di;
  2021. int namelen;
  2022. struct btrfs_path *path;
  2023. u64 root_dirid;
  2024. switch (cmd) {
  2025. case BTRFS_IOC_SNAP_CREATE:
  2026. if (copy_from_user(&vol_args,
  2027. (struct btrfs_ioctl_vol_args __user *)arg,
  2028. sizeof(vol_args)))
  2029. return -EFAULT;
  2030. namelen = strlen(vol_args.name);
  2031. if (namelen > BTRFS_VOL_NAME_MAX)
  2032. return -EINVAL;
  2033. if (strchr(vol_args.name, '/'))
  2034. return -EINVAL;
  2035. path = btrfs_alloc_path();
  2036. if (!path)
  2037. return -ENOMEM;
  2038. root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
  2039. mutex_lock(&root->fs_info->fs_mutex);
  2040. di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
  2041. path, root_dirid,
  2042. vol_args.name, namelen, 0);
  2043. mutex_unlock(&root->fs_info->fs_mutex);
  2044. btrfs_free_path(path);
  2045. if (di && !IS_ERR(di))
  2046. return -EEXIST;
  2047. if (IS_ERR(di))
  2048. return PTR_ERR(di);
  2049. if (root == root->fs_info->tree_root)
  2050. ret = create_subvol(root, vol_args.name, namelen);
  2051. else
  2052. ret = create_snapshot(root, vol_args.name, namelen);
  2053. break;
  2054. default:
  2055. return -ENOTTY;
  2056. }
  2057. return ret;
  2058. }
  2059. #ifdef CONFIG_COMPAT
  2060. long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
  2061. unsigned long arg)
  2062. {
  2063. struct inode *inode = file->f_path.dentry->d_inode;
  2064. int ret;
  2065. lock_kernel();
  2066. ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
  2067. unlock_kernel();
  2068. return ret;
  2069. }
  2070. #endif
  2071. /*
  2072. * Called inside transaction, so use GFP_NOFS
  2073. */
  2074. struct inode *btrfs_alloc_inode(struct super_block *sb)
  2075. {
  2076. struct btrfs_inode *ei;
  2077. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  2078. if (!ei)
  2079. return NULL;
  2080. return &ei->vfs_inode;
  2081. }
  2082. void btrfs_destroy_inode(struct inode *inode)
  2083. {
  2084. WARN_ON(!list_empty(&inode->i_dentry));
  2085. WARN_ON(inode->i_data.nrpages);
  2086. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  2087. }
  2088. static void init_once(void * foo, struct kmem_cache * cachep,
  2089. unsigned long flags)
  2090. {
  2091. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  2092. inode_init_once(&ei->vfs_inode);
  2093. }
  2094. void btrfs_destroy_cachep(void)
  2095. {
  2096. if (btrfs_inode_cachep)
  2097. kmem_cache_destroy(btrfs_inode_cachep);
  2098. if (btrfs_trans_handle_cachep)
  2099. kmem_cache_destroy(btrfs_trans_handle_cachep);
  2100. if (btrfs_transaction_cachep)
  2101. kmem_cache_destroy(btrfs_transaction_cachep);
  2102. if (btrfs_bit_radix_cachep)
  2103. kmem_cache_destroy(btrfs_bit_radix_cachep);
  2104. if (btrfs_path_cachep)
  2105. kmem_cache_destroy(btrfs_path_cachep);
  2106. }
  2107. int btrfs_init_cachep(void)
  2108. {
  2109. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  2110. sizeof(struct btrfs_inode),
  2111. 0, (SLAB_RECLAIM_ACCOUNT|
  2112. SLAB_MEM_SPREAD),
  2113. init_once, NULL);
  2114. if (!btrfs_inode_cachep)
  2115. goto fail;
  2116. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  2117. sizeof(struct btrfs_trans_handle),
  2118. 0, (SLAB_RECLAIM_ACCOUNT|
  2119. SLAB_MEM_SPREAD),
  2120. NULL, NULL);
  2121. if (!btrfs_trans_handle_cachep)
  2122. goto fail;
  2123. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  2124. sizeof(struct btrfs_transaction),
  2125. 0, (SLAB_RECLAIM_ACCOUNT|
  2126. SLAB_MEM_SPREAD),
  2127. NULL, NULL);
  2128. if (!btrfs_transaction_cachep)
  2129. goto fail;
  2130. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  2131. sizeof(struct btrfs_transaction),
  2132. 0, (SLAB_RECLAIM_ACCOUNT|
  2133. SLAB_MEM_SPREAD),
  2134. NULL, NULL);
  2135. if (!btrfs_path_cachep)
  2136. goto fail;
  2137. btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
  2138. 256,
  2139. 0, (SLAB_RECLAIM_ACCOUNT|
  2140. SLAB_MEM_SPREAD |
  2141. SLAB_DESTROY_BY_RCU),
  2142. NULL, NULL);
  2143. if (!btrfs_bit_radix_cachep)
  2144. goto fail;
  2145. return 0;
  2146. fail:
  2147. btrfs_destroy_cachep();
  2148. return -ENOMEM;
  2149. }
  2150. static int btrfs_getattr(struct vfsmount *mnt,
  2151. struct dentry *dentry, struct kstat *stat)
  2152. {
  2153. struct inode *inode = dentry->d_inode;
  2154. generic_fillattr(inode, stat);
  2155. stat->blksize = 256 * 1024;
  2156. return 0;
  2157. }
  2158. static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
  2159. struct inode * new_dir,struct dentry *new_dentry)
  2160. {
  2161. struct btrfs_trans_handle *trans;
  2162. struct btrfs_root *root = BTRFS_I(old_dir)->root;
  2163. struct inode *new_inode = new_dentry->d_inode;
  2164. struct inode *old_inode = old_dentry->d_inode;
  2165. struct timespec ctime = CURRENT_TIME;
  2166. struct btrfs_path *path;
  2167. struct btrfs_dir_item *di;
  2168. int ret;
  2169. if (S_ISDIR(old_inode->i_mode) && new_inode &&
  2170. new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
  2171. return -ENOTEMPTY;
  2172. }
  2173. mutex_lock(&root->fs_info->fs_mutex);
  2174. trans = btrfs_start_transaction(root, 1);
  2175. btrfs_set_trans_block_group(trans, new_dir);
  2176. path = btrfs_alloc_path();
  2177. if (!path) {
  2178. ret = -ENOMEM;
  2179. goto out_fail;
  2180. }
  2181. old_dentry->d_inode->i_nlink++;
  2182. old_dir->i_ctime = old_dir->i_mtime = ctime;
  2183. new_dir->i_ctime = new_dir->i_mtime = ctime;
  2184. old_inode->i_ctime = ctime;
  2185. if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
  2186. struct btrfs_key *location = &BTRFS_I(new_dir)->location;
  2187. u64 old_parent_oid;
  2188. di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
  2189. "..", 2, -1);
  2190. if (IS_ERR(di)) {
  2191. ret = PTR_ERR(di);
  2192. goto out_fail;
  2193. }
  2194. if (!di) {
  2195. ret = -ENOENT;
  2196. goto out_fail;
  2197. }
  2198. old_parent_oid = btrfs_disk_key_objectid(&di->location);
  2199. ret = btrfs_del_item(trans, root, path);
  2200. if (ret) {
  2201. goto out_fail;
  2202. }
  2203. btrfs_release_path(root, path);
  2204. di = btrfs_lookup_dir_index_item(trans, root, path,
  2205. old_inode->i_ino,
  2206. old_parent_oid,
  2207. "..", 2, -1);
  2208. if (IS_ERR(di)) {
  2209. ret = PTR_ERR(di);
  2210. goto out_fail;
  2211. }
  2212. if (!di) {
  2213. ret = -ENOENT;
  2214. goto out_fail;
  2215. }
  2216. ret = btrfs_del_item(trans, root, path);
  2217. if (ret) {
  2218. goto out_fail;
  2219. }
  2220. btrfs_release_path(root, path);
  2221. ret = btrfs_insert_dir_item(trans, root, "..", 2,
  2222. old_inode->i_ino, location,
  2223. BTRFS_FT_DIR);
  2224. if (ret)
  2225. goto out_fail;
  2226. }
  2227. ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
  2228. if (ret)
  2229. goto out_fail;
  2230. if (new_inode) {
  2231. new_inode->i_ctime = CURRENT_TIME;
  2232. ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
  2233. if (ret)
  2234. goto out_fail;
  2235. if (S_ISDIR(new_inode->i_mode))
  2236. clear_nlink(new_inode);
  2237. else
  2238. drop_nlink(new_inode);
  2239. ret = btrfs_update_inode(trans, root, new_inode);
  2240. if (ret)
  2241. goto out_fail;
  2242. }
  2243. ret = btrfs_add_link(trans, new_dentry, old_inode);
  2244. if (ret)
  2245. goto out_fail;
  2246. out_fail:
  2247. btrfs_free_path(path);
  2248. btrfs_end_transaction(trans, root);
  2249. mutex_unlock(&root->fs_info->fs_mutex);
  2250. return ret;
  2251. }
  2252. static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
  2253. const char *symname)
  2254. {
  2255. struct btrfs_trans_handle *trans;
  2256. struct btrfs_root *root = BTRFS_I(dir)->root;
  2257. struct btrfs_path *path;
  2258. struct btrfs_key key;
  2259. struct inode *inode;
  2260. int err;
  2261. int drop_inode = 0;
  2262. u64 objectid;
  2263. int name_len;
  2264. int datasize;
  2265. char *ptr;
  2266. struct btrfs_file_extent_item *ei;
  2267. name_len = strlen(symname) + 1;
  2268. if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
  2269. return -ENAMETOOLONG;
  2270. mutex_lock(&root->fs_info->fs_mutex);
  2271. trans = btrfs_start_transaction(root, 1);
  2272. btrfs_set_trans_block_group(trans, dir);
  2273. err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  2274. if (err) {
  2275. err = -ENOSPC;
  2276. goto out_unlock;
  2277. }
  2278. inode = btrfs_new_inode(trans, root, objectid,
  2279. BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
  2280. err = PTR_ERR(inode);
  2281. if (IS_ERR(inode))
  2282. goto out_unlock;
  2283. btrfs_set_trans_block_group(trans, inode);
  2284. err = btrfs_add_nondir(trans, dentry, inode);
  2285. if (err)
  2286. drop_inode = 1;
  2287. else {
  2288. inode->i_mapping->a_ops = &btrfs_aops;
  2289. inode->i_fop = &btrfs_file_operations;
  2290. inode->i_op = &btrfs_file_inode_operations;
  2291. }
  2292. dir->i_sb->s_dirt = 1;
  2293. btrfs_update_inode_block_group(trans, inode);
  2294. btrfs_update_inode_block_group(trans, dir);
  2295. if (drop_inode)
  2296. goto out_unlock;
  2297. path = btrfs_alloc_path();
  2298. BUG_ON(!path);
  2299. key.objectid = inode->i_ino;
  2300. key.offset = 0;
  2301. key.flags = 0;
  2302. btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
  2303. datasize = btrfs_file_extent_calc_inline_size(name_len);
  2304. err = btrfs_insert_empty_item(trans, root, path, &key,
  2305. datasize);
  2306. if (err) {
  2307. drop_inode = 1;
  2308. goto out_unlock;
  2309. }
  2310. ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  2311. path->slots[0], struct btrfs_file_extent_item);
  2312. btrfs_set_file_extent_generation(ei, trans->transid);
  2313. btrfs_set_file_extent_type(ei,
  2314. BTRFS_FILE_EXTENT_INLINE);
  2315. ptr = btrfs_file_extent_inline_start(ei);
  2316. btrfs_memcpy(root, path->nodes[0]->b_data,
  2317. ptr, symname, name_len);
  2318. btrfs_mark_buffer_dirty(path->nodes[0]);
  2319. btrfs_free_path(path);
  2320. inode->i_op = &btrfs_symlink_inode_operations;
  2321. inode->i_mapping->a_ops = &btrfs_symlink_aops;
  2322. inode->i_size = name_len - 1;
  2323. err = btrfs_update_inode(trans, root, inode);
  2324. if (err)
  2325. drop_inode = 1;
  2326. out_unlock:
  2327. btrfs_end_transaction(trans, root);
  2328. mutex_unlock(&root->fs_info->fs_mutex);
  2329. if (drop_inode) {
  2330. inode_dec_link_count(inode);
  2331. iput(inode);
  2332. }
  2333. btrfs_btree_balance_dirty(root);
  2334. return err;
  2335. }
  2336. static struct inode_operations btrfs_dir_inode_operations = {
  2337. .lookup = btrfs_lookup,
  2338. .create = btrfs_create,
  2339. .unlink = btrfs_unlink,
  2340. .link = btrfs_link,
  2341. .mkdir = btrfs_mkdir,
  2342. .rmdir = btrfs_rmdir,
  2343. .rename = btrfs_rename,
  2344. .symlink = btrfs_symlink,
  2345. .setattr = btrfs_setattr,
  2346. .mknod = btrfs_mknod,
  2347. };
  2348. static struct inode_operations btrfs_dir_ro_inode_operations = {
  2349. .lookup = btrfs_lookup,
  2350. };
  2351. static struct file_operations btrfs_dir_file_operations = {
  2352. .llseek = generic_file_llseek,
  2353. .read = generic_read_dir,
  2354. .readdir = btrfs_readdir,
  2355. .ioctl = btrfs_ioctl,
  2356. #ifdef CONFIG_COMPAT
  2357. .compat_ioctl = btrfs_compat_ioctl,
  2358. #endif
  2359. };
  2360. static struct address_space_operations btrfs_aops = {
  2361. .readpage = btrfs_readpage,
  2362. .writepage = btrfs_writepage,
  2363. .sync_page = block_sync_page,
  2364. .prepare_write = btrfs_prepare_write,
  2365. .commit_write = btrfs_commit_write,
  2366. .bmap = btrfs_bmap,
  2367. };
  2368. static struct address_space_operations btrfs_symlink_aops = {
  2369. .readpage = btrfs_readpage,
  2370. .writepage = btrfs_writepage,
  2371. };
  2372. static struct inode_operations btrfs_file_inode_operations = {
  2373. .truncate = btrfs_truncate,
  2374. .getattr = btrfs_getattr,
  2375. .setattr = btrfs_setattr,
  2376. };
  2377. static struct inode_operations btrfs_special_inode_operations = {
  2378. .getattr = btrfs_getattr,
  2379. .setattr = btrfs_setattr,
  2380. };
  2381. static struct inode_operations btrfs_symlink_inode_operations = {
  2382. .readlink = generic_readlink,
  2383. .follow_link = page_follow_link_light,
  2384. .put_link = page_put_link,
  2385. };