inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 <linux/iversion.h>
  24. #include "internal.h"
  25. static const struct inode_operations afs_symlink_inode_operations = {
  26. .get_link = page_get_link,
  27. .listxattr = afs_listxattr,
  28. };
  29. /*
  30. * Initialise an inode from the vnode status.
  31. */
  32. static int afs_inode_init_from_status(struct afs_vnode *vnode, struct key *key)
  33. {
  34. struct inode *inode = AFS_VNODE_TO_I(vnode);
  35. _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
  36. vnode->status.type,
  37. vnode->status.nlink,
  38. (unsigned long long) vnode->status.size,
  39. vnode->status.data_version,
  40. vnode->status.mode);
  41. read_seqlock_excl(&vnode->cb_lock);
  42. afs_update_inode_from_status(vnode, &vnode->status, NULL,
  43. AFS_VNODE_NOT_YET_SET);
  44. switch (vnode->status.type) {
  45. case AFS_FTYPE_FILE:
  46. inode->i_mode = S_IFREG | vnode->status.mode;
  47. inode->i_op = &afs_file_inode_operations;
  48. inode->i_fop = &afs_file_operations;
  49. inode->i_mapping->a_ops = &afs_fs_aops;
  50. break;
  51. case AFS_FTYPE_DIR:
  52. inode->i_mode = S_IFDIR | vnode->status.mode;
  53. inode->i_op = &afs_dir_inode_operations;
  54. inode->i_fop = &afs_dir_file_operations;
  55. inode->i_mapping->a_ops = &afs_dir_aops;
  56. break;
  57. case AFS_FTYPE_SYMLINK:
  58. /* Symlinks with a mode of 0644 are actually mountpoints. */
  59. if ((vnode->status.mode & 0777) == 0644) {
  60. inode->i_flags |= S_AUTOMOUNT;
  61. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  62. inode->i_mode = S_IFDIR | 0555;
  63. inode->i_op = &afs_mntpt_inode_operations;
  64. inode->i_fop = &afs_mntpt_file_operations;
  65. inode->i_mapping->a_ops = &afs_fs_aops;
  66. } else {
  67. inode->i_mode = S_IFLNK | vnode->status.mode;
  68. inode->i_op = &afs_symlink_inode_operations;
  69. inode->i_mapping->a_ops = &afs_fs_aops;
  70. }
  71. inode_nohighmem(inode);
  72. break;
  73. default:
  74. printk("kAFS: AFS vnode with undefined type\n");
  75. read_sequnlock_excl(&vnode->cb_lock);
  76. return afs_protocol_error(NULL, -EBADMSG, afs_eproto_file_type);
  77. }
  78. inode->i_blocks = 0;
  79. vnode->invalid_before = vnode->status.data_version;
  80. read_sequnlock_excl(&vnode->cb_lock);
  81. return 0;
  82. }
  83. /*
  84. * Fetch file status from the volume.
  85. */
  86. int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool new_inode)
  87. {
  88. struct afs_fs_cursor fc;
  89. int ret;
  90. _enter("%s,{%llx:%llu.%u,S=%lx}",
  91. vnode->volume->name,
  92. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
  93. vnode->flags);
  94. ret = -ERESTARTSYS;
  95. if (afs_begin_vnode_operation(&fc, vnode, key)) {
  96. while (afs_select_fileserver(&fc)) {
  97. fc.cb_break = afs_calc_vnode_cb_break(vnode);
  98. afs_fs_fetch_file_status(&fc, NULL, new_inode);
  99. }
  100. afs_check_for_remote_deletion(&fc, fc.vnode);
  101. afs_vnode_commit_status(&fc, vnode, fc.cb_break);
  102. ret = afs_end_vnode_operation(&fc);
  103. }
  104. _leave(" = %d", ret);
  105. return ret;
  106. }
  107. /*
  108. * iget5() comparator
  109. */
  110. int afs_iget5_test(struct inode *inode, void *opaque)
  111. {
  112. struct afs_iget_data *data = opaque;
  113. struct afs_vnode *vnode = AFS_FS_I(inode);
  114. return memcmp(&vnode->fid, &data->fid, sizeof(data->fid)) == 0;
  115. }
  116. /*
  117. * iget5() comparator for inode created by autocell operations
  118. *
  119. * These pseudo inodes don't match anything.
  120. */
  121. static int afs_iget5_pseudo_dir_test(struct inode *inode, void *opaque)
  122. {
  123. return 0;
  124. }
  125. /*
  126. * iget5() inode initialiser
  127. */
  128. static int afs_iget5_set(struct inode *inode, void *opaque)
  129. {
  130. struct afs_iget_data *data = opaque;
  131. struct afs_vnode *vnode = AFS_FS_I(inode);
  132. vnode->fid = data->fid;
  133. vnode->volume = data->volume;
  134. /* YFS supports 96-bit vnode IDs, but Linux only supports
  135. * 64-bit inode numbers.
  136. */
  137. inode->i_ino = data->fid.vnode;
  138. inode->i_generation = data->fid.unique;
  139. return 0;
  140. }
  141. /*
  142. * Create an inode for a dynamic root directory or an autocell dynamic
  143. * automount dir.
  144. */
  145. struct inode *afs_iget_pseudo_dir(struct super_block *sb, bool root)
  146. {
  147. struct afs_iget_data data;
  148. struct afs_super_info *as;
  149. struct afs_vnode *vnode;
  150. struct inode *inode;
  151. static atomic_t afs_autocell_ino;
  152. _enter("");
  153. as = sb->s_fs_info;
  154. if (as->volume) {
  155. data.volume = as->volume;
  156. data.fid.vid = as->volume->vid;
  157. }
  158. if (root) {
  159. data.fid.vnode = 1;
  160. data.fid.unique = 1;
  161. } else {
  162. data.fid.vnode = atomic_inc_return(&afs_autocell_ino);
  163. data.fid.unique = 0;
  164. }
  165. inode = iget5_locked(sb, data.fid.vnode,
  166. afs_iget5_pseudo_dir_test, afs_iget5_set,
  167. &data);
  168. if (!inode) {
  169. _leave(" = -ENOMEM");
  170. return ERR_PTR(-ENOMEM);
  171. }
  172. _debug("GOT INODE %p { ino=%lu, vl=%llx, vn=%llx, u=%x }",
  173. inode, inode->i_ino, data.fid.vid, data.fid.vnode,
  174. data.fid.unique);
  175. vnode = AFS_FS_I(inode);
  176. /* there shouldn't be an existing inode */
  177. BUG_ON(!(inode->i_state & I_NEW));
  178. inode->i_size = 0;
  179. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  180. if (root) {
  181. inode->i_op = &afs_dynroot_inode_operations;
  182. inode->i_fop = &afs_dynroot_file_operations;
  183. } else {
  184. inode->i_op = &afs_autocell_inode_operations;
  185. }
  186. set_nlink(inode, 2);
  187. inode->i_uid = GLOBAL_ROOT_UID;
  188. inode->i_gid = GLOBAL_ROOT_GID;
  189. inode->i_ctime.tv_sec = get_seconds();
  190. inode->i_ctime.tv_nsec = 0;
  191. inode->i_atime = inode->i_mtime = inode->i_ctime;
  192. inode->i_blocks = 0;
  193. inode_set_iversion_raw(inode, 0);
  194. inode->i_generation = 0;
  195. set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
  196. if (!root) {
  197. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  198. inode->i_flags |= S_AUTOMOUNT;
  199. }
  200. inode->i_flags |= S_NOATIME;
  201. unlock_new_inode(inode);
  202. _leave(" = %p", inode);
  203. return inode;
  204. }
  205. /*
  206. * Get a cache cookie for an inode.
  207. */
  208. static void afs_get_inode_cache(struct afs_vnode *vnode)
  209. {
  210. #ifdef CONFIG_AFS_FSCACHE
  211. struct {
  212. u32 vnode_id;
  213. u32 unique;
  214. u32 vnode_id_ext[2]; /* Allow for a 96-bit key */
  215. } __packed key;
  216. struct afs_vnode_cache_aux aux;
  217. if (vnode->status.type == AFS_FTYPE_DIR) {
  218. vnode->cache = NULL;
  219. return;
  220. }
  221. key.vnode_id = vnode->fid.vnode;
  222. key.unique = vnode->fid.unique;
  223. key.vnode_id_ext[0] = vnode->fid.vnode >> 32;
  224. key.vnode_id_ext[1] = vnode->fid.vnode_hi;
  225. aux.data_version = vnode->status.data_version;
  226. vnode->cache = fscache_acquire_cookie(vnode->volume->cache,
  227. &afs_vnode_cache_index_def,
  228. &key, sizeof(key),
  229. &aux, sizeof(aux),
  230. vnode, vnode->status.size, true);
  231. #endif
  232. }
  233. /*
  234. * inode retrieval
  235. */
  236. struct inode *afs_iget(struct super_block *sb, struct key *key,
  237. struct afs_fid *fid, struct afs_file_status *status,
  238. struct afs_callback *cb, struct afs_cb_interest *cbi)
  239. {
  240. struct afs_iget_data data = { .fid = *fid };
  241. struct afs_super_info *as;
  242. struct afs_vnode *vnode;
  243. struct inode *inode;
  244. int ret;
  245. _enter(",{%llx:%llu.%u},,", fid->vid, fid->vnode, fid->unique);
  246. as = sb->s_fs_info;
  247. data.volume = as->volume;
  248. inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
  249. &data);
  250. if (!inode) {
  251. _leave(" = -ENOMEM");
  252. return ERR_PTR(-ENOMEM);
  253. }
  254. _debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }",
  255. inode, fid->vid, fid->vnode, fid->unique);
  256. vnode = AFS_FS_I(inode);
  257. /* deal with an existing inode */
  258. if (!(inode->i_state & I_NEW)) {
  259. _leave(" = %p", inode);
  260. return inode;
  261. }
  262. if (!status) {
  263. /* it's a remotely extant inode */
  264. ret = afs_fetch_status(vnode, key, true);
  265. if (ret < 0)
  266. goto bad_inode;
  267. } else {
  268. /* it's an inode we just created */
  269. memcpy(&vnode->status, status, sizeof(vnode->status));
  270. if (!cb) {
  271. /* it's a symlink we just created (the fileserver
  272. * didn't give us a callback) */
  273. vnode->cb_version = 0;
  274. vnode->cb_type = 0;
  275. vnode->cb_expires_at = ktime_get();
  276. } else {
  277. vnode->cb_version = cb->version;
  278. vnode->cb_type = cb->type;
  279. vnode->cb_expires_at = cb->expires_at;
  280. vnode->cb_interest = afs_get_cb_interest(cbi);
  281. set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  282. }
  283. vnode->cb_expires_at += ktime_get_real_seconds();
  284. }
  285. ret = afs_inode_init_from_status(vnode, key);
  286. if (ret < 0)
  287. goto bad_inode;
  288. afs_get_inode_cache(vnode);
  289. /* success */
  290. clear_bit(AFS_VNODE_UNSET, &vnode->flags);
  291. inode->i_flags |= S_NOATIME;
  292. unlock_new_inode(inode);
  293. _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
  294. return inode;
  295. /* failure */
  296. bad_inode:
  297. iget_failed(inode);
  298. _leave(" = %d [bad]", ret);
  299. return ERR_PTR(ret);
  300. }
  301. /*
  302. * mark the data attached to an inode as obsolete due to a write on the server
  303. * - might also want to ditch all the outstanding writes and dirty pages
  304. */
  305. void afs_zap_data(struct afs_vnode *vnode)
  306. {
  307. _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
  308. #ifdef CONFIG_AFS_FSCACHE
  309. fscache_invalidate(vnode->cache);
  310. #endif
  311. /* nuke all the non-dirty pages that aren't locked, mapped or being
  312. * written back in a regular file and completely discard the pages in a
  313. * directory or symlink */
  314. if (S_ISREG(vnode->vfs_inode.i_mode))
  315. invalidate_remote_inode(&vnode->vfs_inode);
  316. else
  317. invalidate_inode_pages2(vnode->vfs_inode.i_mapping);
  318. }
  319. /*
  320. * validate a vnode/inode
  321. * - there are several things we need to check
  322. * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
  323. * symlink)
  324. * - parent dir metadata changed (security changes)
  325. * - dentry data changed (write, truncate)
  326. * - dentry metadata changed (security changes)
  327. */
  328. int afs_validate(struct afs_vnode *vnode, struct key *key)
  329. {
  330. time64_t now = ktime_get_real_seconds();
  331. bool valid;
  332. int ret;
  333. _enter("{v={%llx:%llu} fl=%lx},%x",
  334. vnode->fid.vid, vnode->fid.vnode, vnode->flags,
  335. key_serial(key));
  336. /* Quickly check the callback state. Ideally, we'd use read_seqbegin
  337. * here, but we have no way to pass the net namespace to the RCU
  338. * cleanup for the server record.
  339. */
  340. read_seqlock_excl(&vnode->cb_lock);
  341. if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
  342. if (vnode->cb_s_break != vnode->cb_interest->server->cb_s_break ||
  343. vnode->cb_v_break != vnode->volume->cb_v_break) {
  344. vnode->cb_s_break = vnode->cb_interest->server->cb_s_break;
  345. vnode->cb_v_break = vnode->volume->cb_v_break;
  346. valid = false;
  347. } else if (vnode->status.type == AFS_FTYPE_DIR &&
  348. (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags) ||
  349. vnode->cb_expires_at - 10 <= now)) {
  350. valid = false;
  351. } else if (test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags) ||
  352. vnode->cb_expires_at - 10 <= now) {
  353. valid = false;
  354. } else {
  355. valid = true;
  356. }
  357. } else if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  358. valid = true;
  359. } else {
  360. vnode->cb_s_break = vnode->cb_interest->server->cb_s_break;
  361. vnode->cb_v_break = vnode->volume->cb_v_break;
  362. valid = false;
  363. }
  364. read_sequnlock_excl(&vnode->cb_lock);
  365. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  366. clear_nlink(&vnode->vfs_inode);
  367. if (valid)
  368. goto valid;
  369. down_write(&vnode->validate_lock);
  370. /* if the promise has expired, we need to check the server again to get
  371. * a new promise - note that if the (parent) directory's metadata was
  372. * changed then the security may be different and we may no longer have
  373. * access */
  374. if (!test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
  375. _debug("not promised");
  376. ret = afs_fetch_status(vnode, key, false);
  377. if (ret < 0) {
  378. if (ret == -ENOENT) {
  379. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  380. ret = -ESTALE;
  381. }
  382. goto error_unlock;
  383. }
  384. _debug("new promise [fl=%lx]", vnode->flags);
  385. }
  386. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  387. _debug("file already deleted");
  388. ret = -ESTALE;
  389. goto error_unlock;
  390. }
  391. /* if the vnode's data version number changed then its contents are
  392. * different */
  393. if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
  394. afs_zap_data(vnode);
  395. up_write(&vnode->validate_lock);
  396. valid:
  397. _leave(" = 0");
  398. return 0;
  399. error_unlock:
  400. up_write(&vnode->validate_lock);
  401. _leave(" = %d", ret);
  402. return ret;
  403. }
  404. /*
  405. * read the attributes of an inode
  406. */
  407. int afs_getattr(const struct path *path, struct kstat *stat,
  408. u32 request_mask, unsigned int query_flags)
  409. {
  410. struct inode *inode = d_inode(path->dentry);
  411. struct afs_vnode *vnode = AFS_FS_I(inode);
  412. int seq = 0;
  413. _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation);
  414. do {
  415. read_seqbegin_or_lock(&vnode->cb_lock, &seq);
  416. generic_fillattr(inode, stat);
  417. } while (need_seqretry(&vnode->cb_lock, seq));
  418. done_seqretry(&vnode->cb_lock, seq);
  419. return 0;
  420. }
  421. /*
  422. * discard an AFS inode
  423. */
  424. int afs_drop_inode(struct inode *inode)
  425. {
  426. _enter("");
  427. if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags))
  428. return generic_delete_inode(inode);
  429. else
  430. return generic_drop_inode(inode);
  431. }
  432. /*
  433. * clear an AFS inode
  434. */
  435. void afs_evict_inode(struct inode *inode)
  436. {
  437. struct afs_vnode *vnode;
  438. vnode = AFS_FS_I(inode);
  439. _enter("{%llx:%llu.%d}",
  440. vnode->fid.vid,
  441. vnode->fid.vnode,
  442. vnode->fid.unique);
  443. _debug("CLEAR INODE %p", inode);
  444. ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
  445. truncate_inode_pages_final(&inode->i_data);
  446. clear_inode(inode);
  447. if (vnode->cb_interest) {
  448. afs_put_cb_interest(afs_i2net(inode), vnode->cb_interest);
  449. vnode->cb_interest = NULL;
  450. }
  451. while (!list_empty(&vnode->wb_keys)) {
  452. struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next,
  453. struct afs_wb_key, vnode_link);
  454. list_del(&wbk->vnode_link);
  455. afs_put_wb_key(wbk);
  456. }
  457. #ifdef CONFIG_AFS_FSCACHE
  458. {
  459. struct afs_vnode_cache_aux aux;
  460. aux.data_version = vnode->status.data_version;
  461. fscache_relinquish_cookie(vnode->cache, &aux,
  462. test_bit(AFS_VNODE_DELETED, &vnode->flags));
  463. vnode->cache = NULL;
  464. }
  465. #endif
  466. afs_put_permits(rcu_access_pointer(vnode->permit_cache));
  467. _leave("");
  468. }
  469. /*
  470. * set the attributes of an inode
  471. */
  472. int afs_setattr(struct dentry *dentry, struct iattr *attr)
  473. {
  474. struct afs_fs_cursor fc;
  475. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  476. struct key *key;
  477. int ret;
  478. _enter("{%llx:%llu},{n=%pd},%x",
  479. vnode->fid.vid, vnode->fid.vnode, dentry,
  480. attr->ia_valid);
  481. if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
  482. ATTR_MTIME))) {
  483. _leave(" = 0 [unsupported]");
  484. return 0;
  485. }
  486. /* flush any dirty data outstanding on a regular file */
  487. if (S_ISREG(vnode->vfs_inode.i_mode))
  488. filemap_write_and_wait(vnode->vfs_inode.i_mapping);
  489. if (attr->ia_valid & ATTR_FILE) {
  490. key = afs_file_key(attr->ia_file);
  491. } else {
  492. key = afs_request_key(vnode->volume->cell);
  493. if (IS_ERR(key)) {
  494. ret = PTR_ERR(key);
  495. goto error;
  496. }
  497. }
  498. ret = -ERESTARTSYS;
  499. if (afs_begin_vnode_operation(&fc, vnode, key)) {
  500. while (afs_select_fileserver(&fc)) {
  501. fc.cb_break = afs_calc_vnode_cb_break(vnode);
  502. afs_fs_setattr(&fc, attr);
  503. }
  504. afs_check_for_remote_deletion(&fc, fc.vnode);
  505. afs_vnode_commit_status(&fc, vnode, fc.cb_break);
  506. ret = afs_end_vnode_operation(&fc);
  507. }
  508. if (!(attr->ia_valid & ATTR_FILE))
  509. key_put(key);
  510. error:
  511. _leave(" = %d", ret);
  512. return ret;
  513. }