symlink.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * linux/fs/nfs/symlink.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * Optimization changes Copyright (C) 1994 Florian La Roche
  7. *
  8. * Jun 7 1999, cache symlink lookups in the page cache. -DaveM
  9. *
  10. * nfs symlink handling code
  11. */
  12. #define NFS_NEED_XDR_TYPES
  13. #include <linux/time.h>
  14. #include <linux/errno.h>
  15. #include <linux/sunrpc/clnt.h>
  16. #include <linux/nfs.h>
  17. #include <linux/nfs2.h>
  18. #include <linux/nfs_fs.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/stat.h>
  21. #include <linux/mm.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/namei.h>
  26. /* Symlink caching in the page cache is even more simplistic
  27. * and straight-forward than readdir caching.
  28. *
  29. * At the beginning of the page we store pointer to struct page in question,
  30. * simplifying nfs_put_link() (if inode got invalidated we can't find the page
  31. * to be freed via pagecache lookup).
  32. * The NUL-terminated string follows immediately thereafter.
  33. */
  34. struct nfs_symlink {
  35. struct page *page;
  36. char body[0];
  37. };
  38. static int nfs_symlink_filler(struct inode *inode, struct page *page)
  39. {
  40. const unsigned int pgbase = offsetof(struct nfs_symlink, body);
  41. const unsigned int pglen = PAGE_SIZE - pgbase;
  42. int error;
  43. lock_kernel();
  44. error = NFS_PROTO(inode)->readlink(inode, page, pgbase, pglen);
  45. unlock_kernel();
  46. if (error < 0)
  47. goto error;
  48. SetPageUptodate(page);
  49. unlock_page(page);
  50. return 0;
  51. error:
  52. SetPageError(page);
  53. unlock_page(page);
  54. return -EIO;
  55. }
  56. static int nfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  57. {
  58. struct inode *inode = dentry->d_inode;
  59. struct page *page;
  60. struct nfs_symlink *p;
  61. void *err = ERR_PTR(nfs_revalidate_inode(NFS_SERVER(inode), inode));
  62. if (err)
  63. goto read_failed;
  64. page = read_cache_page(&inode->i_data, 0,
  65. (filler_t *)nfs_symlink_filler, inode);
  66. if (IS_ERR(page)) {
  67. err = page;
  68. goto read_failed;
  69. }
  70. if (!PageUptodate(page)) {
  71. err = ERR_PTR(-EIO);
  72. goto getlink_read_error;
  73. }
  74. p = kmap(page);
  75. p->page = page;
  76. nd_set_link(nd, p->body);
  77. return 0;
  78. getlink_read_error:
  79. page_cache_release(page);
  80. read_failed:
  81. nd_set_link(nd, err);
  82. return 0;
  83. }
  84. static void nfs_put_link(struct dentry *dentry, struct nameidata *nd)
  85. {
  86. char *s = nd_get_link(nd);
  87. if (!IS_ERR(s)) {
  88. struct nfs_symlink *p;
  89. struct page *page;
  90. p = container_of(s, struct nfs_symlink, body[0]);
  91. page = p->page;
  92. kunmap(page);
  93. page_cache_release(page);
  94. }
  95. }
  96. /*
  97. * symlinks can't do much...
  98. */
  99. struct inode_operations nfs_symlink_inode_operations = {
  100. .readlink = generic_readlink,
  101. .follow_link = nfs_follow_link,
  102. .put_link = nfs_put_link,
  103. .getattr = nfs_getattr,
  104. .setattr = nfs_setattr,
  105. };