inode.c 58 KB

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