inode.c 66 KB

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