inode.c 12 KB

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