super.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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_type(&item->key) != BTRFS_DIR_INDEX_KEY)
  453. continue;
  454. if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
  455. continue;
  456. filp->f_pos = btrfs_disk_key_offset(&item->key);
  457. advance = 1;
  458. di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
  459. over = filldir(dirent, (const char *)(di + 1),
  460. btrfs_dir_name_len(di),
  461. btrfs_disk_key_offset(&item->key),
  462. btrfs_dir_objectid(di), d_type);
  463. if (over)
  464. goto nopos;
  465. }
  466. filp->f_pos++;
  467. nopos:
  468. ret = 0;
  469. err:
  470. btrfs_release_path(root, path);
  471. btrfs_free_path(path);
  472. mutex_unlock(&root->fs_info->fs_mutex);
  473. return ret;
  474. }
  475. static void btrfs_put_super (struct super_block * sb)
  476. {
  477. struct btrfs_root *root = btrfs_sb(sb);
  478. int ret;
  479. ret = close_ctree(root);
  480. if (ret) {
  481. printk("close ctree returns %d\n", ret);
  482. }
  483. sb->s_fs_info = NULL;
  484. }
  485. static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
  486. {
  487. struct inode * inode;
  488. struct dentry * root_dentry;
  489. struct btrfs_super_block *disk_super;
  490. struct btrfs_root *root;
  491. sb->s_maxbytes = MAX_LFS_FILESIZE;
  492. sb->s_magic = BTRFS_SUPER_MAGIC;
  493. sb->s_op = &btrfs_super_ops;
  494. sb->s_time_gran = 1;
  495. root = open_ctree(sb);
  496. if (!root) {
  497. printk("btrfs: open_ctree failed\n");
  498. return -EIO;
  499. }
  500. sb->s_fs_info = root;
  501. disk_super = root->fs_info->disk_super;
  502. printk("read in super total blocks %Lu root %Lu\n",
  503. btrfs_super_total_blocks(disk_super),
  504. btrfs_super_root_dir(disk_super));
  505. inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
  506. if (!inode)
  507. return -ENOMEM;
  508. if (inode->i_state & I_NEW) {
  509. btrfs_read_locked_inode(inode);
  510. unlock_new_inode(inode);
  511. }
  512. root_dentry = d_alloc_root(inode);
  513. if (!root_dentry) {
  514. iput(inode);
  515. return -ENOMEM;
  516. }
  517. sb->s_root = root_dentry;
  518. return 0;
  519. }
  520. static void fill_inode_item(struct btrfs_inode_item *item,
  521. struct inode *inode)
  522. {
  523. btrfs_set_inode_uid(item, inode->i_uid);
  524. btrfs_set_inode_gid(item, inode->i_gid);
  525. btrfs_set_inode_size(item, inode->i_size);
  526. btrfs_set_inode_mode(item, inode->i_mode);
  527. btrfs_set_inode_nlink(item, inode->i_nlink);
  528. btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
  529. btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
  530. btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
  531. btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
  532. btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
  533. btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
  534. btrfs_set_inode_nblocks(item, inode->i_blocks);
  535. btrfs_set_inode_generation(item, inode->i_generation);
  536. check_inode(inode);
  537. }
  538. static int btrfs_update_inode(struct btrfs_trans_handle *trans,
  539. struct btrfs_root *root,
  540. struct inode *inode)
  541. {
  542. struct btrfs_inode_item *inode_item;
  543. struct btrfs_path *path;
  544. int ret;
  545. path = btrfs_alloc_path();
  546. BUG_ON(!path);
  547. btrfs_init_path(path);
  548. ret = btrfs_lookup_inode(trans, root, path, inode->i_ino, 1);
  549. if (ret) {
  550. if (ret > 0)
  551. ret = -ENOENT;
  552. goto failed;
  553. }
  554. inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  555. path->slots[0],
  556. struct btrfs_inode_item);
  557. fill_inode_item(inode_item, inode);
  558. btrfs_mark_buffer_dirty(path->nodes[0]);
  559. failed:
  560. btrfs_release_path(root, path);
  561. btrfs_free_path(path);
  562. check_inode(inode);
  563. return 0;
  564. }
  565. static int btrfs_write_inode(struct inode *inode, int wait)
  566. {
  567. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  568. struct btrfs_trans_handle *trans;
  569. int ret;
  570. mutex_lock(&root->fs_info->fs_mutex);
  571. trans = btrfs_start_transaction(root, 1);
  572. ret = btrfs_update_inode(trans, root, inode);
  573. if (wait)
  574. btrfs_commit_transaction(trans, root);
  575. else
  576. btrfs_end_transaction(trans, root);
  577. mutex_unlock(&root->fs_info->fs_mutex);
  578. check_inode(inode);
  579. return ret;
  580. }
  581. static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
  582. struct inode *dir, int mode)
  583. {
  584. struct inode *inode;
  585. struct btrfs_inode_item inode_item;
  586. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  587. struct btrfs_key key;
  588. int ret;
  589. u64 objectid;
  590. inode = new_inode(dir->i_sb);
  591. if (!inode)
  592. return ERR_PTR(-ENOMEM);
  593. check_inode(inode);
  594. ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
  595. BUG_ON(ret);
  596. inode->i_uid = current->fsuid;
  597. inode->i_gid = current->fsgid;
  598. inode->i_mode = mode;
  599. inode->i_ino = objectid;
  600. inode->i_blocks = 0;
  601. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  602. fill_inode_item(&inode_item, inode);
  603. key.objectid = objectid;
  604. key.flags = 0;
  605. key.offset = 0;
  606. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  607. ret = btrfs_insert_inode_map(trans, root, objectid, &key);
  608. BUG_ON(ret);
  609. ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
  610. BUG_ON(ret);
  611. insert_inode_hash(inode);
  612. check_inode(inode);
  613. check_inode(dir);
  614. return inode;
  615. }
  616. static int btrfs_add_link(struct btrfs_trans_handle *trans,
  617. struct dentry *dentry, struct inode *inode)
  618. {
  619. int ret;
  620. ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
  621. dentry->d_name.name, dentry->d_name.len,
  622. dentry->d_parent->d_inode->i_ino,
  623. inode->i_ino, 0);
  624. if (ret == 0) {
  625. dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
  626. ret = btrfs_update_inode(trans, btrfs_sb(inode->i_sb),
  627. dentry->d_parent->d_inode);
  628. }
  629. check_inode(inode);
  630. check_inode(dentry->d_parent->d_inode);
  631. return ret;
  632. }
  633. static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
  634. struct dentry *dentry, struct inode *inode)
  635. {
  636. int err = btrfs_add_link(trans, dentry, inode);
  637. if (!err) {
  638. d_instantiate(dentry, inode);
  639. return 0;
  640. }
  641. if (err > 0)
  642. err = -EEXIST;
  643. check_inode(inode);
  644. return err;
  645. }
  646. static int btrfs_create(struct inode *dir, struct dentry *dentry,
  647. int mode, struct nameidata *nd)
  648. {
  649. struct btrfs_trans_handle *trans;
  650. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  651. struct inode *inode;
  652. int err;
  653. int drop_inode = 0;
  654. mutex_lock(&root->fs_info->fs_mutex);
  655. trans = btrfs_start_transaction(root, 1);
  656. inode = btrfs_new_inode(trans, dir, mode);
  657. err = PTR_ERR(inode);
  658. if (IS_ERR(inode))
  659. goto out_unlock;
  660. // FIXME mark the inode dirty
  661. err = btrfs_add_nondir(trans, dentry, inode);
  662. if (err)
  663. drop_inode = 1;
  664. else {
  665. inode->i_mapping->a_ops = &btrfs_aops;
  666. inode->i_fop = &btrfs_file_operations;
  667. inode->i_op = &btrfs_file_inode_operations;
  668. }
  669. dir->i_sb->s_dirt = 1;
  670. out_unlock:
  671. btrfs_end_transaction(trans, root);
  672. mutex_unlock(&root->fs_info->fs_mutex);
  673. check_inode(inode);
  674. check_inode(dir);
  675. if (drop_inode) {
  676. inode_dec_link_count(inode);
  677. iput(inode);
  678. }
  679. return err;
  680. }
  681. static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
  682. struct inode *inode, struct inode *dir)
  683. {
  684. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  685. int ret;
  686. char buf[2];
  687. buf[0] = '.';
  688. buf[1] = '.';
  689. ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
  690. inode->i_ino, 1);
  691. if (ret)
  692. goto error;
  693. ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
  694. dir->i_ino, 1);
  695. if (ret)
  696. goto error;
  697. inode->i_size = 6;
  698. ret = btrfs_update_inode(trans, root, inode);
  699. error:
  700. return ret;
  701. }
  702. static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  703. {
  704. struct inode *inode;
  705. struct btrfs_trans_handle *trans;
  706. struct btrfs_root *root = btrfs_sb(dir->i_sb);
  707. int err = 0;
  708. int drop_on_err = 0;
  709. mutex_lock(&root->fs_info->fs_mutex);
  710. trans = btrfs_start_transaction(root, 1);
  711. if (IS_ERR(trans)) {
  712. err = PTR_ERR(trans);
  713. goto out_unlock;
  714. }
  715. inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
  716. if (IS_ERR(inode)) {
  717. err = PTR_ERR(inode);
  718. goto out_fail;
  719. }
  720. drop_on_err = 1;
  721. inode->i_op = &btrfs_dir_inode_operations;
  722. inode->i_fop = &btrfs_dir_file_operations;
  723. err = btrfs_make_empty_dir(trans, inode, dir);
  724. if (err)
  725. goto out_fail;
  726. err = btrfs_add_link(trans, dentry, inode);
  727. if (err)
  728. goto out_fail;
  729. d_instantiate(dentry, inode);
  730. drop_on_err = 0;
  731. out_fail:
  732. btrfs_end_transaction(trans, root);
  733. out_unlock:
  734. mutex_unlock(&root->fs_info->fs_mutex);
  735. if (drop_on_err)
  736. iput(inode);
  737. return err;
  738. }
  739. static int btrfs_sync_fs(struct super_block *sb, int wait)
  740. {
  741. struct btrfs_trans_handle *trans;
  742. struct btrfs_root *root;
  743. int ret;
  744. root = btrfs_sb(sb);
  745. sb->s_dirt = 0;
  746. if (!wait) {
  747. filemap_flush(root->fs_info->btree_inode->i_mapping);
  748. return 0;
  749. }
  750. filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
  751. mutex_lock(&root->fs_info->fs_mutex);
  752. trans = btrfs_start_transaction(root, 1);
  753. ret = btrfs_commit_transaction(trans, root);
  754. sb->s_dirt = 0;
  755. BUG_ON(ret);
  756. printk("btrfs sync_fs\n");
  757. mutex_unlock(&root->fs_info->fs_mutex);
  758. return 0;
  759. }
  760. #if 0
  761. static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
  762. struct buffer_head *result, int create)
  763. {
  764. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  765. struct btrfs_path *path;
  766. struct btrfs_key key;
  767. struct btrfs_leaf *leaf;
  768. int num_bytes = result->b_size;
  769. int item_size;
  770. int ret;
  771. u64 pos;
  772. char *ptr;
  773. int copy_size;
  774. int err = 0;
  775. char *safe_ptr;
  776. char *data_ptr;
  777. path = btrfs_alloc_path();
  778. BUG_ON(!path);
  779. WARN_ON(create);
  780. if (create) {
  781. return 0;
  782. }
  783. pos = iblock << inode->i_blkbits;
  784. key.objectid = inode->i_ino;
  785. key.flags = 0;
  786. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  787. ptr = kmap(result->b_page);
  788. safe_ptr = ptr;
  789. ptr += (pos & (PAGE_CACHE_SIZE -1));
  790. again:
  791. key.offset = pos;
  792. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  793. if (ret) {
  794. if (ret < 0)
  795. err = ret;
  796. else
  797. err = 0;
  798. goto out;
  799. }
  800. leaf = btrfs_buffer_leaf(path->nodes[0]);
  801. item_size = btrfs_item_size(leaf->items + path->slots[0]);
  802. copy_size = min(num_bytes, item_size);
  803. data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
  804. WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
  805. memcpy(ptr, data_ptr, copy_size);
  806. pos += copy_size;
  807. num_bytes -= copy_size;
  808. WARN_ON(num_bytes < 0);
  809. ptr += copy_size;
  810. btrfs_release_path(root, path);
  811. if (num_bytes != 0) {
  812. if (pos >= i_size_read(inode))
  813. memset(ptr, 0, num_bytes);
  814. else
  815. goto again;
  816. }
  817. set_buffer_uptodate(result);
  818. map_bh(result, inode->i_sb, 0);
  819. err = 0;
  820. out:
  821. btrfs_free_path(path);
  822. kunmap(result->b_page);
  823. return err;
  824. }
  825. #endif
  826. static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
  827. struct buffer_head *result, int create)
  828. {
  829. int ret;
  830. int err = 0;
  831. u64 blocknr;
  832. u64 extent_start = 0;
  833. u64 extent_end = 0;
  834. u64 objectid = inode->i_ino;
  835. struct btrfs_path *path;
  836. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  837. struct btrfs_trans_handle *trans = NULL;
  838. struct btrfs_file_extent_item *item;
  839. struct btrfs_leaf *leaf;
  840. struct btrfs_disk_key *found_key;
  841. path = btrfs_alloc_path();
  842. BUG_ON(!path);
  843. btrfs_init_path(path);
  844. if (create)
  845. trans = btrfs_start_transaction(root, 1);
  846. ret = btrfs_lookup_file_extent(trans, root, path,
  847. inode->i_ino,
  848. iblock << inode->i_blkbits, 0);
  849. if (ret < 0) {
  850. err = ret;
  851. goto out;
  852. }
  853. if (ret != 0) {
  854. if (path->slots[0] == 0) {
  855. btrfs_release_path(root, path);
  856. goto allocate;
  857. }
  858. path->slots[0]--;
  859. }
  860. item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  861. struct btrfs_file_extent_item);
  862. leaf = btrfs_buffer_leaf(path->nodes[0]);
  863. blocknr = btrfs_file_extent_disk_blocknr(item);
  864. blocknr += btrfs_file_extent_offset(item);
  865. /* exact match found, use it */
  866. if (ret == 0) {
  867. err = 0;
  868. map_bh(result, inode->i_sb, blocknr);
  869. goto out;
  870. }
  871. /* are we inside the extent that was found? */
  872. found_key = &leaf->items[path->slots[0]].key;
  873. if (btrfs_disk_key_objectid(found_key) != objectid ||
  874. btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
  875. extent_end = 0;
  876. extent_start = 0;
  877. btrfs_release_path(root, path);
  878. goto allocate;
  879. }
  880. extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
  881. extent_start = extent_start >> inode->i_blkbits;
  882. extent_start += btrfs_file_extent_offset(item);
  883. extent_end = extent_start + btrfs_file_extent_num_blocks(item);
  884. if (iblock >= extent_start && iblock < extent_end) {
  885. err = 0;
  886. map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
  887. goto out;
  888. }
  889. allocate:
  890. /* ok, create a new extent */
  891. if (!create) {
  892. err = 0;
  893. goto out;
  894. }
  895. ret = btrfs_alloc_file_extent(trans, root, objectid,
  896. iblock << inode->i_blkbits,
  897. 1, extent_end, &blocknr);
  898. if (ret) {
  899. err = ret;
  900. goto out;
  901. }
  902. inode->i_blocks += inode->i_sb->s_blocksize >> 9;
  903. set_buffer_new(result);
  904. map_bh(result, inode->i_sb, blocknr);
  905. out:
  906. btrfs_release_path(root, path);
  907. btrfs_free_path(path);
  908. if (trans)
  909. btrfs_end_transaction(trans, root);
  910. return err;
  911. }
  912. static int btrfs_get_block(struct inode *inode, sector_t iblock,
  913. struct buffer_head *result, int create)
  914. {
  915. int err;
  916. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  917. mutex_lock(&root->fs_info->fs_mutex);
  918. err = btrfs_get_block_lock(inode, iblock, result, create);
  919. // err = btrfs_get_block_inline(inode, iblock, result, create);
  920. mutex_unlock(&root->fs_info->fs_mutex);
  921. return err;
  922. }
  923. static int btrfs_prepare_write(struct file *file, struct page *page,
  924. unsigned from, unsigned to)
  925. {
  926. return nobh_prepare_write(page, from, to, btrfs_get_block);
  927. }
  928. static int btrfs_commit_write(struct file *file, struct page *page,
  929. unsigned from, unsigned to)
  930. {
  931. return nobh_commit_write(file, page, from, to);
  932. }
  933. static void btrfs_write_super(struct super_block *sb)
  934. {
  935. btrfs_sync_fs(sb, 1);
  936. }
  937. static int btrfs_readpage(struct file *file, struct page *page)
  938. {
  939. return mpage_readpage(page, btrfs_get_block);
  940. }
  941. static int btrfs_readpages(struct file *file, struct address_space *mapping,
  942. struct list_head *pages, unsigned nr_pages)
  943. {
  944. return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
  945. }
  946. static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
  947. {
  948. return nobh_writepage(page, btrfs_get_block, wbc);
  949. }
  950. static void btrfs_truncate(struct inode *inode)
  951. {
  952. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  953. int ret;
  954. struct btrfs_trans_handle *trans;
  955. if (!S_ISREG(inode->i_mode))
  956. return;
  957. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  958. return;
  959. nobh_truncate_page(inode->i_mapping, inode->i_size);
  960. /* FIXME, add redo link to tree so we don't leak on crash */
  961. mutex_lock(&root->fs_info->fs_mutex);
  962. trans = btrfs_start_transaction(root, 1);
  963. ret = btrfs_truncate_in_trans(trans, root, inode);
  964. BUG_ON(ret);
  965. ret = btrfs_end_transaction(trans, root);
  966. BUG_ON(ret);
  967. mutex_unlock(&root->fs_info->fs_mutex);
  968. mark_inode_dirty(inode);
  969. }
  970. static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
  971. struct page **prepared_pages,
  972. const char __user * buf)
  973. {
  974. long page_fault = 0;
  975. int i;
  976. int offset = pos & (PAGE_CACHE_SIZE - 1);
  977. for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
  978. size_t count = min_t(size_t,
  979. PAGE_CACHE_SIZE - offset, write_bytes);
  980. struct page *page = prepared_pages[i];
  981. fault_in_pages_readable(buf, count);
  982. /* Copy data from userspace to the current page */
  983. kmap(page);
  984. page_fault = __copy_from_user(page_address(page) + offset,
  985. buf, count);
  986. /* Flush processor's dcache for this page */
  987. flush_dcache_page(page);
  988. kunmap(page);
  989. buf += count;
  990. write_bytes -= count;
  991. if (page_fault)
  992. break;
  993. }
  994. return page_fault ? -EFAULT : 0;
  995. }
  996. static void btrfs_drop_pages(struct page **pages, size_t num_pages)
  997. {
  998. size_t i;
  999. for (i = 0; i < num_pages; i++) {
  1000. if (!pages[i])
  1001. break;
  1002. unlock_page(pages[i]);
  1003. mark_page_accessed(pages[i]);
  1004. page_cache_release(pages[i]);
  1005. }
  1006. }
  1007. static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
  1008. struct btrfs_root *root,
  1009. struct file *file,
  1010. struct page **pages,
  1011. size_t num_pages,
  1012. loff_t pos,
  1013. size_t write_bytes)
  1014. {
  1015. int i;
  1016. int offset;
  1017. int err = 0;
  1018. int ret;
  1019. int this_write;
  1020. struct inode *inode = file->f_path.dentry->d_inode;
  1021. for (i = 0; i < num_pages; i++) {
  1022. offset = pos & (PAGE_CACHE_SIZE -1);
  1023. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1024. /* FIXME, one block at a time */
  1025. mutex_lock(&root->fs_info->fs_mutex);
  1026. trans = btrfs_start_transaction(root, 1);
  1027. btrfs_csum_file_block(trans, root, inode->i_ino,
  1028. pages[i]->index << PAGE_CACHE_SHIFT,
  1029. kmap(pages[i]), PAGE_CACHE_SIZE);
  1030. kunmap(pages[i]);
  1031. SetPageChecked(pages[i]);
  1032. ret = btrfs_end_transaction(trans, root);
  1033. BUG_ON(ret);
  1034. mutex_unlock(&root->fs_info->fs_mutex);
  1035. ret = nobh_commit_write(file, pages[i], offset,
  1036. offset + this_write);
  1037. pos += this_write;
  1038. if (ret) {
  1039. err = ret;
  1040. goto failed;
  1041. }
  1042. WARN_ON(this_write > write_bytes);
  1043. write_bytes -= this_write;
  1044. }
  1045. failed:
  1046. return err;
  1047. }
  1048. static int prepare_pages(struct btrfs_trans_handle *trans,
  1049. struct btrfs_root *root,
  1050. struct file *file,
  1051. struct page **pages,
  1052. size_t num_pages,
  1053. loff_t pos,
  1054. size_t write_bytes)
  1055. {
  1056. int i;
  1057. unsigned long index = pos >> PAGE_CACHE_SHIFT;
  1058. struct inode *inode = file->f_path.dentry->d_inode;
  1059. int offset;
  1060. int err = 0;
  1061. int ret;
  1062. int this_write;
  1063. loff_t isize = i_size_read(inode);
  1064. memset(pages, 0, num_pages * sizeof(struct page *));
  1065. for (i = 0; i < num_pages; i++) {
  1066. pages[i] = grab_cache_page(inode->i_mapping, index + i);
  1067. if (!pages[i]) {
  1068. err = -ENOMEM;
  1069. goto failed_release;
  1070. }
  1071. offset = pos & (PAGE_CACHE_SIZE -1);
  1072. this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
  1073. ret = nobh_prepare_write(pages[i], offset,
  1074. offset + this_write,
  1075. btrfs_get_block);
  1076. pos += this_write;
  1077. if (ret) {
  1078. err = ret;
  1079. goto failed_truncate;
  1080. }
  1081. WARN_ON(this_write > write_bytes);
  1082. write_bytes -= this_write;
  1083. }
  1084. return 0;
  1085. failed_release:
  1086. btrfs_drop_pages(pages, num_pages);
  1087. return err;
  1088. failed_truncate:
  1089. btrfs_drop_pages(pages, num_pages);
  1090. if (pos > isize)
  1091. vmtruncate(inode, isize);
  1092. return err;
  1093. }
  1094. static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
  1095. size_t count, loff_t *ppos)
  1096. {
  1097. loff_t pos;
  1098. size_t num_written = 0;
  1099. int err = 0;
  1100. int ret = 0;
  1101. struct inode *inode = file->f_path.dentry->d_inode;
  1102. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  1103. struct page *pages[1];
  1104. if (file->f_flags & O_DIRECT)
  1105. return -EINVAL;
  1106. pos = *ppos;
  1107. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1108. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1109. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1110. if (err)
  1111. goto out;
  1112. if (count == 0)
  1113. goto out;
  1114. err = remove_suid(file->f_path.dentry);
  1115. if (err)
  1116. goto out;
  1117. file_update_time(file);
  1118. mutex_lock(&inode->i_mutex);
  1119. while(count > 0) {
  1120. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1121. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1122. size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
  1123. PAGE_CACHE_SHIFT;
  1124. ret = prepare_pages(NULL, root, file, pages, num_pages,
  1125. pos, write_bytes);
  1126. BUG_ON(ret);
  1127. ret = btrfs_copy_from_user(pos, num_pages,
  1128. write_bytes, pages, buf);
  1129. BUG_ON(ret);
  1130. ret = dirty_and_release_pages(NULL, root, file, pages,
  1131. num_pages, pos, write_bytes);
  1132. BUG_ON(ret);
  1133. btrfs_drop_pages(pages, num_pages);
  1134. buf += write_bytes;
  1135. count -= write_bytes;
  1136. pos += write_bytes;
  1137. num_written += write_bytes;
  1138. balance_dirty_pages_ratelimited(inode->i_mapping);
  1139. cond_resched();
  1140. }
  1141. mutex_unlock(&inode->i_mutex);
  1142. out:
  1143. *ppos = pos;
  1144. current->backing_dev_info = NULL;
  1145. return num_written ? num_written : err;
  1146. }
  1147. #if 0
  1148. static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
  1149. struct page *page, loff_t pos,
  1150. size_t offset, size_t write_bytes)
  1151. {
  1152. struct btrfs_path *path;
  1153. struct btrfs_trans_handle *trans;
  1154. struct btrfs_key key;
  1155. struct btrfs_leaf *leaf;
  1156. struct btrfs_key found_key;
  1157. int ret;
  1158. size_t copy_size = 0;
  1159. char *dst = NULL;
  1160. int err = 0;
  1161. size_t num_written = 0;
  1162. path = btrfs_alloc_path();
  1163. BUG_ON(!path);
  1164. mutex_lock(&root->fs_info->fs_mutex);
  1165. trans = btrfs_start_transaction(root, 1);
  1166. key.objectid = inode->i_ino;
  1167. key.flags = 0;
  1168. btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
  1169. again:
  1170. key.offset = pos;
  1171. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1172. if (ret < 0) {
  1173. err = ret;
  1174. goto out;
  1175. }
  1176. if (ret == 0) {
  1177. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1178. btrfs_disk_key_to_cpu(&found_key,
  1179. &leaf->items[path->slots[0]].key);
  1180. copy_size = btrfs_item_size(leaf->items + path->slots[0]);
  1181. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1182. copy_size = min(write_bytes, copy_size);
  1183. goto copyit;
  1184. } else {
  1185. int slot = path->slots[0];
  1186. if (slot > 0) {
  1187. slot--;
  1188. }
  1189. // FIXME find max key
  1190. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1191. btrfs_disk_key_to_cpu(&found_key,
  1192. &leaf->items[slot].key);
  1193. if (found_key.objectid != inode->i_ino)
  1194. goto insert;
  1195. if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
  1196. goto insert;
  1197. copy_size = btrfs_item_size(leaf->items + slot);
  1198. if (found_key.offset + copy_size <= pos)
  1199. goto insert;
  1200. dst = btrfs_item_ptr(leaf, path->slots[0], char);
  1201. dst += pos - found_key.offset;
  1202. copy_size = copy_size - (pos - found_key.offset);
  1203. BUG_ON(copy_size < 0);
  1204. copy_size = min(write_bytes, copy_size);
  1205. WARN_ON(copy_size == 0);
  1206. goto copyit;
  1207. }
  1208. insert:
  1209. btrfs_release_path(root, path);
  1210. copy_size = min(write_bytes,
  1211. (size_t)BTRFS_LEAF_DATA_SIZE(root) -
  1212. sizeof(struct btrfs_item) * 4);
  1213. ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
  1214. BUG_ON(ret);
  1215. dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1216. path->slots[0], char);
  1217. copyit:
  1218. WARN_ON(copy_size == 0);
  1219. WARN_ON(dst + copy_size >
  1220. btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
  1221. path->slots[0], char) +
  1222. btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
  1223. path->slots[0]));
  1224. btrfs_memcpy(root, path->nodes[0]->b_data, dst,
  1225. page_address(page) + offset, copy_size);
  1226. mark_buffer_dirty(path->nodes[0]);
  1227. btrfs_release_path(root, path);
  1228. pos += copy_size;
  1229. offset += copy_size;
  1230. num_written += copy_size;
  1231. write_bytes -= copy_size;
  1232. if (write_bytes)
  1233. goto again;
  1234. out:
  1235. btrfs_free_path(path);
  1236. ret = btrfs_end_transaction(trans, root);
  1237. BUG_ON(ret);
  1238. mutex_unlock(&root->fs_info->fs_mutex);
  1239. return num_written ? num_written : err;
  1240. }
  1241. static ssize_t btrfs_file_inline_write(struct file *file,
  1242. const char __user *buf,
  1243. size_t count, loff_t *ppos)
  1244. {
  1245. loff_t pos;
  1246. size_t num_written = 0;
  1247. int err = 0;
  1248. int ret = 0;
  1249. struct inode *inode = file->f_path.dentry->d_inode;
  1250. struct btrfs_root *root = btrfs_sb(inode->i_sb);
  1251. unsigned long page_index;
  1252. if (file->f_flags & O_DIRECT)
  1253. return -EINVAL;
  1254. pos = *ppos;
  1255. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1256. current->backing_dev_info = inode->i_mapping->backing_dev_info;
  1257. err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
  1258. if (err)
  1259. goto out;
  1260. if (count == 0)
  1261. goto out;
  1262. err = remove_suid(file->f_path.dentry);
  1263. if (err)
  1264. goto out;
  1265. file_update_time(file);
  1266. mutex_lock(&inode->i_mutex);
  1267. while(count > 0) {
  1268. size_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1269. size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
  1270. struct page *page;
  1271. page_index = pos >> PAGE_CACHE_SHIFT;
  1272. page = grab_cache_page(inode->i_mapping, page_index);
  1273. if (!PageUptodate(page)) {
  1274. ret = mpage_readpage(page, btrfs_get_block);
  1275. BUG_ON(ret);
  1276. lock_page(page);
  1277. }
  1278. ret = btrfs_copy_from_user(pos, 1,
  1279. write_bytes, &page, buf);
  1280. BUG_ON(ret);
  1281. write_bytes = inline_one_page(root, inode, page, pos,
  1282. offset, write_bytes);
  1283. SetPageUptodate(page);
  1284. if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
  1285. i_size_write(inode, pos + write_bytes);
  1286. mark_inode_dirty(inode);
  1287. }
  1288. page_cache_release(page);
  1289. unlock_page(page);
  1290. if (write_bytes < 0)
  1291. goto out_unlock;
  1292. buf += write_bytes;
  1293. count -= write_bytes;
  1294. pos += write_bytes;
  1295. num_written += write_bytes;
  1296. balance_dirty_pages_ratelimited(inode->i_mapping);
  1297. cond_resched();
  1298. }
  1299. out_unlock:
  1300. mutex_unlock(&inode->i_mutex);
  1301. out:
  1302. *ppos = pos;
  1303. current->backing_dev_info = NULL;
  1304. return num_written ? num_written : err;
  1305. }
  1306. #endif
  1307. static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
  1308. unsigned long offset, unsigned long size)
  1309. {
  1310. char *kaddr;
  1311. unsigned long left, count = desc->count;
  1312. if (size > count)
  1313. size = count;
  1314. if (!PageChecked(page)) {
  1315. /* FIXME, do it per block */
  1316. struct btrfs_root *root = btrfs_sb(page->mapping->host->i_sb);
  1317. int ret = btrfs_csum_verify_file_block(root,
  1318. page->mapping->host->i_ino,
  1319. page->index << PAGE_CACHE_SHIFT,
  1320. kmap(page), PAGE_CACHE_SIZE);
  1321. if (ret) {
  1322. printk("failed to verify ino %lu page %lu\n",
  1323. page->mapping->host->i_ino,
  1324. page->index);
  1325. memset(page_address(page), 0, PAGE_CACHE_SIZE);
  1326. }
  1327. SetPageChecked(page);
  1328. kunmap(page);
  1329. }
  1330. /*
  1331. * Faults on the destination of a read are common, so do it before
  1332. * taking the kmap.
  1333. */
  1334. if (!fault_in_pages_writeable(desc->arg.buf, size)) {
  1335. kaddr = kmap_atomic(page, KM_USER0);
  1336. left = __copy_to_user_inatomic(desc->arg.buf,
  1337. kaddr + offset, size);
  1338. kunmap_atomic(kaddr, KM_USER0);
  1339. if (left == 0)
  1340. goto success;
  1341. }
  1342. /* Do it the slow way */
  1343. kaddr = kmap(page);
  1344. left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
  1345. kunmap(page);
  1346. if (left) {
  1347. size -= left;
  1348. desc->error = -EFAULT;
  1349. }
  1350. success:
  1351. desc->count = count - size;
  1352. desc->written += size;
  1353. desc->arg.buf += size;
  1354. return size;
  1355. }
  1356. /**
  1357. * btrfs_file_aio_read - filesystem read routine
  1358. * @iocb: kernel I/O control block
  1359. * @iov: io vector request
  1360. * @nr_segs: number of segments in the iovec
  1361. * @pos: current file position
  1362. */
  1363. static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  1364. unsigned long nr_segs, loff_t pos)
  1365. {
  1366. struct file *filp = iocb->ki_filp;
  1367. ssize_t retval;
  1368. unsigned long seg;
  1369. size_t count;
  1370. loff_t *ppos = &iocb->ki_pos;
  1371. count = 0;
  1372. for (seg = 0; seg < nr_segs; seg++) {
  1373. const struct iovec *iv = &iov[seg];
  1374. /*
  1375. * If any segment has a negative length, or the cumulative
  1376. * length ever wraps negative then return -EINVAL.
  1377. */
  1378. count += iv->iov_len;
  1379. if (unlikely((ssize_t)(count|iv->iov_len) < 0))
  1380. return -EINVAL;
  1381. if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
  1382. continue;
  1383. if (seg == 0)
  1384. return -EFAULT;
  1385. nr_segs = seg;
  1386. count -= iv->iov_len; /* This segment is no good */
  1387. break;
  1388. }
  1389. retval = 0;
  1390. if (count) {
  1391. for (seg = 0; seg < nr_segs; seg++) {
  1392. read_descriptor_t desc;
  1393. desc.written = 0;
  1394. desc.arg.buf = iov[seg].iov_base;
  1395. desc.count = iov[seg].iov_len;
  1396. if (desc.count == 0)
  1397. continue;
  1398. desc.error = 0;
  1399. do_generic_file_read(filp, ppos, &desc,
  1400. btrfs_read_actor);
  1401. retval += desc.written;
  1402. if (desc.error) {
  1403. retval = retval ?: desc.error;
  1404. break;
  1405. }
  1406. }
  1407. }
  1408. return retval;
  1409. }
  1410. static struct kmem_cache *btrfs_inode_cachep;
  1411. struct kmem_cache *btrfs_trans_handle_cachep;
  1412. struct kmem_cache *btrfs_transaction_cachep;
  1413. struct kmem_cache *btrfs_bit_radix_cachep;
  1414. struct kmem_cache *btrfs_path_cachep;
  1415. /*
  1416. * Called inside transaction, so use GFP_NOFS
  1417. */
  1418. static struct inode *btrfs_alloc_inode(struct super_block *sb)
  1419. {
  1420. struct btrfs_inode *ei;
  1421. ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
  1422. if (!ei)
  1423. return NULL;
  1424. ei->magic = 0xDEADBEEF;
  1425. ei->magic2 = 0xDEADBEAF;
  1426. return &ei->vfs_inode;
  1427. }
  1428. static void btrfs_destroy_inode(struct inode *inode)
  1429. {
  1430. struct btrfs_inode *ei = BTRFS_I(inode);
  1431. WARN_ON(ei->magic != 0xDEADBEEF);
  1432. WARN_ON(ei->magic2 != 0xDEADBEAF);
  1433. WARN_ON(!list_empty(&inode->i_dentry));
  1434. WARN_ON(inode->i_data.nrpages);
  1435. ei->magic = 0;
  1436. ei->magic2 = 0;
  1437. kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
  1438. }
  1439. static void init_once(void * foo, struct kmem_cache * cachep,
  1440. unsigned long flags)
  1441. {
  1442. struct btrfs_inode *ei = (struct btrfs_inode *) foo;
  1443. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  1444. SLAB_CTOR_CONSTRUCTOR) {
  1445. inode_init_once(&ei->vfs_inode);
  1446. }
  1447. }
  1448. static int init_inodecache(void)
  1449. {
  1450. btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
  1451. sizeof(struct btrfs_inode),
  1452. 0, (SLAB_RECLAIM_ACCOUNT|
  1453. SLAB_MEM_SPREAD),
  1454. init_once, NULL);
  1455. btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
  1456. sizeof(struct btrfs_trans_handle),
  1457. 0, (SLAB_RECLAIM_ACCOUNT|
  1458. SLAB_MEM_SPREAD),
  1459. NULL, NULL);
  1460. btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
  1461. sizeof(struct btrfs_transaction),
  1462. 0, (SLAB_RECLAIM_ACCOUNT|
  1463. SLAB_MEM_SPREAD),
  1464. NULL, NULL);
  1465. btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
  1466. sizeof(struct btrfs_transaction),
  1467. 0, (SLAB_RECLAIM_ACCOUNT|
  1468. SLAB_MEM_SPREAD),
  1469. NULL, NULL);
  1470. btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
  1471. 256,
  1472. 0, (SLAB_RECLAIM_ACCOUNT|
  1473. SLAB_MEM_SPREAD |
  1474. SLAB_DESTROY_BY_RCU),
  1475. NULL, NULL);
  1476. if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
  1477. btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
  1478. return -ENOMEM;
  1479. return 0;
  1480. }
  1481. static void destroy_inodecache(void)
  1482. {
  1483. kmem_cache_destroy(btrfs_inode_cachep);
  1484. kmem_cache_destroy(btrfs_trans_handle_cachep);
  1485. kmem_cache_destroy(btrfs_transaction_cachep);
  1486. kmem_cache_destroy(btrfs_bit_radix_cachep);
  1487. kmem_cache_destroy(btrfs_path_cachep);
  1488. }
  1489. static int btrfs_get_sb(struct file_system_type *fs_type,
  1490. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  1491. {
  1492. return get_sb_bdev(fs_type, flags, dev_name, data,
  1493. btrfs_fill_super, mnt);
  1494. }
  1495. static struct file_system_type btrfs_fs_type = {
  1496. .owner = THIS_MODULE,
  1497. .name = "btrfs",
  1498. .get_sb = btrfs_get_sb,
  1499. .kill_sb = kill_block_super,
  1500. .fs_flags = FS_REQUIRES_DEV,
  1501. };
  1502. static struct super_operations btrfs_super_ops = {
  1503. .statfs = simple_statfs,
  1504. .delete_inode = btrfs_delete_inode,
  1505. .put_super = btrfs_put_super,
  1506. .read_inode = btrfs_read_locked_inode,
  1507. .write_super = btrfs_write_super,
  1508. .sync_fs = btrfs_sync_fs,
  1509. .write_inode = btrfs_write_inode,
  1510. .alloc_inode = btrfs_alloc_inode,
  1511. .destroy_inode = btrfs_destroy_inode,
  1512. };
  1513. static struct inode_operations btrfs_dir_inode_operations = {
  1514. .lookup = btrfs_lookup,
  1515. .create = btrfs_create,
  1516. .unlink = btrfs_unlink,
  1517. .mkdir = btrfs_mkdir,
  1518. .rmdir = btrfs_rmdir,
  1519. };
  1520. static struct file_operations btrfs_dir_file_operations = {
  1521. .llseek = generic_file_llseek,
  1522. .read = generic_read_dir,
  1523. .readdir = btrfs_readdir,
  1524. };
  1525. static struct address_space_operations btrfs_aops = {
  1526. .readpage = btrfs_readpage,
  1527. .readpages = btrfs_readpages,
  1528. .writepage = btrfs_writepage,
  1529. .sync_page = block_sync_page,
  1530. .prepare_write = btrfs_prepare_write,
  1531. .commit_write = btrfs_commit_write,
  1532. };
  1533. static struct inode_operations btrfs_file_inode_operations = {
  1534. .truncate = btrfs_truncate,
  1535. };
  1536. static struct file_operations btrfs_file_operations = {
  1537. .llseek = generic_file_llseek,
  1538. .read = do_sync_read,
  1539. .aio_read = btrfs_file_aio_read,
  1540. .write = btrfs_file_write,
  1541. .mmap = generic_file_mmap,
  1542. .open = generic_file_open,
  1543. };
  1544. static int __init init_btrfs_fs(void)
  1545. {
  1546. int err;
  1547. printk("btrfs loaded!\n");
  1548. err = init_inodecache();
  1549. if (err)
  1550. return err;
  1551. return register_filesystem(&btrfs_fs_type);
  1552. }
  1553. static void __exit exit_btrfs_fs(void)
  1554. {
  1555. destroy_inodecache();
  1556. unregister_filesystem(&btrfs_fs_type);
  1557. printk("btrfs unloaded\n");
  1558. }
  1559. module_init(init_btrfs_fs)
  1560. module_exit(exit_btrfs_fs)
  1561. MODULE_LICENSE("GPL");