namei.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * namei.c - NILFS pathname lookup operations.
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Modified for NILFS by Amagai Yoshiji and Ryusuke Konishi.
  17. */
  18. /*
  19. * linux/fs/ext2/namei.c
  20. *
  21. * Copyright (C) 1992, 1993, 1994, 1995
  22. * Remy Card (card@masi.ibp.fr)
  23. * Laboratoire MASI - Institut Blaise Pascal
  24. * Universite Pierre et Marie Curie (Paris VI)
  25. *
  26. * from
  27. *
  28. * linux/fs/minix/namei.c
  29. *
  30. * Copyright (C) 1991, 1992 Linus Torvalds
  31. *
  32. * Big-endian to little-endian byte-swapping/bitmaps by
  33. * David S. Miller (davem@caip.rutgers.edu), 1995
  34. */
  35. #include <linux/pagemap.h>
  36. #include "nilfs.h"
  37. #include "export.h"
  38. #define NILFS_FID_SIZE_NON_CONNECTABLE \
  39. (offsetof(struct nilfs_fid, parent_gen) / 4)
  40. #define NILFS_FID_SIZE_CONNECTABLE (sizeof(struct nilfs_fid) / 4)
  41. static inline int nilfs_add_nondir(struct dentry *dentry, struct inode *inode)
  42. {
  43. int err = nilfs_add_link(dentry, inode);
  44. if (!err) {
  45. d_instantiate_new(dentry, inode);
  46. return 0;
  47. }
  48. inode_dec_link_count(inode);
  49. unlock_new_inode(inode);
  50. iput(inode);
  51. return err;
  52. }
  53. /*
  54. * Methods themselves.
  55. */
  56. static struct dentry *
  57. nilfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  58. {
  59. struct inode *inode;
  60. ino_t ino;
  61. if (dentry->d_name.len > NILFS_NAME_LEN)
  62. return ERR_PTR(-ENAMETOOLONG);
  63. ino = nilfs_inode_by_name(dir, &dentry->d_name);
  64. inode = ino ? nilfs_iget(dir->i_sb, NILFS_I(dir)->i_root, ino) : NULL;
  65. return d_splice_alias(inode, dentry);
  66. }
  67. /*
  68. * By the time this is called, we already have created
  69. * the directory cache entry for the new file, but it
  70. * is so far negative - it has no inode.
  71. *
  72. * If the create succeeds, we fill in the inode information
  73. * with d_instantiate().
  74. */
  75. static int nilfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  76. bool excl)
  77. {
  78. struct inode *inode;
  79. struct nilfs_transaction_info ti;
  80. int err;
  81. err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
  82. if (err)
  83. return err;
  84. inode = nilfs_new_inode(dir, mode);
  85. err = PTR_ERR(inode);
  86. if (!IS_ERR(inode)) {
  87. inode->i_op = &nilfs_file_inode_operations;
  88. inode->i_fop = &nilfs_file_operations;
  89. inode->i_mapping->a_ops = &nilfs_aops;
  90. nilfs_mark_inode_dirty(inode);
  91. err = nilfs_add_nondir(dentry, inode);
  92. }
  93. if (!err)
  94. err = nilfs_transaction_commit(dir->i_sb);
  95. else
  96. nilfs_transaction_abort(dir->i_sb);
  97. return err;
  98. }
  99. static int
  100. nilfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev)
  101. {
  102. struct inode *inode;
  103. struct nilfs_transaction_info ti;
  104. int err;
  105. err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
  106. if (err)
  107. return err;
  108. inode = nilfs_new_inode(dir, mode);
  109. err = PTR_ERR(inode);
  110. if (!IS_ERR(inode)) {
  111. init_special_inode(inode, inode->i_mode, rdev);
  112. nilfs_mark_inode_dirty(inode);
  113. err = nilfs_add_nondir(dentry, inode);
  114. }
  115. if (!err)
  116. err = nilfs_transaction_commit(dir->i_sb);
  117. else
  118. nilfs_transaction_abort(dir->i_sb);
  119. return err;
  120. }
  121. static int nilfs_symlink(struct inode *dir, struct dentry *dentry,
  122. const char *symname)
  123. {
  124. struct nilfs_transaction_info ti;
  125. struct super_block *sb = dir->i_sb;
  126. unsigned int l = strlen(symname) + 1;
  127. struct inode *inode;
  128. int err;
  129. if (l > sb->s_blocksize)
  130. return -ENAMETOOLONG;
  131. err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
  132. if (err)
  133. return err;
  134. inode = nilfs_new_inode(dir, S_IFLNK | S_IRWXUGO);
  135. err = PTR_ERR(inode);
  136. if (IS_ERR(inode))
  137. goto out;
  138. /* slow symlink */
  139. inode->i_op = &nilfs_symlink_inode_operations;
  140. inode_nohighmem(inode);
  141. inode->i_mapping->a_ops = &nilfs_aops;
  142. err = page_symlink(inode, symname, l);
  143. if (err)
  144. goto out_fail;
  145. /* mark_inode_dirty(inode); */
  146. /* page_symlink() do this */
  147. err = nilfs_add_nondir(dentry, inode);
  148. out:
  149. if (!err)
  150. err = nilfs_transaction_commit(dir->i_sb);
  151. else
  152. nilfs_transaction_abort(dir->i_sb);
  153. return err;
  154. out_fail:
  155. drop_nlink(inode);
  156. nilfs_mark_inode_dirty(inode);
  157. unlock_new_inode(inode);
  158. iput(inode);
  159. goto out;
  160. }
  161. static int nilfs_link(struct dentry *old_dentry, struct inode *dir,
  162. struct dentry *dentry)
  163. {
  164. struct inode *inode = d_inode(old_dentry);
  165. struct nilfs_transaction_info ti;
  166. int err;
  167. err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
  168. if (err)
  169. return err;
  170. inode->i_ctime = current_time(inode);
  171. inode_inc_link_count(inode);
  172. ihold(inode);
  173. err = nilfs_add_link(dentry, inode);
  174. if (!err) {
  175. d_instantiate(dentry, inode);
  176. err = nilfs_transaction_commit(dir->i_sb);
  177. } else {
  178. inode_dec_link_count(inode);
  179. iput(inode);
  180. nilfs_transaction_abort(dir->i_sb);
  181. }
  182. return err;
  183. }
  184. static int nilfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  185. {
  186. struct inode *inode;
  187. struct nilfs_transaction_info ti;
  188. int err;
  189. err = nilfs_transaction_begin(dir->i_sb, &ti, 1);
  190. if (err)
  191. return err;
  192. inc_nlink(dir);
  193. inode = nilfs_new_inode(dir, S_IFDIR | mode);
  194. err = PTR_ERR(inode);
  195. if (IS_ERR(inode))
  196. goto out_dir;
  197. inode->i_op = &nilfs_dir_inode_operations;
  198. inode->i_fop = &nilfs_dir_operations;
  199. inode->i_mapping->a_ops = &nilfs_aops;
  200. inc_nlink(inode);
  201. err = nilfs_make_empty(inode, dir);
  202. if (err)
  203. goto out_fail;
  204. err = nilfs_add_link(dentry, inode);
  205. if (err)
  206. goto out_fail;
  207. nilfs_mark_inode_dirty(inode);
  208. d_instantiate_new(dentry, inode);
  209. out:
  210. if (!err)
  211. err = nilfs_transaction_commit(dir->i_sb);
  212. else
  213. nilfs_transaction_abort(dir->i_sb);
  214. return err;
  215. out_fail:
  216. drop_nlink(inode);
  217. drop_nlink(inode);
  218. nilfs_mark_inode_dirty(inode);
  219. unlock_new_inode(inode);
  220. iput(inode);
  221. out_dir:
  222. drop_nlink(dir);
  223. nilfs_mark_inode_dirty(dir);
  224. goto out;
  225. }
  226. static int nilfs_do_unlink(struct inode *dir, struct dentry *dentry)
  227. {
  228. struct inode *inode;
  229. struct nilfs_dir_entry *de;
  230. struct page *page;
  231. int err;
  232. err = -ENOENT;
  233. de = nilfs_find_entry(dir, &dentry->d_name, &page);
  234. if (!de)
  235. goto out;
  236. inode = d_inode(dentry);
  237. err = -EIO;
  238. if (le64_to_cpu(de->inode) != inode->i_ino)
  239. goto out;
  240. if (!inode->i_nlink) {
  241. nilfs_msg(inode->i_sb, KERN_WARNING,
  242. "deleting nonexistent file (ino=%lu), %d",
  243. inode->i_ino, inode->i_nlink);
  244. set_nlink(inode, 1);
  245. }
  246. err = nilfs_delete_entry(de, page);
  247. if (err)
  248. goto out;
  249. inode->i_ctime = dir->i_ctime;
  250. drop_nlink(inode);
  251. err = 0;
  252. out:
  253. return err;
  254. }
  255. static int nilfs_unlink(struct inode *dir, struct dentry *dentry)
  256. {
  257. struct nilfs_transaction_info ti;
  258. int err;
  259. err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
  260. if (err)
  261. return err;
  262. err = nilfs_do_unlink(dir, dentry);
  263. if (!err) {
  264. nilfs_mark_inode_dirty(dir);
  265. nilfs_mark_inode_dirty(d_inode(dentry));
  266. err = nilfs_transaction_commit(dir->i_sb);
  267. } else
  268. nilfs_transaction_abort(dir->i_sb);
  269. return err;
  270. }
  271. static int nilfs_rmdir(struct inode *dir, struct dentry *dentry)
  272. {
  273. struct inode *inode = d_inode(dentry);
  274. struct nilfs_transaction_info ti;
  275. int err;
  276. err = nilfs_transaction_begin(dir->i_sb, &ti, 0);
  277. if (err)
  278. return err;
  279. err = -ENOTEMPTY;
  280. if (nilfs_empty_dir(inode)) {
  281. err = nilfs_do_unlink(dir, dentry);
  282. if (!err) {
  283. inode->i_size = 0;
  284. drop_nlink(inode);
  285. nilfs_mark_inode_dirty(inode);
  286. drop_nlink(dir);
  287. nilfs_mark_inode_dirty(dir);
  288. }
  289. }
  290. if (!err)
  291. err = nilfs_transaction_commit(dir->i_sb);
  292. else
  293. nilfs_transaction_abort(dir->i_sb);
  294. return err;
  295. }
  296. static int nilfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  297. struct inode *new_dir, struct dentry *new_dentry,
  298. unsigned int flags)
  299. {
  300. struct inode *old_inode = d_inode(old_dentry);
  301. struct inode *new_inode = d_inode(new_dentry);
  302. struct page *dir_page = NULL;
  303. struct nilfs_dir_entry *dir_de = NULL;
  304. struct page *old_page;
  305. struct nilfs_dir_entry *old_de;
  306. struct nilfs_transaction_info ti;
  307. int err;
  308. if (flags & ~RENAME_NOREPLACE)
  309. return -EINVAL;
  310. err = nilfs_transaction_begin(old_dir->i_sb, &ti, 1);
  311. if (unlikely(err))
  312. return err;
  313. err = -ENOENT;
  314. old_de = nilfs_find_entry(old_dir, &old_dentry->d_name, &old_page);
  315. if (!old_de)
  316. goto out;
  317. if (S_ISDIR(old_inode->i_mode)) {
  318. err = -EIO;
  319. dir_de = nilfs_dotdot(old_inode, &dir_page);
  320. if (!dir_de)
  321. goto out_old;
  322. }
  323. if (new_inode) {
  324. struct page *new_page;
  325. struct nilfs_dir_entry *new_de;
  326. err = -ENOTEMPTY;
  327. if (dir_de && !nilfs_empty_dir(new_inode))
  328. goto out_dir;
  329. err = -ENOENT;
  330. new_de = nilfs_find_entry(new_dir, &new_dentry->d_name, &new_page);
  331. if (!new_de)
  332. goto out_dir;
  333. nilfs_set_link(new_dir, new_de, new_page, old_inode);
  334. nilfs_mark_inode_dirty(new_dir);
  335. new_inode->i_ctime = current_time(new_inode);
  336. if (dir_de)
  337. drop_nlink(new_inode);
  338. drop_nlink(new_inode);
  339. nilfs_mark_inode_dirty(new_inode);
  340. } else {
  341. err = nilfs_add_link(new_dentry, old_inode);
  342. if (err)
  343. goto out_dir;
  344. if (dir_de) {
  345. inc_nlink(new_dir);
  346. nilfs_mark_inode_dirty(new_dir);
  347. }
  348. }
  349. /*
  350. * Like most other Unix systems, set the ctime for inodes on a
  351. * rename.
  352. */
  353. old_inode->i_ctime = current_time(old_inode);
  354. nilfs_delete_entry(old_de, old_page);
  355. if (dir_de) {
  356. nilfs_set_link(old_inode, dir_de, dir_page, new_dir);
  357. drop_nlink(old_dir);
  358. }
  359. nilfs_mark_inode_dirty(old_dir);
  360. nilfs_mark_inode_dirty(old_inode);
  361. err = nilfs_transaction_commit(old_dir->i_sb);
  362. return err;
  363. out_dir:
  364. if (dir_de) {
  365. kunmap(dir_page);
  366. put_page(dir_page);
  367. }
  368. out_old:
  369. kunmap(old_page);
  370. put_page(old_page);
  371. out:
  372. nilfs_transaction_abort(old_dir->i_sb);
  373. return err;
  374. }
  375. /*
  376. * Export operations
  377. */
  378. static struct dentry *nilfs_get_parent(struct dentry *child)
  379. {
  380. unsigned long ino;
  381. struct inode *inode;
  382. struct qstr dotdot = QSTR_INIT("..", 2);
  383. struct nilfs_root *root;
  384. ino = nilfs_inode_by_name(d_inode(child), &dotdot);
  385. if (!ino)
  386. return ERR_PTR(-ENOENT);
  387. root = NILFS_I(d_inode(child))->i_root;
  388. inode = nilfs_iget(child->d_sb, root, ino);
  389. if (IS_ERR(inode))
  390. return ERR_CAST(inode);
  391. return d_obtain_alias(inode);
  392. }
  393. static struct dentry *nilfs_get_dentry(struct super_block *sb, u64 cno,
  394. u64 ino, u32 gen)
  395. {
  396. struct nilfs_root *root;
  397. struct inode *inode;
  398. if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO)
  399. return ERR_PTR(-ESTALE);
  400. root = nilfs_lookup_root(sb->s_fs_info, cno);
  401. if (!root)
  402. return ERR_PTR(-ESTALE);
  403. inode = nilfs_iget(sb, root, ino);
  404. nilfs_put_root(root);
  405. if (IS_ERR(inode))
  406. return ERR_CAST(inode);
  407. if (gen && inode->i_generation != gen) {
  408. iput(inode);
  409. return ERR_PTR(-ESTALE);
  410. }
  411. return d_obtain_alias(inode);
  412. }
  413. static struct dentry *nilfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
  414. int fh_len, int fh_type)
  415. {
  416. struct nilfs_fid *fid = (struct nilfs_fid *)fh;
  417. if (fh_len < NILFS_FID_SIZE_NON_CONNECTABLE ||
  418. (fh_type != FILEID_NILFS_WITH_PARENT &&
  419. fh_type != FILEID_NILFS_WITHOUT_PARENT))
  420. return NULL;
  421. return nilfs_get_dentry(sb, fid->cno, fid->ino, fid->gen);
  422. }
  423. static struct dentry *nilfs_fh_to_parent(struct super_block *sb, struct fid *fh,
  424. int fh_len, int fh_type)
  425. {
  426. struct nilfs_fid *fid = (struct nilfs_fid *)fh;
  427. if (fh_len < NILFS_FID_SIZE_CONNECTABLE ||
  428. fh_type != FILEID_NILFS_WITH_PARENT)
  429. return NULL;
  430. return nilfs_get_dentry(sb, fid->cno, fid->parent_ino, fid->parent_gen);
  431. }
  432. static int nilfs_encode_fh(struct inode *inode, __u32 *fh, int *lenp,
  433. struct inode *parent)
  434. {
  435. struct nilfs_fid *fid = (struct nilfs_fid *)fh;
  436. struct nilfs_root *root = NILFS_I(inode)->i_root;
  437. int type;
  438. if (parent && *lenp < NILFS_FID_SIZE_CONNECTABLE) {
  439. *lenp = NILFS_FID_SIZE_CONNECTABLE;
  440. return FILEID_INVALID;
  441. }
  442. if (*lenp < NILFS_FID_SIZE_NON_CONNECTABLE) {
  443. *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
  444. return FILEID_INVALID;
  445. }
  446. fid->cno = root->cno;
  447. fid->ino = inode->i_ino;
  448. fid->gen = inode->i_generation;
  449. if (parent) {
  450. fid->parent_ino = parent->i_ino;
  451. fid->parent_gen = parent->i_generation;
  452. type = FILEID_NILFS_WITH_PARENT;
  453. *lenp = NILFS_FID_SIZE_CONNECTABLE;
  454. } else {
  455. type = FILEID_NILFS_WITHOUT_PARENT;
  456. *lenp = NILFS_FID_SIZE_NON_CONNECTABLE;
  457. }
  458. return type;
  459. }
  460. const struct inode_operations nilfs_dir_inode_operations = {
  461. .create = nilfs_create,
  462. .lookup = nilfs_lookup,
  463. .link = nilfs_link,
  464. .unlink = nilfs_unlink,
  465. .symlink = nilfs_symlink,
  466. .mkdir = nilfs_mkdir,
  467. .rmdir = nilfs_rmdir,
  468. .mknod = nilfs_mknod,
  469. .rename = nilfs_rename,
  470. .setattr = nilfs_setattr,
  471. .permission = nilfs_permission,
  472. .fiemap = nilfs_fiemap,
  473. };
  474. const struct inode_operations nilfs_special_inode_operations = {
  475. .setattr = nilfs_setattr,
  476. .permission = nilfs_permission,
  477. };
  478. const struct inode_operations nilfs_symlink_inode_operations = {
  479. .readlink = generic_readlink,
  480. .get_link = page_get_link,
  481. .permission = nilfs_permission,
  482. };
  483. const struct export_operations nilfs_export_ops = {
  484. .encode_fh = nilfs_encode_fh,
  485. .fh_to_dentry = nilfs_fh_to_dentry,
  486. .fh_to_parent = nilfs_fh_to_parent,
  487. .get_parent = nilfs_get_parent,
  488. };