inode.c 67 KB

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