namei.c 13 KB

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