inode.c 12 KB

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