super.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. #include <linux/module.h>
  2. #include <linux/buffer_head.h>
  3. #include <linux/fs.h>
  4. #include <linux/pagemap.h>
  5. #include <linux/highmem.h>
  6. #include <linux/time.h>
  7. #include <linux/init.h>
  8. #include <linux/string.h>
  9. #include <linux/smp_lock.h>
  10. #include <linux/backing-dev.h>
  11. #include <linux/mpage.h>
  12. #include <linux/swap.h>
  13. #include <linux/writeback.h>
  14. #include "ctree.h"
  15. #include "disk-io.h"
  16. #include "transaction.h"
  17. #include "btrfs_inode.h"
  18. #define BTRFS_SUPER_MAGIC 0x9123682E
  19. static struct inode_operations btrfs_dir_inode_operations;
  20. static struct super_operations btrfs_super_ops;
  21. static struct file_operations btrfs_dir_file_operations;
  22. static struct inode_operations btrfs_file_inode_operations;
  23. static struct address_space_operations btrfs_aops;
  24. static struct file_operations btrfs_file_operations;
  25. static int check_inode(struct inode *inode)
  26. {
  27. struct btrfs_inode *ei = BTRFS_I(inode);
  28. WARN_ON(ei->magic != 0xDEADBEEF);
  29. WARN_ON(ei->magic2 != 0xDEADBEAF);
  30. return 0;
  31. }
  32. static void btrfs_read_locked_inode(struct inode *inode)
  33. {
  34. struct btrfs_path *path;
  35. struct btrfs_inode_item *inode_item;
  36. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  37. int ret;
  38. path = btrfs_alloc_path();
  39. BUG_ON(!path);
  40. btrfs_init_path(path);
  41. mutex_lock(&root->fs_info->fs_mutex);
  42. check_inode(inode);
  43. ret = btrfs_lookup_inode(NULL, root, path, inode->i_ino, 0);
  44. if (ret) {
  45. btrfs_release_path(root, path);
  46. btrfs_free_path(path);
  47. mutex_unlock(&root->fs_info->fs_mutex);
  48. make_bad_inode(inode);
  49. return;
  50. }
  51. check_inode(inode);
  52. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  53. path->slots[0],
  54. struct btrfs_inode_item);
  55. inode->i_mode = btrfs_inode_mode(inode_item);
  56. inode->i_nlink = btrfs_inode_nlink(inode_item);
  57. inode->i_uid = btrfs_inode_uid(inode_item);
  58. inode->i_gid = btrfs_inode_gid(inode_item);
  59. inode->i_size = btrfs_inode_size(inode_item);
  60. inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
  61. inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
  62. inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
  63. inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
  64. inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
  65. inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
  66. inode->i_blocks = btrfs_inode_nblocks(inode_item);
  67. inode->i_generation = btrfs_inode_generation(inode_item);
  68. btrfs_release_path(root, path);
  69. btrfs_free_path(path);
  70. inode_item = NULL;
  71. mutex_unlock(&root->fs_info->fs_mutex);
  72. check_inode(inode);
  73. switch (inode->i_mode & S_IFMT) {
  74. #if 0
  75. default:
  76. init_special_inode(inode, inode->i_mode,
  77. btrfs_inode_rdev(inode_item));
  78. break;
  79. #endif
  80. case S_IFREG:
  81. inode->i_mapping->a_ops = &btrfs_aops;
  82. inode->i_fop = &btrfs_file_operations;
  83. inode->i_op = &btrfs_file_inode_operations;
  84. break;
  85. case S_IFDIR:
  86. inode->i_op = &btrfs_dir_inode_operations;
  87. inode->i_fop = &btrfs_dir_file_operations;
  88. break;
  89. case S_IFLNK:
  90. // inode->i_op = &page_symlink_inode_operations;
  91. break;
  92. }
  93. check_inode(inode);
  94. return;
  95. }
  96. static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
  97. struct btrfs_root *root,
  98. struct inode *dir,
  99. struct dentry *dentry)
  100. {
  101. struct btrfs_path *path;
  102. const char *name = dentry->d_name.name;
  103. int name_len = dentry->d_name.len;
  104. int ret;
  105. u64 objectid;
  106. struct btrfs_dir_item *di;
  107. path = btrfs_alloc_path();
  108. BUG_ON(!path);
  109. btrfs_init_path(path);
  110. ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
  111. name, name_len, -1);
  112. if (ret < 0)
  113. goto err;
  114. if (ret > 0) {
  115. ret = -ENOENT;
  116. goto err;
  117. }
  118. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  119. struct btrfs_dir_item);
  120. objectid = btrfs_dir_objectid(di);
  121. ret = btrfs_del_item(trans, root, path);
  122. BUG_ON(ret);
  123. btrfs_release_path(root, path);
  124. ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
  125. objectid, -1);
  126. BUG_ON(ret);
  127. ret = btrfs_del_item(trans, root, path);
  128. BUG_ON(ret);
  129. dentry->d_inode->i_ctime = dir->i_ctime;
  130. err:
  131. btrfs_release_path(root, path);
  132. btrfs_free_path(path);
  133. if (ret == 0) {
  134. inode_dec_link_count(dentry->d_inode);
  135. dir->i_size -= name_len * 2;
  136. mark_inode_dirty(dir);
  137. }
  138. return ret;
  139. }
  140. static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
  141. {
  142. struct btrfs_root *root;
  143. struct btrfs_trans_handle *trans;
  144. int ret;
  145. root = btrfs_sb(dir->i_sb);
  146. mutex_lock(&root->fs_info->fs_mutex);
  147. trans = btrfs_start_transaction(root, 1);
  148. ret = btrfs_unlink_trans(trans, root, dir, dentry);
  149. btrfs_end_transaction(trans, root);
  150. mutex_unlock(&root->fs_info->fs_mutex);
  151. return ret;
  152. }
  153. static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
  154. {
  155. struct inode *inode = dentry->d_inode;
  156. int err;
  157. int ret;
  158. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  159. struct btrfs_path *path;
  160. struct btrfs_key key;
  161. struct btrfs_trans_handle *trans;
  162. struct btrfs_key found_key;
  163. int found_type;
  164. struct btrfs_leaf *leaf;
  165. char *goodnames = "..";
  166. path = btrfs_alloc_path();
  167. BUG_ON(!path);
  168. btrfs_init_path(path);
  169. mutex_lock(&root->fs_info->fs_mutex);
  170. trans = btrfs_start_transaction(root, 1);
  171. key.objectid = inode->i_ino;
  172. key.offset = (u64)-1;
  173. key.flags = (u32)-1;
  174. while(1) {
  175. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  176. if (ret < 0) {
  177. err = ret;
  178. goto out;
  179. }
  180. BUG_ON(ret == 0);
  181. if (path->slots[0] == 0) {
  182. err = -ENOENT;
  183. goto out;
  184. }
  185. path->slots[0]--;
  186. leaf = btrfs_buffer_leaf(path->nodes[0]);
  187. btrfs_disk_key_to_cpu(&found_key,
  188. &leaf->items[path->slots[0]].key);
  189. found_type = btrfs_key_type(&found_key);
  190. if (found_key.objectid != inode->i_ino) {
  191. err = -ENOENT;
  192. goto out;
  193. }
  194. if ((found_type != BTRFS_DIR_ITEM_KEY &&
  195. found_type != BTRFS_DIR_INDEX_KEY) ||
  196. (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
  197. !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
  198. err = -ENOTEMPTY;
  199. goto out;
  200. }
  201. ret = btrfs_del_item(trans, root, path);
  202. BUG_ON(ret);
  203. if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
  204. break;
  205. btrfs_release_path(root, path);
  206. }
  207. ret = 0;
  208. btrfs_release_path(root, path);
  209. /* now the directory is empty */
  210. err = btrfs_unlink_trans(trans, root, dir, dentry);
  211. if (!err) {
  212. inode->i_size = 0;
  213. }
  214. out:
  215. btrfs_release_path(root, path);
  216. btrfs_free_path(path);
  217. mutex_unlock(&root->fs_info->fs_mutex);
  218. ret = btrfs_end_transaction(trans, root);
  219. if (ret && !err)
  220. err = ret;
  221. return err;
  222. }
  223. static int btrfs_free_inode(struct btrfs_trans_handle *trans,
  224. struct btrfs_root *root,
  225. struct inode *inode)
  226. {
  227. u64 objectid = inode->i_ino;
  228. struct btrfs_path *path;
  229. struct btrfs_inode_map_item *map;
  230. struct btrfs_key stat_data_key;
  231. int ret;
  232. clear_inode(inode);
  233. path = btrfs_alloc_path();
  234. BUG_ON(!path);
  235. btrfs_init_path(path);
  236. ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
  237. if (ret) {
  238. if (ret > 0)
  239. ret = -ENOENT;
  240. goto error;
  241. }
  242. map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  243. struct btrfs_inode_map_item);
  244. btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
  245. ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
  246. BUG_ON(ret);
  247. btrfs_release_path(root, path);
  248. ret = btrfs_lookup_inode(trans, root, path, objectid, -1);
  249. BUG_ON(ret);
  250. ret = btrfs_del_item(trans, root, path);
  251. BUG_ON(ret);
  252. error:
  253. btrfs_release_path(root, path);
  254. btrfs_free_path(path);
  255. return ret;
  256. }
  257. static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
  258. struct btrfs_root *root,
  259. struct inode *inode)
  260. {
  261. int ret;
  262. struct btrfs_path *path;
  263. struct btrfs_key key;
  264. struct btrfs_disk_key *found_key;
  265. struct btrfs_leaf *leaf;
  266. struct btrfs_file_extent_item *fi = NULL;
  267. u64 extent_start = 0;
  268. u64 extent_num_blocks = 0;
  269. int found_extent;
  270. path = btrfs_alloc_path();
  271. BUG_ON(!path);
  272. /* FIXME, add redo link to tree so we don't leak on crash */
  273. key.objectid = inode->i_ino;
  274. key.offset = (u64)-1;
  275. key.flags = 0;
  276. /*
  277. * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
  278. * or extent data
  279. */
  280. btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
  281. while(1) {
  282. btrfs_init_path(path);
  283. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  284. if (ret < 0) {
  285. goto error;
  286. }
  287. if (ret > 0) {
  288. BUG_ON(path->slots[0] == 0);
  289. path->slots[0]--;
  290. }
  291. leaf = btrfs_buffer_leaf(path->nodes[0]);
  292. found_key = &leaf->items[path->slots[0]].key;
  293. if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
  294. break;
  295. if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
  296. btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
  297. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
  298. break;
  299. if (btrfs_disk_key_offset(found_key) < inode->i_size)
  300. break;
  301. if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
  302. fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  303. path->slots[0],
  304. struct btrfs_file_extent_item);
  305. extent_start = btrfs_file_extent_disk_blocknr(fi);
  306. extent_num_blocks =
  307. btrfs_file_extent_disk_num_blocks(fi);
  308. inode->i_blocks -=
  309. btrfs_file_extent_num_blocks(fi) >> 9;
  310. found_extent = 1;
  311. } else {
  312. found_extent = 0;
  313. }
  314. ret = btrfs_del_item(trans, root, path);
  315. BUG_ON(ret);
  316. btrfs_release_path(root, path);
  317. if (found_extent) {
  318. ret = btrfs_free_extent(trans, root, extent_start,
  319. extent_num_blocks, 0);
  320. BUG_ON(ret);
  321. }
  322. }
  323. ret = 0;
  324. error:
  325. btrfs_release_path(root, path);
  326. btrfs_free_path(path);
  327. return ret;
  328. }
  329. static void btrfs_delete_inode(struct inode *inode)
  330. {
  331. struct btrfs_trans_handle *trans;
  332. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  333. int ret;
  334. truncate_inode_pages(&inode->i_data, 0);
  335. if (is_bad_inode(inode)) {
  336. goto no_delete;
  337. }
  338. inode->i_size = 0;
  339. mutex_lock(&root->fs_info->fs_mutex);
  340. trans = btrfs_start_transaction(root, 1);
  341. if (S_ISREG(inode->i_mode)) {
  342. ret = btrfs_truncate_in_trans(trans, root, inode);
  343. BUG_ON(ret);
  344. }
  345. btrfs_free_inode(trans, root, inode);
  346. btrfs_end_transaction(trans, root);
  347. mutex_unlock(&root->fs_info->fs_mutex);
  348. return;
  349. no_delete:
  350. clear_inode(inode);
  351. }
  352. static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
  353. ino_t *ino)
  354. {
  355. const char *name = dentry->d_name.name;
  356. int namelen = dentry->d_name.len;
  357. struct btrfs_dir_item *di;
  358. struct btrfs_path *path;
  359. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  360. int ret;
  361. path = btrfs_alloc_path();
  362. BUG_ON(!path);
  363. btrfs_init_path(path);
  364. ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
  365. namelen, 0);
  366. if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
  367. *ino = 0;
  368. ret = 0;
  369. goto out;
  370. }
  371. di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  372. struct btrfs_dir_item);
  373. *ino = btrfs_dir_objectid(di);
  374. out:
  375. btrfs_release_path(root, path);
  376. btrfs_free_path(path);
  377. check_inode(dir);
  378. return ret;
  379. }
  380. static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
  381. struct nameidata *nd)
  382. {
  383. struct inode * inode;
  384. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  385. ino_t ino;
  386. int ret;
  387. if (dentry->d_name.len > BTRFS_NAME_LEN)
  388. return ERR_PTR(-ENAMETOOLONG);
  389. mutex_lock(&root->fs_info->fs_mutex);
  390. ret = btrfs_inode_by_name(dir, dentry, &ino);
  391. mutex_unlock(&root->fs_info->fs_mutex);
  392. if (ret < 0)
  393. return ERR_PTR(ret);
  394. inode = NULL;
  395. if (ino) {
  396. inode = iget(dir->i_sb, ino);
  397. if (!inode)
  398. return ERR_PTR(-EACCES);
  399. check_inode(inode);
  400. }
  401. check_inode(dir);
  402. return d_splice_alias(inode, dentry);
  403. }
  404. static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  405. {
  406. struct inode *inode = filp->f_path.dentry->d_inode;
  407. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  408. struct btrfs_item *item;
  409. struct btrfs_dir_item *di;
  410. struct btrfs_key key;
  411. struct btrfs_path *path;
  412. int ret;
  413. u32 nritems;
  414. struct btrfs_leaf *leaf;
  415. int slot;
  416. int advance;
  417. unsigned char d_type = DT_UNKNOWN;
  418. int over = 0;
  419. mutex_lock(&root->fs_info->fs_mutex);
  420. key.objectid = inode->i_ino;
  421. key.flags = 0;
  422. btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
  423. key.offset = filp->f_pos;
  424. path = btrfs_alloc_path();
  425. btrfs_init_path(path);
  426. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  427. if (ret < 0) {
  428. goto err;
  429. }
  430. advance = 0;
  431. while(1) {
  432. leaf = btrfs_buffer_leaf(path->nodes[0]);
  433. nritems = btrfs_header_nritems(&leaf->header);
  434. slot = path->slots[0];
  435. if (advance || slot >= nritems) {
  436. if (slot >= nritems -1) {
  437. ret = btrfs_next_leaf(root, path);
  438. if (ret)
  439. break;
  440. leaf = btrfs_buffer_leaf(path->nodes[0]);
  441. nritems = btrfs_header_nritems(&leaf->header);
  442. slot = path->slots[0];
  443. } else {
  444. slot++;
  445. path->slots[0]++;
  446. }
  447. }
  448. advance = 1;
  449. item = leaf->items + slot;
  450. if (btrfs_disk_key_objectid(&item->key) != key.objectid)
  451. break;
  452. if (btrfs_disk_key_offset(&item->key) >
  453. root->fs_info->highest_inode)
  454. break;
  455. if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_INDEX_KEY)
  456. continue;
  457. if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
  458. continue;
  459. filp->f_pos = btrfs_disk_key_offset(&item->key);
  460. advance = 1;
  461. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  462. over = filldir(dirent, (const char *)(di + 1),
  463. btrfs_dir_name_len(di),
  464. btrfs_disk_key_offset(&item->key),
  465. btrfs_dir_objectid(di), d_type);
  466. if (over)
  467. goto nopos;
  468. }
  469. filp->f_pos++;
  470. nopos:
  471. ret = 0;
  472. err:
  473. btrfs_release_path(root, path);
  474. btrfs_free_path(path);
  475. mutex_unlock(&root->fs_info->fs_mutex);
  476. return ret;
  477. }
  478. static void btrfs_put_super (struct super_block * sb)
  479. {
  480. struct btrfs_root *root = btrfs_sb(sb);
  481. int ret;
  482. ret = close_ctree(root);
  483. if (ret) {
  484. printk("close ctree returns %d\n", ret);
  485. }
  486. sb->s_fs_info = NULL;
  487. }
  488. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  489. {
  490. struct inode * inode;
  491. struct dentry * root_dentry;
  492. struct btrfs_super_block *disk_super;
  493. struct btrfs_root *root;
  494. sb->s_maxbytes = MAX_LFS_FILESIZE;
  495. sb->s_magic = BTRFS_SUPER_MAGIC;
  496. sb->s_op = &btrfs_super_ops;
  497. sb->s_time_gran = 1;
  498. root = open_ctree(sb);
  499. if (!root) {
  500. printk("btrfs: open_ctree failed\n");
  501. return -EIO;
  502. }
  503. sb->s_fs_info = root;
  504. disk_super = root->fs_info->disk_super;
  505. printk("read in super total blocks %Lu root %Lu\n",
  506. btrfs_super_total_blocks(disk_super),
  507. btrfs_super_root_dir(disk_super));
  508. inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
  509. if (!inode)
  510. return -ENOMEM;
  511. if (inode->i_state & I_NEW) {
  512. btrfs_read_locked_inode(inode);
  513. unlock_new_inode(inode);
  514. }
  515. root_dentry = d_alloc_root(inode);
  516. if (!root_dentry) {
  517. iput(inode);
  518. return -ENOMEM;
  519. }
  520. sb->s_root = root_dentry;
  521. return 0;
  522. }
  523. static void fill_inode_item(struct btrfs_inode_item *item,
  524. struct inode *inode)
  525. {
  526. btrfs_set_inode_uid(item, inode->i_uid);
  527. btrfs_set_inode_gid(item, inode->i_gid);
  528. btrfs_set_inode_size(item, inode->i_size);
  529. btrfs_set_inode_mode(item, inode->i_mode);
  530. btrfs_set_inode_nlink(item, inode->i_nlink);
  531. btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
  532. btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
  533. btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
  534. btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
  535. btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
  536. btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
  537. btrfs_set_inode_nblocks(item, inode->i_blocks);
  538. btrfs_set_inode_generation(item, inode->i_generation);
  539. check_inode(inode);
  540. }
  541. static int btrfs_update_inode(struct btrfs_trans_handle *trans,
  542. struct btrfs_root *root,
  543. struct inode *inode)
  544. {
  545. struct btrfs_inode_item *inode_item;
  546. struct btrfs_path *path;
  547. int ret;
  548. path = btrfs_alloc_path();
  549. BUG_ON(!path);
  550. btrfs_init_path(path);
  551. ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
  552. if (ret) {
  553. if (ret > 0)
  554. ret = -ENOENT;
  555. goto failed;
  556. }
  557. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  558. path->slots[0],
  559. struct btrfs_inode_item);
  560. fill_inode_item(inode_item, inode);
  561. btrfs_mark_buffer_dirty(path->nodes[0]);
  562. failed:
  563. btrfs_release_path(root, path);
  564. btrfs_free_path(path);
  565. check_inode(inode);
  566. return 0;
  567. }
  568. static int btrfs_write_inode(struct inode *inode, int wait)
  569. {
  570. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  571. struct btrfs_trans_handle *trans;
  572. int ret;
  573. mutex_lock(&root->fs_info->fs_mutex);
  574. trans = btrfs_start_transaction(root, 1);
  575. ret = btrfs_update_inode(trans, root, inode);
  576. if (wait)
  577. btrfs_commit_transaction(trans, root);
  578. else
  579. btrfs_end_transaction(trans, root);
  580. mutex_unlock(&root->fs_info->fs_mutex);
  581. check_inode(inode);
  582. return ret;
  583. }
  584. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  585. struct inode *dir, int mode)
  586. {
  587. struct inode *inode;
  588. struct btrfs_inode_item inode_item;
  589. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  590. struct btrfs_key key;
  591. int ret;
  592. u64 objectid;
  593. inode = new_inode(dir->i_sb);
  594. if (!inode)
  595. return ERR_PTR(-ENOMEM);
  596. check_inode(inode);
  597. ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  598. BUG_ON(ret);
  599. inode->i_uid = current->fsuid;
  600. inode->i_gid = current->fsgid;
  601. inode->i_mode = mode;
  602. inode->i_ino = objectid;
  603. inode->i_blocks = 0;
  604. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  605. fill_inode_item(&inode_item, inode);
  606. key.objectid = objectid;
  607. key.flags = 0;
  608. key.offset = 0;
  609. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  610. ret = btrfs_insert_inode_map(trans, root, objectid, &key);
  611. BUG_ON(ret);
  612. ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
  613. BUG_ON(ret);
  614. insert_inode_hash(inode);
  615. check_inode(inode);
  616. check_inode(dir);
  617. return inode;
  618. }
  619. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  620. struct dentry *dentry, struct inode *inode)
  621. {
  622. int ret;
  623. ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
  624. dentry->d_name.name, dentry->d_name.len,
  625. dentry->d_parent->d_inode->i_ino,
  626. inode->i_ino, 0);
  627. if (ret == 0) {
  628. dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
  629. ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
  630. dentry->d_parent->d_inode);
  631. }
  632. check_inode(inode);
  633. check_inode(dentry->d_parent->d_inode);
  634. return ret;
  635. }
  636. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  637. struct dentry *dentry, struct inode *inode)
  638. {
  639. int err = btrfs_add_link(trans, dentry, inode);
  640. if (!err) {
  641. d_instantiate(dentry, inode);
  642. return 0;
  643. }
  644. if (err > 0)
  645. err = -EEXIST;
  646. check_inode(inode);
  647. return err;
  648. }
  649. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  650. int mode, struct nameidata *nd)
  651. {
  652. struct btrfs_trans_handle *trans;
  653. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  654. struct inode *inode;
  655. int err;
  656. int drop_inode = 0;
  657. mutex_lock(&root->fs_info->fs_mutex);
  658. trans = btrfs_start_transaction(root, 1);
  659. inode = btrfs_new_inode(trans, dir, mode);
  660. err = PTR_ERR(inode);
  661. if (IS_ERR(inode))
  662. goto out_unlock;
  663. // FIXME mark the inode dirty
  664. err = btrfs_add_nondir(trans, dentry, inode);
  665. if (err)
  666. drop_inode = 1;
  667. else {
  668. inode->i_mapping->a_ops = &btrfs_aops;
  669. inode->i_fop = &btrfs_file_operations;
  670. inode->i_op = &btrfs_file_inode_operations;
  671. }
  672. dir->i_sb->s_dirt = 1;
  673. out_unlock:
  674. btrfs_end_transaction(trans, root);
  675. mutex_unlock(&root->fs_info->fs_mutex);
  676. check_inode(inode);
  677. check_inode(dir);
  678. if (drop_inode) {
  679. inode_dec_link_count(inode);
  680. iput(inode);
  681. }
  682. return err;
  683. }
  684. static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
  685. struct inode *inode, struct inode *dir)
  686. {
  687. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  688. int ret;
  689. char buf[2];
  690. buf[0] = '.';
  691. buf[1] = '.';
  692. ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
  693. inode->i_ino, 1);
  694. if (ret)
  695. goto error;
  696. ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
  697. dir->i_ino, 1);
  698. if (ret)
  699. goto error;
  700. inode->i_size = 6;
  701. ret = btrfs_update_inode(trans, root, inode);
  702. error:
  703. return ret;
  704. }
  705. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  706. {
  707. struct inode *inode;
  708. struct btrfs_trans_handle *trans;
  709. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  710. int err = 0;
  711. int drop_on_err = 0;
  712. mutex_lock(&root->fs_info->fs_mutex);
  713. trans = btrfs_start_transaction(root, 1);
  714. if (IS_ERR(trans)) {
  715. err = PTR_ERR(trans);
  716. goto out_unlock;
  717. }
  718. inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
  719. if (IS_ERR(inode)) {
  720. err = PTR_ERR(inode);
  721. goto out_fail;
  722. }
  723. drop_on_err = 1;
  724. inode->i_op = &btrfs_dir_inode_operations;
  725. inode->i_fop = &btrfs_dir_file_operations;
  726. err = btrfs_make_empty_dir(trans, inode, dir);
  727. if (err)
  728. goto out_fail;
  729. err = btrfs_add_link(trans, dentry, inode);
  730. if (err)
  731. goto out_fail;
  732. d_instantiate(dentry, inode);
  733. drop_on_err = 0;
  734. out_fail:
  735. btrfs_end_transaction(trans, root);
  736. out_unlock:
  737. mutex_unlock(&root->fs_info->fs_mutex);
  738. if (drop_on_err)
  739. iput(inode);
  740. return err;
  741. }
  742. static int btrfs_sync_fs(struct super_block *sb, int wait)
  743. {
  744. struct btrfs_trans_handle *trans;
  745. struct btrfs_root *root;
  746. int ret;
  747. root = btrfs_sb(sb);
  748. sb->s_dirt = 0;
  749. if (!wait) {
  750. filemap_flush(root->fs_info->btree_inode->i_mapping);
  751. return 0;
  752. }
  753. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  754. mutex_lock(&root->fs_info->fs_mutex);
  755. trans = btrfs_start_transaction(root, 1);
  756. ret = btrfs_commit_transaction(trans, root);
  757. sb->s_dirt = 0;
  758. BUG_ON(ret);
  759. printk("btrfs sync_fs\n");
  760. mutex_unlock(&root->fs_info->fs_mutex);
  761. return 0;
  762. }
  763. #if 0
  764. static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
  765. struct buffer_head *result, int create)
  766. {
  767. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  768. struct btrfs_path *path;
  769. struct btrfs_key key;
  770. struct btrfs_leaf *leaf;
  771. int num_bytes = result->b_size;
  772. int item_size;
  773. int ret;
  774. u64 pos;
  775. char *ptr;
  776. int copy_size;
  777. int err = 0;
  778. char *safe_ptr;
  779. char *data_ptr;
  780. path = btrfs_alloc_path();
  781. BUG_ON(!path);
  782. WARN_ON(create);
  783. if (create) {
  784. return 0;
  785. }
  786. pos = iblock << inode->i_blkbits;
  787. key.objectid = inode->i_ino;
  788. key.flags = 0;
  789. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  790. ptr = kmap(result->b_page);
  791. safe_ptr = ptr;
  792. ptr += (pos & (PAGE_CACHE_SIZE -1));
  793. again:
  794. key.offset = pos;
  795. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  796. if (ret) {
  797. if (ret < 0)
  798. err = ret;
  799. else
  800. err = 0;
  801. goto out;
  802. }
  803. leaf = btrfs_buffer_leaf(path->nodes[0]);
  804. item_size = btrfs_item_size(leaf->items + path->slots[0]);
  805. copy_size = min(num_bytes, item_size);
  806. data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
  807. WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
  808. memcpy(ptr, data_ptr, copy_size);
  809. pos += copy_size;
  810. num_bytes -= copy_size;
  811. WARN_ON(num_bytes < 0);
  812. ptr += copy_size;
  813. btrfs_release_path(root, path);
  814. if (num_bytes != 0) {
  815. if (pos >= i_size_read(inode))
  816. memset(ptr, 0, num_bytes);
  817. else
  818. goto again;
  819. }
  820. set_buffer_uptodate(result);
  821. map_bh(result, inode->i_sb, 0);
  822. err = 0;
  823. out:
  824. btrfs_free_path(path);
  825. kunmap(result->b_page);
  826. return err;
  827. }
  828. #endif
  829. static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
  830. struct buffer_head *result, int create)
  831. {
  832. int ret;
  833. int err = 0;
  834. u64 blocknr;
  835. u64 extent_start = 0;
  836. u64 extent_end = 0;
  837. u64 objectid = inode->i_ino;
  838. struct btrfs_path *path;
  839. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  840. struct btrfs_trans_handle *trans = NULL;
  841. struct btrfs_file_extent_item *item;
  842. struct btrfs_leaf *leaf;
  843. struct btrfs_disk_key *found_key;
  844. path = btrfs_alloc_path();
  845. BUG_ON(!path);
  846. btrfs_init_path(path);
  847. if (create)
  848. trans = btrfs_start_transaction(root, 1);
  849. ret = btrfs_lookup_file_extent(trans, root, path,
  850. inode->i_ino,
  851. iblock << inode->i_blkbits, 0);
  852. if (ret < 0) {
  853. err = ret;
  854. goto out;
  855. }
  856. if (ret != 0) {
  857. if (path->slots[0] == 0) {
  858. btrfs_release_path(root, path);
  859. goto allocate;
  860. }
  861. path->slots[0]--;
  862. }
  863. item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  864. struct btrfs_file_extent_item);
  865. leaf = btrfs_buffer_leaf(path->nodes[0]);
  866. blocknr = btrfs_file_extent_disk_blocknr(item);
  867. blocknr += btrfs_file_extent_offset(item);
  868. /* exact match found, use it */
  869. if (ret == 0) {
  870. err = 0;
  871. map_bh(result, inode->i_sb, blocknr);
  872. goto out;
  873. }
  874. /* are we inside the extent that was found? */
  875. found_key = &leaf->items[path->slots[0]].key;
  876. if (btrfs_disk_key_objectid(found_key) != objectid ||
  877. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
  878. extent_end = 0;
  879. extent_start = 0;
  880. btrfs_release_path(root, path);
  881. goto allocate;
  882. }
  883. extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
  884. extent_start = extent_start >> inode->i_blkbits;
  885. extent_start += btrfs_file_extent_offset(item);
  886. extent_end = extent_start + btrfs_file_extent_num_blocks(item);
  887. if (iblock >= extent_start && iblock < extent_end) {
  888. err = 0;
  889. map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
  890. goto out;
  891. }
  892. allocate:
  893. /* ok, create a new extent */
  894. if (!create) {
  895. err = 0;
  896. goto out;
  897. }
  898. ret = btrfs_alloc_file_extent(trans, root, objectid,
  899. iblock << inode->i_blkbits,
  900. 1, extent_end, &blocknr);
  901. if (ret) {
  902. err = ret;
  903. goto out;
  904. }
  905. inode->i_blocks += inode->i_sb->s_blocksize >> 9;
  906. set_buffer_new(result);
  907. map_bh(result, inode->i_sb, blocknr);
  908. out:
  909. btrfs_release_path(root, path);
  910. btrfs_free_path(path);
  911. if (trans)
  912. btrfs_end_transaction(trans, root);
  913. return err;
  914. }
  915. static int btrfs_get_block(struct inode *inode, sector_t iblock,
  916. struct buffer_head *result, int create)
  917. {
  918. int err;
  919. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  920. mutex_lock(&root->fs_info->fs_mutex);
  921. err = btrfs_get_block_lock(inode, iblock, result, create);
  922. // err = btrfs_get_block_inline(inode, iblock, result, create);
  923. mutex_unlock(&root->fs_info->fs_mutex);
  924. return err;
  925. }
  926. static int btrfs_prepare_write(struct file *file, struct page *page,
  927. unsigned from, unsigned to)
  928. {
  929. return nobh_prepare_write(page, from, to, btrfs_get_block);
  930. }
  931. static int btrfs_commit_write(struct file *file, struct page *page,
  932. unsigned from, unsigned to)
  933. {
  934. return nobh_commit_write(file, page, from, to);
  935. }
  936. static void btrfs_write_super(struct super_block *sb)
  937. {
  938. btrfs_sync_fs(sb, 1);
  939. }
  940. static int btrfs_readpage(struct file *file, struct page *page)
  941. {
  942. return mpage_readpage(page, btrfs_get_block);
  943. }
  944. static int btrfs_readpages(struct file *file, struct address_space *mapping,
  945. struct list_head *pages, unsigned nr_pages)
  946. {
  947. return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
  948. }
  949. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  950. {
  951. return nobh_writepage(page, btrfs_get_block, wbc);
  952. }
  953. static void btrfs_truncate(struct inode *inode)
  954. {
  955. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  956. int ret;
  957. struct btrfs_trans_handle *trans;
  958. if (!S_ISREG(inode->i_mode))
  959. return;
  960. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  961. return;
  962. nobh_truncate_page(inode->i_mapping, inode->i_size);
  963. /* FIXME, add redo link to tree so we don't leak on crash */
  964. mutex_lock(&root->fs_info->fs_mutex);
  965. trans = btrfs_start_transaction(root, 1);
  966. ret = btrfs_truncate_in_trans(trans, root, inode);
  967. BUG_ON(ret);
  968. ret = btrfs_end_transaction(trans, root);
  969. BUG_ON(ret);
  970. mutex_unlock(&root->fs_info->fs_mutex);
  971. mark_inode_dirty(inode);
  972. }
  973. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  974. struct page **prepared_pages,
  975. const char __user * buf)
  976. {
  977. long page_fault = 0;
  978. int i;
  979. int offset = pos & (PAGE_CACHE_SIZE - 1);
  980. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  981. size_t count = min_t(size_t,
  982. PAGE_CACHE_SIZE - offset, write_bytes);
  983. struct page *page = prepared_pages[i];
  984. fault_in_pages_readable(buf, count);
  985. /* Copy data from userspace to the current page */
  986. kmap(page);
  987. page_fault = __copy_from_user(page_address(page) + offset,
  988. buf, count);
  989. /* Flush processor's dcache for this page */
  990. flush_dcache_page(page);
  991. kunmap(page);
  992. buf += count;
  993. write_bytes -= count;
  994. if (page_fault)
  995. break;
  996. }
  997. return page_fault ? -EFAULT : 0;
  998. }
  999. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  1000. {
  1001. size_t i;
  1002. for (i = 0; i < num_pages; i++) {
  1003. if (!pages[i])
  1004. break;
  1005. unlock_page(pages[i]);
  1006. mark_page_accessed(pages[i]);
  1007. page_cache_release(pages[i]);
  1008. }
  1009. }
  1010. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  1011. struct btrfs_root *root,
  1012. struct file *file,
  1013. struct page **pages,
  1014. size_t num_pages,
  1015. loff_t pos,
  1016. size_t write_bytes)
  1017. {
  1018. int i;
  1019. int offset;
  1020. int err = 0;
  1021. int ret;
  1022. int this_write;
  1023. struct inode *inode = file->f_path.dentry->d_inode;
  1024. for (i = 0; i < num_pages; i++) {
  1025. offset = pos & (PAGE_CACHE_SIZE -1);
  1026. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1027. /* FIXME, one block at a time */
  1028. mutex_lock(&root->fs_info->fs_mutex);
  1029. trans = btrfs_start_transaction(root, 1);
  1030. btrfs_csum_file_block(trans, root, inode->i_ino,
  1031. pages[i]->index << PAGE_CACHE_SHIFT,
  1032. kmap(pages[i]), PAGE_CACHE_SIZE);
  1033. kunmap(pages[i]);
  1034. SetPageChecked(pages[i]);
  1035. ret = btrfs_end_transaction(trans, root);
  1036. BUG_ON(ret);
  1037. mutex_unlock(&root->fs_info->fs_mutex);
  1038. ret = nobh_commit_write(file, pages[i], offset,
  1039. offset + this_write);
  1040. pos += this_write;
  1041. if (ret) {
  1042. err = ret;
  1043. goto failed;
  1044. }
  1045. WARN_ON(this_write > write_bytes);
  1046. write_bytes -= this_write;
  1047. }
  1048. failed:
  1049. return err;
  1050. }
  1051. static int prepare_pages(struct btrfs_trans_handle *trans,
  1052. struct btrfs_root *root,
  1053. struct file *file,
  1054. struct page **pages,
  1055. size_t num_pages,
  1056. loff_t pos,
  1057. size_t write_bytes)
  1058. {
  1059. int i;
  1060. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1061. struct inode *inode = file->f_path.dentry->d_inode;
  1062. int offset;
  1063. int err = 0;
  1064. int ret;
  1065. int this_write;
  1066. loff_t isize = i_size_read(inode);
  1067. memset(pages, 0, num_pages * sizeof(struct page *));
  1068. for (i = 0; i < num_pages; i++) {
  1069. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  1070. if (!pages[i]) {
  1071. err = -ENOMEM;
  1072. goto failed_release;
  1073. }
  1074. offset = pos & (PAGE_CACHE_SIZE -1);
  1075. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1076. ret = nobh_prepare_write(pages[i], offset,
  1077. offset + this_write,
  1078. btrfs_get_block);
  1079. pos += this_write;
  1080. if (ret) {
  1081. err = ret;
  1082. goto failed_truncate;
  1083. }
  1084. WARN_ON(this_write > write_bytes);
  1085. write_bytes -= this_write;
  1086. }
  1087. return 0;
  1088. failed_release:
  1089. btrfs_drop_pages(pages, num_pages);
  1090. return err;
  1091. failed_truncate:
  1092. btrfs_drop_pages(pages, num_pages);
  1093. if (pos > isize)
  1094. vmtruncate(inode, isize);
  1095. return err;
  1096. }
  1097. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  1098. size_t count, loff_t *ppos)
  1099. {
  1100. loff_t pos;
  1101. size_t num_written = 0;
  1102. int err = 0;
  1103. int ret = 0;
  1104. struct inode *inode = file->f_path.dentry->d_inode;
  1105. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  1106. struct page *pages[1];
  1107. if (file->f_flags & O_DIRECT)
  1108. return -EINVAL;
  1109. pos = *ppos;
  1110. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1111. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1112. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1113. if (err)
  1114. goto out;
  1115. if (count == 0)
  1116. goto out;
  1117. err = remove_suid(file->f_path.dentry);
  1118. if (err)
  1119. goto out;
  1120. file_update_time(file);
  1121. mutex_lock(&inode->i_mutex);
  1122. while(count > 0) {
  1123. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1124. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1125. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1126. PAGE_CACHE_SHIFT;
  1127. ret = prepare_pages(NULL, root, file, pages, num_pages,
  1128. pos, write_bytes);
  1129. BUG_ON(ret);
  1130. ret = btrfs_copy_from_user(pos, num_pages,
  1131. write_bytes, pages, buf);
  1132. BUG_ON(ret);
  1133. ret = dirty_and_release_pages(NULL, root, file, pages,
  1134. num_pages, pos, write_bytes);
  1135. BUG_ON(ret);
  1136. btrfs_drop_pages(pages, num_pages);
  1137. buf += write_bytes;
  1138. count -= write_bytes;
  1139. pos += write_bytes;
  1140. num_written += write_bytes;
  1141. balance_dirty_pages_ratelimited(inode->i_mapping);
  1142. cond_resched();
  1143. }
  1144. mutex_unlock(&inode->i_mutex);
  1145. out:
  1146. *ppos = pos;
  1147. current->backing_dev_info = NULL;
  1148. return num_written ? num_written : err;
  1149. }
  1150. #if 0
  1151. static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
  1152. struct page *page, loff_t pos,
  1153. size_t offset, size_t write_bytes)
  1154. {
  1155. struct btrfs_path *path;
  1156. struct btrfs_trans_handle *trans;
  1157. struct btrfs_key key;
  1158. struct btrfs_leaf *leaf;
  1159. struct btrfs_key found_key;
  1160. int ret;
  1161. size_t copy_size = 0;
  1162. char *dst = NULL;
  1163. int err = 0;
  1164. size_t num_written = 0;
  1165. path = btrfs_alloc_path();
  1166. BUG_ON(!path);
  1167. mutex_lock(&root->fs_info->fs_mutex);
  1168. trans = btrfs_start_transaction(root, 1);
  1169. key.objectid = inode->i_ino;
  1170. key.flags = 0;
  1171. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  1172. again:
  1173. key.offset = pos;
  1174. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1175. if (ret < 0) {
  1176. err = ret;
  1177. goto out;
  1178. }
  1179. if (ret == 0) {
  1180. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1181. btrfs_disk_key_to_cpu(&found_key,
  1182. &leaf->items[path->slots[0]].key);
  1183. copy_size = btrfs_item_size(leaf->items + path->slots[0]);
  1184. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1185. copy_size = min(write_bytes, copy_size);
  1186. goto copyit;
  1187. } else {
  1188. int slot = path->slots[0];
  1189. if (slot > 0) {
  1190. slot--;
  1191. }
  1192. // FIXME find max key
  1193. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1194. btrfs_disk_key_to_cpu(&found_key,
  1195. &leaf->items[slot].key);
  1196. if (found_key.objectid != inode->i_ino)
  1197. goto insert;
  1198. if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
  1199. goto insert;
  1200. copy_size = btrfs_item_size(leaf->items + slot);
  1201. if (found_key.offset + copy_size <= pos)
  1202. goto insert;
  1203. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1204. dst += pos - found_key.offset;
  1205. copy_size = copy_size - (pos - found_key.offset);
  1206. BUG_ON(copy_size < 0);
  1207. copy_size = min(write_bytes, copy_size);
  1208. WARN_ON(copy_size == 0);
  1209. goto copyit;
  1210. }
  1211. insert:
  1212. btrfs_release_path(root, path);
  1213. copy_size = min(write_bytes,
  1214. (size_t)BTRFS_LEAF_DATA_SIZE(root) -
  1215. sizeof(struct btrfs_item) * 4);
  1216. ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
  1217. BUG_ON(ret);
  1218. dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1219. path->slots[0], char);
  1220. copyit:
  1221. WARN_ON(copy_size == 0);
  1222. WARN_ON(dst + copy_size >
  1223. btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1224. path->slots[0], char) +
  1225. btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
  1226. path->slots[0]));
  1227. btrfs_memcpy(root, path->nodes[0]->b_data, dst,
  1228. page_address(page) + offset, copy_size);
  1229. mark_buffer_dirty(path->nodes[0]);
  1230. btrfs_release_path(root, path);
  1231. pos += copy_size;
  1232. offset += copy_size;
  1233. num_written += copy_size;
  1234. write_bytes -= copy_size;
  1235. if (write_bytes)
  1236. goto again;
  1237. out:
  1238. btrfs_free_path(path);
  1239. ret = btrfs_end_transaction(trans, root);
  1240. BUG_ON(ret);
  1241. mutex_unlock(&root->fs_info->fs_mutex);
  1242. return num_written ? num_written : err;
  1243. }
  1244. static ssize_t btrfs_file_inline_write(struct file *file,
  1245. const char __user *buf,
  1246. size_t count, loff_t *ppos)
  1247. {
  1248. loff_t pos;
  1249. size_t num_written = 0;
  1250. int err = 0;
  1251. int ret = 0;
  1252. struct inode *inode = file->f_path.dentry->d_inode;
  1253. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  1254. unsigned long page_index;
  1255. if (file->f_flags & O_DIRECT)
  1256. return -EINVAL;
  1257. pos = *ppos;
  1258. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1259. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1260. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1261. if (err)
  1262. goto out;
  1263. if (count == 0)
  1264. goto out;
  1265. err = remove_suid(file->f_path.dentry);
  1266. if (err)
  1267. goto out;
  1268. file_update_time(file);
  1269. mutex_lock(&inode->i_mutex);
  1270. while(count > 0) {
  1271. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1272. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1273. struct page *page;
  1274. page_index = pos >> PAGE_CACHE_SHIFT;
  1275. page = grab_cache_page(inode->i_mapping, page_index);
  1276. if (!PageUptodate(page)) {
  1277. ret = mpage_readpage(page, btrfs_get_block);
  1278. BUG_ON(ret);
  1279. lock_page(page);
  1280. }
  1281. ret = btrfs_copy_from_user(pos, 1,
  1282. write_bytes, &page, buf);
  1283. BUG_ON(ret);
  1284. write_bytes = inline_one_page(root, inode, page, pos,
  1285. offset, write_bytes);
  1286. SetPageUptodate(page);
  1287. if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
  1288. i_size_write(inode, pos + write_bytes);
  1289. mark_inode_dirty(inode);
  1290. }
  1291. page_cache_release(page);
  1292. unlock_page(page);
  1293. if (write_bytes < 0)
  1294. goto out_unlock;
  1295. buf += write_bytes;
  1296. count -= write_bytes;
  1297. pos += write_bytes;
  1298. num_written += write_bytes;
  1299. balance_dirty_pages_ratelimited(inode->i_mapping);
  1300. cond_resched();
  1301. }
  1302. out_unlock:
  1303. mutex_unlock(&inode->i_mutex);
  1304. out:
  1305. *ppos = pos;
  1306. current->backing_dev_info = NULL;
  1307. return num_written ? num_written : err;
  1308. }
  1309. #endif
  1310. static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
  1311. unsigned long offset, unsigned long size)
  1312. {
  1313. char *kaddr;
  1314. unsigned long left, count = desc->count;
  1315. if (size > count)
  1316. size = count;
  1317. if (!PageChecked(page)) {
  1318. /* FIXME, do it per block */
  1319. struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
  1320. int ret = btrfs_csum_verify_file_block(root,
  1321. page->mapping->host->i_ino,
  1322. page->index << PAGE_CACHE_SHIFT,
  1323. kmap(page), PAGE_CACHE_SIZE);
  1324. if (ret) {
  1325. printk("failed to verify ino %lu page %lu\n",
  1326. page->mapping->host->i_ino,
  1327. page->index);
  1328. memset(page_address(page), 0, PAGE_CACHE_SIZE);
  1329. }
  1330. SetPageChecked(page);
  1331. kunmap(page);
  1332. }
  1333. /*
  1334. * Faults on the destination of a read are common, so do it before
  1335. * taking the kmap.
  1336. */
  1337. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  1338. kaddr = kmap_atomic(page, KM_USER0);
  1339. left = __copy_to_user_inatomic(desc->arg.buf,
  1340. kaddr + offset, size);
  1341. kunmap_atomic(kaddr, KM_USER0);
  1342. if (left == 0)
  1343. goto success;
  1344. }
  1345. /* Do it the slow way */
  1346. kaddr = kmap(page);
  1347. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1348. kunmap(page);
  1349. if (left) {
  1350. size -= left;
  1351. desc->error = -EFAULT;
  1352. }
  1353. success:
  1354. desc->count = count - size;
  1355. desc->written += size;
  1356. desc->arg.buf += size;
  1357. return size;
  1358. }
  1359. /**
  1360. * btrfs_file_aio_read - filesystem read routine
  1361. * @iocb: kernel I/O control block
  1362. * @iov: io vector request
  1363. * @nr_segs: number of segments in the iovec
  1364. * @pos: current file position
  1365. */
  1366. static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1367. unsigned long nr_segs, loff_t pos)
  1368. {
  1369. struct file *filp = iocb->ki_filp;
  1370. ssize_t retval;
  1371. unsigned long seg;
  1372. size_t count;
  1373. loff_t *ppos = &iocb->ki_pos;
  1374. count = 0;
  1375. for (seg = 0; seg < nr_segs; seg++) {
  1376. const struct iovec *iv = &iov[seg];
  1377. /*
  1378. * If any segment has a negative length, or the cumulative
  1379. * length ever wraps negative then return -EINVAL.
  1380. */
  1381. count += iv->iov_len;
  1382. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  1383. return -EINVAL;
  1384. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  1385. continue;
  1386. if (seg == 0)
  1387. return -EFAULT;
  1388. nr_segs = seg;
  1389. count -= iv->iov_len; /* This segment is no good */
  1390. break;
  1391. }
  1392. retval = 0;
  1393. if (count) {
  1394. for (seg = 0; seg < nr_segs; seg++) {
  1395. read_descriptor_t desc;
  1396. desc.written = 0;
  1397. desc.arg.buf = iov[seg].iov_base;
  1398. desc.count = iov[seg].iov_len;
  1399. if (desc.count == 0)
  1400. continue;
  1401. desc.error = 0;
  1402. do_generic_file_read(filp, ppos, &desc,
  1403. btrfs_read_actor);
  1404. retval += desc.written;
  1405. if (desc.error) {
  1406. retval = retval ?: desc.error;
  1407. break;
  1408. }
  1409. }
  1410. }
  1411. return retval;
  1412. }
  1413. static struct kmem_cache *btrfs_inode_cachep;
  1414. struct kmem_cache *btrfs_trans_handle_cachep;
  1415. struct kmem_cache *btrfs_transaction_cachep;
  1416. struct kmem_cache *btrfs_bit_radix_cachep;
  1417. struct kmem_cache *btrfs_path_cachep;
  1418. /*
  1419. * Called inside transaction, so use GFP_NOFS
  1420. */
  1421. static struct inode *btrfs_alloc_inode(struct super_block *sb)
  1422. {
  1423. struct btrfs_inode *ei;
  1424. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  1425. if (!ei)
  1426. return NULL;
  1427. ei->magic = 0xDEADBEEF;
  1428. ei->magic2 = 0xDEADBEAF;
  1429. return &ei->vfs_inode;
  1430. }
  1431. static void btrfs_destroy_inode(struct inode *inode)
  1432. {
  1433. struct btrfs_inode *ei = BTRFS_I(inode);
  1434. WARN_ON(ei->magic != 0xDEADBEEF);
  1435. WARN_ON(ei->magic2 != 0xDEADBEAF);
  1436. WARN_ON(!list_empty(&inode->i_dentry));
  1437. WARN_ON(inode->i_data.nrpages);
  1438. ei->magic = 0;
  1439. ei->magic2 = 0;
  1440. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  1441. }
  1442. static void init_once(void * foo, struct kmem_cache * cachep,
  1443. unsigned long flags)
  1444. {
  1445. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  1446. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1447. SLAB_CTOR_CONSTRUCTOR) {
  1448. inode_init_once(&ei->vfs_inode);
  1449. }
  1450. }
  1451. static int init_inodecache(void)
  1452. {
  1453. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  1454. sizeof(struct btrfs_inode),
  1455. 0, (SLAB_RECLAIM_ACCOUNT|
  1456. SLAB_MEM_SPREAD),
  1457. init_once, NULL);
  1458. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  1459. sizeof(struct btrfs_trans_handle),
  1460. 0, (SLAB_RECLAIM_ACCOUNT|
  1461. SLAB_MEM_SPREAD),
  1462. NULL, NULL);
  1463. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  1464. sizeof(struct btrfs_transaction),
  1465. 0, (SLAB_RECLAIM_ACCOUNT|
  1466. SLAB_MEM_SPREAD),
  1467. NULL, NULL);
  1468. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  1469. sizeof(struct btrfs_transaction),
  1470. 0, (SLAB_RECLAIM_ACCOUNT|
  1471. SLAB_MEM_SPREAD),
  1472. NULL, NULL);
  1473. btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
  1474. 256,
  1475. 0, (SLAB_RECLAIM_ACCOUNT|
  1476. SLAB_MEM_SPREAD |
  1477. SLAB_DESTROY_BY_RCU),
  1478. NULL, NULL);
  1479. if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
  1480. btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
  1481. return -ENOMEM;
  1482. return 0;
  1483. }
  1484. static void destroy_inodecache(void)
  1485. {
  1486. kmem_cache_destroy(btrfs_inode_cachep);
  1487. kmem_cache_destroy(btrfs_trans_handle_cachep);
  1488. kmem_cache_destroy(btrfs_transaction_cachep);
  1489. kmem_cache_destroy(btrfs_bit_radix_cachep);
  1490. kmem_cache_destroy(btrfs_path_cachep);
  1491. }
  1492. static int btrfs_get_sb(struct file_system_type *fs_type,
  1493. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  1494. {
  1495. return get_sb_bdev(fs_type, flags, dev_name, data,
  1496. btrfs_fill_super, mnt);
  1497. }
  1498. static struct file_system_type btrfs_fs_type = {
  1499. .owner = THIS_MODULE,
  1500. .name = "btrfs",
  1501. .get_sb = btrfs_get_sb,
  1502. .kill_sb = kill_block_super,
  1503. .fs_flags = FS_REQUIRES_DEV,
  1504. };
  1505. static struct super_operations btrfs_super_ops = {
  1506. .statfs = simple_statfs,
  1507. .delete_inode = btrfs_delete_inode,
  1508. .put_super = btrfs_put_super,
  1509. .read_inode = btrfs_read_locked_inode,
  1510. .write_super = btrfs_write_super,
  1511. .sync_fs = btrfs_sync_fs,
  1512. .write_inode = btrfs_write_inode,
  1513. .alloc_inode = btrfs_alloc_inode,
  1514. .destroy_inode = btrfs_destroy_inode,
  1515. };
  1516. static struct inode_operations btrfs_dir_inode_operations = {
  1517. .lookup = btrfs_lookup,
  1518. .create = btrfs_create,
  1519. .unlink = btrfs_unlink,
  1520. .mkdir = btrfs_mkdir,
  1521. .rmdir = btrfs_rmdir,
  1522. };
  1523. static struct file_operations btrfs_dir_file_operations = {
  1524. .llseek = generic_file_llseek,
  1525. .read = generic_read_dir,
  1526. .readdir = btrfs_readdir,
  1527. };
  1528. static struct address_space_operations btrfs_aops = {
  1529. .readpage = btrfs_readpage,
  1530. .readpages = btrfs_readpages,
  1531. .writepage = btrfs_writepage,
  1532. .sync_page = block_sync_page,
  1533. .prepare_write = btrfs_prepare_write,
  1534. .commit_write = btrfs_commit_write,
  1535. };
  1536. static struct inode_operations btrfs_file_inode_operations = {
  1537. .truncate = btrfs_truncate,
  1538. };
  1539. static struct file_operations btrfs_file_operations = {
  1540. .llseek = generic_file_llseek,
  1541. .read = do_sync_read,
  1542. .aio_read = btrfs_file_aio_read,
  1543. .write = btrfs_file_write,
  1544. .mmap = generic_file_mmap,
  1545. .open = generic_file_open,
  1546. };
  1547. static int __init init_btrfs_fs(void)
  1548. {
  1549. int err;
  1550. printk("btrfs loaded!\n");
  1551. err = init_inodecache();
  1552. if (err)
  1553. return err;
  1554. return register_filesystem(&btrfs_fs_type);
  1555. }
  1556. static void __exit exit_btrfs_fs(void)
  1557. {
  1558. destroy_inodecache();
  1559. unregister_filesystem(&btrfs_fs_type);
  1560. printk("btrfs unloaded\n");
  1561. }
  1562. module_init(init_btrfs_fs)
  1563. module_exit(exit_btrfs_fs)
  1564. MODULE_LICENSE("GPL");