inode.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
  3. *
  4. * This software may be freely redistributed under the terms of the
  5. * GNU General Public License.
  6. *
  7. * You should have received a copy of the GNU General Public License
  8. * along with this program; if not, write to the Free Software
  9. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  10. *
  11. * Authors: David Woodhouse <dwmw2@cambridge.redhat.com>
  12. * David Howells <dhowells@redhat.com>
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/fs.h>
  21. #include <linux/pagemap.h>
  22. #include "volume.h"
  23. #include "vnode.h"
  24. #include "super.h"
  25. #include "internal.h"
  26. struct afs_iget_data {
  27. struct afs_fid fid;
  28. struct afs_volume *volume; /* volume on which resides */
  29. };
  30. /*****************************************************************************/
  31. /*
  32. * map the AFS file status to the inode member variables
  33. */
  34. static int afs_inode_map_status(struct afs_vnode *vnode)
  35. {
  36. struct inode *inode = AFS_VNODE_TO_I(vnode);
  37. _debug("FS: ft=%d lk=%d sz=%Zu ver=%Lu mod=%hu",
  38. vnode->status.type,
  39. vnode->status.nlink,
  40. vnode->status.size,
  41. vnode->status.version,
  42. vnode->status.mode);
  43. switch (vnode->status.type) {
  44. case AFS_FTYPE_FILE:
  45. inode->i_mode = S_IFREG | vnode->status.mode;
  46. inode->i_op = &afs_file_inode_operations;
  47. inode->i_fop = &generic_ro_fops;
  48. break;
  49. case AFS_FTYPE_DIR:
  50. inode->i_mode = S_IFDIR | vnode->status.mode;
  51. inode->i_op = &afs_dir_inode_operations;
  52. inode->i_fop = &afs_dir_file_operations;
  53. break;
  54. case AFS_FTYPE_SYMLINK:
  55. inode->i_mode = S_IFLNK | vnode->status.mode;
  56. inode->i_op = &page_symlink_inode_operations;
  57. break;
  58. default:
  59. printk("kAFS: AFS vnode with undefined type\n");
  60. return -EBADMSG;
  61. }
  62. inode->i_nlink = vnode->status.nlink;
  63. inode->i_uid = vnode->status.owner;
  64. inode->i_gid = 0;
  65. inode->i_size = vnode->status.size;
  66. inode->i_ctime.tv_sec = vnode->status.mtime_server;
  67. inode->i_ctime.tv_nsec = 0;
  68. inode->i_atime = inode->i_mtime = inode->i_ctime;
  69. inode->i_blksize = PAGE_CACHE_SIZE;
  70. inode->i_blocks = 0;
  71. inode->i_version = vnode->fid.unique;
  72. inode->i_mapping->a_ops = &afs_fs_aops;
  73. /* check to see whether a symbolic link is really a mountpoint */
  74. if (vnode->status.type == AFS_FTYPE_SYMLINK) {
  75. afs_mntpt_check_symlink(vnode);
  76. if (vnode->flags & AFS_VNODE_MOUNTPOINT) {
  77. inode->i_mode = S_IFDIR | vnode->status.mode;
  78. inode->i_op = &afs_mntpt_inode_operations;
  79. inode->i_fop = &afs_mntpt_file_operations;
  80. }
  81. }
  82. return 0;
  83. } /* end afs_inode_map_status() */
  84. /*****************************************************************************/
  85. /*
  86. * attempt to fetch the status of an inode, coelescing multiple simultaneous
  87. * fetches
  88. */
  89. static int afs_inode_fetch_status(struct inode *inode)
  90. {
  91. struct afs_vnode *vnode;
  92. int ret;
  93. vnode = AFS_FS_I(inode);
  94. ret = afs_vnode_fetch_status(vnode);
  95. if (ret == 0)
  96. ret = afs_inode_map_status(vnode);
  97. return ret;
  98. } /* end afs_inode_fetch_status() */
  99. /*****************************************************************************/
  100. /*
  101. * iget5() comparator
  102. */
  103. static int afs_iget5_test(struct inode *inode, void *opaque)
  104. {
  105. struct afs_iget_data *data = opaque;
  106. return inode->i_ino == data->fid.vnode &&
  107. inode->i_version == data->fid.unique;
  108. } /* end afs_iget5_test() */
  109. /*****************************************************************************/
  110. /*
  111. * iget5() inode initialiser
  112. */
  113. static int afs_iget5_set(struct inode *inode, void *opaque)
  114. {
  115. struct afs_iget_data *data = opaque;
  116. struct afs_vnode *vnode = AFS_FS_I(inode);
  117. inode->i_ino = data->fid.vnode;
  118. inode->i_version = data->fid.unique;
  119. vnode->fid = data->fid;
  120. vnode->volume = data->volume;
  121. return 0;
  122. } /* end afs_iget5_set() */
  123. /*****************************************************************************/
  124. /*
  125. * inode retrieval
  126. */
  127. inline int afs_iget(struct super_block *sb, struct afs_fid *fid,
  128. struct inode **_inode)
  129. {
  130. struct afs_iget_data data = { .fid = *fid };
  131. struct afs_super_info *as;
  132. struct afs_vnode *vnode;
  133. struct inode *inode;
  134. int ret;
  135. _enter(",{%u,%u,%u},,", fid->vid, fid->vnode, fid->unique);
  136. as = sb->s_fs_info;
  137. data.volume = as->volume;
  138. inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
  139. &data);
  140. if (!inode) {
  141. _leave(" = -ENOMEM");
  142. return -ENOMEM;
  143. }
  144. vnode = AFS_FS_I(inode);
  145. /* deal with an existing inode */
  146. if (!(inode->i_state & I_NEW)) {
  147. ret = afs_vnode_fetch_status(vnode);
  148. if (ret==0)
  149. *_inode = inode;
  150. else
  151. iput(inode);
  152. _leave(" = %d", ret);
  153. return ret;
  154. }
  155. #ifdef AFS_CACHING_SUPPORT
  156. /* set up caching before reading the status, as fetch-status reads the
  157. * first page of symlinks to see if they're really mntpts */
  158. cachefs_acquire_cookie(vnode->volume->cache,
  159. NULL,
  160. vnode,
  161. &vnode->cache);
  162. #endif
  163. /* okay... it's a new inode */
  164. inode->i_flags |= S_NOATIME;
  165. vnode->flags |= AFS_VNODE_CHANGED;
  166. ret = afs_inode_fetch_status(inode);
  167. if (ret<0)
  168. goto bad_inode;
  169. /* success */
  170. unlock_new_inode(inode);
  171. *_inode = inode;
  172. _leave(" = 0 [CB { v=%u x=%lu t=%u }]",
  173. vnode->cb_version,
  174. vnode->cb_timeout.timo_jif,
  175. vnode->cb_type);
  176. return 0;
  177. /* failure */
  178. bad_inode:
  179. make_bad_inode(inode);
  180. unlock_new_inode(inode);
  181. iput(inode);
  182. _leave(" = %d [bad]", ret);
  183. return ret;
  184. } /* end afs_iget() */
  185. /*****************************************************************************/
  186. /*
  187. * read the attributes of an inode
  188. */
  189. int afs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
  190. struct kstat *stat)
  191. {
  192. struct afs_vnode *vnode;
  193. struct inode *inode;
  194. int ret;
  195. inode = dentry->d_inode;
  196. _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
  197. vnode = AFS_FS_I(inode);
  198. ret = afs_inode_fetch_status(inode);
  199. if (ret == -ENOENT) {
  200. _leave(" = %d [%d %p]",
  201. ret, atomic_read(&dentry->d_count), dentry->d_inode);
  202. return ret;
  203. }
  204. else if (ret < 0) {
  205. make_bad_inode(inode);
  206. _leave(" = %d", ret);
  207. return ret;
  208. }
  209. /* transfer attributes from the inode structure to the stat
  210. * structure */
  211. generic_fillattr(inode, stat);
  212. _leave(" = 0 CB { v=%u x=%u t=%u }",
  213. vnode->cb_version,
  214. vnode->cb_expiry,
  215. vnode->cb_type);
  216. return 0;
  217. } /* end afs_inode_getattr() */
  218. /*****************************************************************************/
  219. /*
  220. * clear an AFS inode
  221. */
  222. void afs_clear_inode(struct inode *inode)
  223. {
  224. struct afs_vnode *vnode;
  225. vnode = AFS_FS_I(inode);
  226. _enter("ino=%lu { vn=%08x v=%u x=%u t=%u }",
  227. inode->i_ino,
  228. vnode->fid.vnode,
  229. vnode->cb_version,
  230. vnode->cb_expiry,
  231. vnode->cb_type
  232. );
  233. BUG_ON(inode->i_ino != vnode->fid.vnode);
  234. afs_vnode_give_up_callback(vnode);
  235. #ifdef AFS_CACHING_SUPPORT
  236. cachefs_relinquish_cookie(vnode->cache, 0);
  237. vnode->cache = NULL;
  238. #endif
  239. _leave("");
  240. } /* end afs_clear_inode() */