inode.c 9.8 KB

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