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