inode.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_blocks = 0;
  70. inode->i_version = vnode->fid.unique;
  71. inode->i_mapping->a_ops = &afs_fs_aops;
  72. /* check to see whether a symbolic link is really a mountpoint */
  73. if (vnode->status.type == AFS_FTYPE_SYMLINK) {
  74. afs_mntpt_check_symlink(vnode);
  75. if (vnode->flags & AFS_VNODE_MOUNTPOINT) {
  76. inode->i_mode = S_IFDIR | vnode->status.mode;
  77. inode->i_op = &afs_mntpt_inode_operations;
  78. inode->i_fop = &afs_mntpt_file_operations;
  79. }
  80. }
  81. return 0;
  82. } /* end afs_inode_map_status() */
  83. /*****************************************************************************/
  84. /*
  85. * attempt to fetch the status of an inode, coelescing multiple simultaneous
  86. * fetches
  87. */
  88. static int afs_inode_fetch_status(struct inode *inode)
  89. {
  90. struct afs_vnode *vnode;
  91. int ret;
  92. vnode = AFS_FS_I(inode);
  93. ret = afs_vnode_fetch_status(vnode);
  94. if (ret == 0)
  95. ret = afs_inode_map_status(vnode);
  96. return ret;
  97. } /* end afs_inode_fetch_status() */
  98. /*****************************************************************************/
  99. /*
  100. * iget5() comparator
  101. */
  102. static int afs_iget5_test(struct inode *inode, void *opaque)
  103. {
  104. struct afs_iget_data *data = opaque;
  105. return inode->i_ino == data->fid.vnode &&
  106. inode->i_version == data->fid.unique;
  107. } /* end afs_iget5_test() */
  108. /*****************************************************************************/
  109. /*
  110. * iget5() inode initialiser
  111. */
  112. static int afs_iget5_set(struct inode *inode, void *opaque)
  113. {
  114. struct afs_iget_data *data = opaque;
  115. struct afs_vnode *vnode = AFS_FS_I(inode);
  116. inode->i_ino = data->fid.vnode;
  117. inode->i_version = data->fid.unique;
  118. vnode->fid = data->fid;
  119. vnode->volume = data->volume;
  120. return 0;
  121. } /* end afs_iget5_set() */
  122. /*****************************************************************************/
  123. /*
  124. * inode retrieval
  125. */
  126. inline int afs_iget(struct super_block *sb, struct afs_fid *fid,
  127. struct inode **_inode)
  128. {
  129. struct afs_iget_data data = { .fid = *fid };
  130. struct afs_super_info *as;
  131. struct afs_vnode *vnode;
  132. struct inode *inode;
  133. int ret;
  134. _enter(",{%u,%u,%u},,", fid->vid, fid->vnode, fid->unique);
  135. as = sb->s_fs_info;
  136. data.volume = as->volume;
  137. inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
  138. &data);
  139. if (!inode) {
  140. _leave(" = -ENOMEM");
  141. return -ENOMEM;
  142. }
  143. vnode = AFS_FS_I(inode);
  144. /* deal with an existing inode */
  145. if (!(inode->i_state & I_NEW)) {
  146. ret = afs_vnode_fetch_status(vnode);
  147. if (ret==0)
  148. *_inode = inode;
  149. else
  150. iput(inode);
  151. _leave(" = %d", ret);
  152. return ret;
  153. }
  154. #ifdef AFS_CACHING_SUPPORT
  155. /* set up caching before reading the status, as fetch-status reads the
  156. * first page of symlinks to see if they're really mntpts */
  157. cachefs_acquire_cookie(vnode->volume->cache,
  158. NULL,
  159. vnode,
  160. &vnode->cache);
  161. #endif
  162. /* okay... it's a new inode */
  163. inode->i_flags |= S_NOATIME;
  164. vnode->flags |= AFS_VNODE_CHANGED;
  165. ret = afs_inode_fetch_status(inode);
  166. if (ret<0)
  167. goto bad_inode;
  168. /* success */
  169. unlock_new_inode(inode);
  170. *_inode = inode;
  171. _leave(" = 0 [CB { v=%u x=%lu t=%u }]",
  172. vnode->cb_version,
  173. vnode->cb_timeout.timo_jif,
  174. vnode->cb_type);
  175. return 0;
  176. /* failure */
  177. bad_inode:
  178. make_bad_inode(inode);
  179. unlock_new_inode(inode);
  180. iput(inode);
  181. _leave(" = %d [bad]", ret);
  182. return ret;
  183. } /* end afs_iget() */
  184. /*****************************************************************************/
  185. /*
  186. * read the attributes of an inode
  187. */
  188. int afs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
  189. struct kstat *stat)
  190. {
  191. struct afs_vnode *vnode;
  192. struct inode *inode;
  193. int ret;
  194. inode = dentry->d_inode;
  195. _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
  196. vnode = AFS_FS_I(inode);
  197. ret = afs_inode_fetch_status(inode);
  198. if (ret == -ENOENT) {
  199. _leave(" = %d [%d %p]",
  200. ret, atomic_read(&dentry->d_count), dentry->d_inode);
  201. return ret;
  202. }
  203. else if (ret < 0) {
  204. make_bad_inode(inode);
  205. _leave(" = %d", ret);
  206. return ret;
  207. }
  208. /* transfer attributes from the inode structure to the stat
  209. * structure */
  210. generic_fillattr(inode, stat);
  211. _leave(" = 0 CB { v=%u x=%u t=%u }",
  212. vnode->cb_version,
  213. vnode->cb_expiry,
  214. vnode->cb_type);
  215. return 0;
  216. } /* end afs_inode_getattr() */
  217. /*****************************************************************************/
  218. /*
  219. * clear an AFS inode
  220. */
  221. void afs_clear_inode(struct inode *inode)
  222. {
  223. struct afs_vnode *vnode;
  224. vnode = AFS_FS_I(inode);
  225. _enter("ino=%lu { vn=%08x v=%u x=%u t=%u }",
  226. inode->i_ino,
  227. vnode->fid.vnode,
  228. vnode->cb_version,
  229. vnode->cb_expiry,
  230. vnode->cb_type
  231. );
  232. BUG_ON(inode->i_ino != vnode->fid.vnode);
  233. afs_vnode_give_up_callback(vnode);
  234. #ifdef AFS_CACHING_SUPPORT
  235. cachefs_relinquish_cookie(vnode->cache, 0);
  236. vnode->cache = NULL;
  237. #endif
  238. _leave("");
  239. } /* end afs_clear_inode() */