inode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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@infradead.org>
  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/fs.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/sched.h>
  21. #include "internal.h"
  22. struct afs_iget_data {
  23. struct afs_fid fid;
  24. struct afs_volume *volume; /* volume on which resides */
  25. };
  26. /*
  27. * map the AFS file status to the inode member variables
  28. */
  29. static int afs_inode_map_status(struct afs_vnode *vnode, struct key *key)
  30. {
  31. struct inode *inode = AFS_VNODE_TO_I(vnode);
  32. _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
  33. vnode->status.type,
  34. vnode->status.nlink,
  35. (unsigned long long) vnode->status.size,
  36. vnode->status.data_version,
  37. vnode->status.mode);
  38. switch (vnode->status.type) {
  39. case AFS_FTYPE_FILE:
  40. inode->i_mode = S_IFREG | vnode->status.mode;
  41. inode->i_op = &afs_file_inode_operations;
  42. inode->i_fop = &afs_file_operations;
  43. break;
  44. case AFS_FTYPE_DIR:
  45. inode->i_mode = S_IFDIR | vnode->status.mode;
  46. inode->i_op = &afs_dir_inode_operations;
  47. inode->i_fop = &afs_dir_file_operations;
  48. break;
  49. case AFS_FTYPE_SYMLINK:
  50. inode->i_mode = S_IFLNK | vnode->status.mode;
  51. inode->i_op = &page_symlink_inode_operations;
  52. break;
  53. default:
  54. printk("kAFS: AFS vnode with undefined type\n");
  55. return -EBADMSG;
  56. }
  57. #ifdef CONFIG_AFS_FSCACHE
  58. if (vnode->status.size != inode->i_size)
  59. fscache_attr_changed(vnode->cache);
  60. #endif
  61. inode->i_nlink = vnode->status.nlink;
  62. inode->i_uid = vnode->status.owner;
  63. inode->i_gid = 0;
  64. inode->i_size = vnode->status.size;
  65. inode->i_ctime.tv_sec = vnode->status.mtime_server;
  66. inode->i_ctime.tv_nsec = 0;
  67. inode->i_atime = inode->i_mtime = inode->i_ctime;
  68. inode->i_blocks = 0;
  69. inode->i_version = vnode->fid.unique;
  70. inode->i_mapping->a_ops = &afs_fs_aops;
  71. /* check to see whether a symbolic link is really a mountpoint */
  72. if (vnode->status.type == AFS_FTYPE_SYMLINK) {
  73. afs_mntpt_check_symlink(vnode, key);
  74. if (test_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags)) {
  75. inode->i_mode = S_IFDIR | vnode->status.mode;
  76. inode->i_op = &afs_mntpt_inode_operations;
  77. inode->i_fop = &afs_mntpt_file_operations;
  78. }
  79. }
  80. return 0;
  81. }
  82. /*
  83. * iget5() comparator
  84. */
  85. static int afs_iget5_test(struct inode *inode, void *opaque)
  86. {
  87. struct afs_iget_data *data = opaque;
  88. return inode->i_ino == data->fid.vnode &&
  89. inode->i_version == data->fid.unique;
  90. }
  91. /*
  92. * iget5() inode initialiser
  93. */
  94. static int afs_iget5_set(struct inode *inode, void *opaque)
  95. {
  96. struct afs_iget_data *data = opaque;
  97. struct afs_vnode *vnode = AFS_FS_I(inode);
  98. inode->i_ino = data->fid.vnode;
  99. inode->i_version = data->fid.unique;
  100. vnode->fid = data->fid;
  101. vnode->volume = data->volume;
  102. return 0;
  103. }
  104. /*
  105. * inode retrieval
  106. */
  107. struct inode *afs_iget(struct super_block *sb, struct key *key,
  108. struct afs_fid *fid, struct afs_file_status *status,
  109. struct afs_callback *cb)
  110. {
  111. struct afs_iget_data data = { .fid = *fid };
  112. struct afs_super_info *as;
  113. struct afs_vnode *vnode;
  114. struct inode *inode;
  115. int ret;
  116. _enter(",{%x:%u.%u},,", fid->vid, fid->vnode, fid->unique);
  117. as = sb->s_fs_info;
  118. data.volume = as->volume;
  119. inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
  120. &data);
  121. if (!inode) {
  122. _leave(" = -ENOMEM");
  123. return ERR_PTR(-ENOMEM);
  124. }
  125. _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
  126. inode, fid->vid, fid->vnode, fid->unique);
  127. vnode = AFS_FS_I(inode);
  128. /* deal with an existing inode */
  129. if (!(inode->i_state & I_NEW)) {
  130. _leave(" = %p", inode);
  131. return inode;
  132. }
  133. if (!status) {
  134. /* it's a remotely extant inode */
  135. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  136. ret = afs_vnode_fetch_status(vnode, NULL, key);
  137. if (ret < 0)
  138. goto bad_inode;
  139. } else {
  140. /* it's an inode we just created */
  141. memcpy(&vnode->status, status, sizeof(vnode->status));
  142. if (!cb) {
  143. /* it's a symlink we just created (the fileserver
  144. * didn't give us a callback) */
  145. vnode->cb_version = 0;
  146. vnode->cb_expiry = 0;
  147. vnode->cb_type = 0;
  148. vnode->cb_expires = get_seconds();
  149. } else {
  150. vnode->cb_version = cb->version;
  151. vnode->cb_expiry = cb->expiry;
  152. vnode->cb_type = cb->type;
  153. vnode->cb_expires = vnode->cb_expiry + get_seconds();
  154. }
  155. }
  156. /* set up caching before mapping the status, as map-status reads the
  157. * first page of symlinks to see if they're really mountpoints */
  158. inode->i_size = vnode->status.size;
  159. #ifdef CONFIG_AFS_FSCACHE
  160. vnode->cache = fscache_acquire_cookie(vnode->volume->cache,
  161. &afs_vnode_cache_index_def,
  162. vnode);
  163. #endif
  164. ret = afs_inode_map_status(vnode, key);
  165. if (ret < 0)
  166. goto bad_inode;
  167. /* success */
  168. clear_bit(AFS_VNODE_UNSET, &vnode->flags);
  169. inode->i_flags |= S_NOATIME;
  170. unlock_new_inode(inode);
  171. _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
  172. return inode;
  173. /* failure */
  174. bad_inode:
  175. #ifdef CONFIG_AFS_FSCACHE
  176. fscache_relinquish_cookie(vnode->cache, 0);
  177. vnode->cache = NULL;
  178. #endif
  179. iget_failed(inode);
  180. _leave(" = %d [bad]", ret);
  181. return ERR_PTR(ret);
  182. }
  183. /*
  184. * mark the data attached to an inode as obsolete due to a write on the server
  185. * - might also want to ditch all the outstanding writes and dirty pages
  186. */
  187. void afs_zap_data(struct afs_vnode *vnode)
  188. {
  189. _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
  190. /* nuke all the non-dirty pages that aren't locked, mapped or being
  191. * written back in a regular file and completely discard the pages in a
  192. * directory or symlink */
  193. if (S_ISREG(vnode->vfs_inode.i_mode))
  194. invalidate_remote_inode(&vnode->vfs_inode);
  195. else
  196. invalidate_inode_pages2(vnode->vfs_inode.i_mapping);
  197. }
  198. /*
  199. * validate a vnode/inode
  200. * - there are several things we need to check
  201. * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
  202. * symlink)
  203. * - parent dir metadata changed (security changes)
  204. * - dentry data changed (write, truncate)
  205. * - dentry metadata changed (security changes)
  206. */
  207. int afs_validate(struct afs_vnode *vnode, struct key *key)
  208. {
  209. int ret;
  210. _enter("{v={%x:%u} fl=%lx},%x",
  211. vnode->fid.vid, vnode->fid.vnode, vnode->flags,
  212. key_serial(key));
  213. if (vnode->cb_promised &&
  214. !test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags) &&
  215. !test_bit(AFS_VNODE_MODIFIED, &vnode->flags) &&
  216. !test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
  217. if (vnode->cb_expires < get_seconds() + 10) {
  218. _debug("callback expired");
  219. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  220. } else {
  221. goto valid;
  222. }
  223. }
  224. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  225. goto valid;
  226. mutex_lock(&vnode->validate_lock);
  227. /* if the promise has expired, we need to check the server again to get
  228. * a new promise - note that if the (parent) directory's metadata was
  229. * changed then the security may be different and we may no longer have
  230. * access */
  231. if (!vnode->cb_promised ||
  232. test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
  233. _debug("not promised");
  234. ret = afs_vnode_fetch_status(vnode, NULL, key);
  235. if (ret < 0)
  236. goto error_unlock;
  237. _debug("new promise [fl=%lx]", vnode->flags);
  238. }
  239. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  240. _debug("file already deleted");
  241. ret = -ESTALE;
  242. goto error_unlock;
  243. }
  244. /* if the vnode's data version number changed then its contents are
  245. * different */
  246. if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
  247. afs_zap_data(vnode);
  248. clear_bit(AFS_VNODE_MODIFIED, &vnode->flags);
  249. mutex_unlock(&vnode->validate_lock);
  250. valid:
  251. _leave(" = 0");
  252. return 0;
  253. error_unlock:
  254. mutex_unlock(&vnode->validate_lock);
  255. _leave(" = %d", ret);
  256. return ret;
  257. }
  258. /*
  259. * read the attributes of an inode
  260. */
  261. int afs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  262. struct kstat *stat)
  263. {
  264. struct inode *inode;
  265. inode = dentry->d_inode;
  266. _enter("{ ino=%lu v=%llu }", inode->i_ino,
  267. (unsigned long long)inode->i_version);
  268. generic_fillattr(inode, stat);
  269. return 0;
  270. }
  271. /*
  272. * clear an AFS inode
  273. */
  274. void afs_clear_inode(struct inode *inode)
  275. {
  276. struct afs_permits *permits;
  277. struct afs_vnode *vnode;
  278. vnode = AFS_FS_I(inode);
  279. _enter("{%x:%u.%d} v=%u x=%u t=%u }",
  280. vnode->fid.vid,
  281. vnode->fid.vnode,
  282. vnode->fid.unique,
  283. vnode->cb_version,
  284. vnode->cb_expiry,
  285. vnode->cb_type);
  286. _debug("CLEAR INODE %p", inode);
  287. ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
  288. afs_give_up_callback(vnode);
  289. if (vnode->server) {
  290. spin_lock(&vnode->server->fs_lock);
  291. rb_erase(&vnode->server_rb, &vnode->server->fs_vnodes);
  292. spin_unlock(&vnode->server->fs_lock);
  293. afs_put_server(vnode->server);
  294. vnode->server = NULL;
  295. }
  296. ASSERT(list_empty(&vnode->writebacks));
  297. ASSERT(!vnode->cb_promised);
  298. #ifdef CONFIG_AFS_FSCACHE
  299. fscache_relinquish_cookie(vnode->cache, 0);
  300. vnode->cache = NULL;
  301. #endif
  302. mutex_lock(&vnode->permits_lock);
  303. permits = vnode->permits;
  304. rcu_assign_pointer(vnode->permits, NULL);
  305. mutex_unlock(&vnode->permits_lock);
  306. if (permits)
  307. call_rcu(&permits->rcu, afs_zap_permits);
  308. _leave("");
  309. }
  310. /*
  311. * set the attributes of an inode
  312. */
  313. int afs_setattr(struct dentry *dentry, struct iattr *attr)
  314. {
  315. struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
  316. struct key *key;
  317. int ret;
  318. _enter("{%x:%u},{n=%s},%x",
  319. vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
  320. attr->ia_valid);
  321. if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
  322. ATTR_MTIME))) {
  323. _leave(" = 0 [unsupported]");
  324. return 0;
  325. }
  326. /* flush any dirty data outstanding on a regular file */
  327. if (S_ISREG(vnode->vfs_inode.i_mode)) {
  328. filemap_write_and_wait(vnode->vfs_inode.i_mapping);
  329. afs_writeback_all(vnode);
  330. }
  331. if (attr->ia_valid & ATTR_FILE) {
  332. key = attr->ia_file->private_data;
  333. } else {
  334. key = afs_request_key(vnode->volume->cell);
  335. if (IS_ERR(key)) {
  336. ret = PTR_ERR(key);
  337. goto error;
  338. }
  339. }
  340. ret = afs_vnode_setattr(vnode, key, attr);
  341. if (!(attr->ia_valid & ATTR_FILE))
  342. key_put(key);
  343. error:
  344. _leave(" = %d", ret);
  345. return ret;
  346. }