dir.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /* dir.c: AFS filesystem directory handling
  2. *
  3. * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/fs.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/smp_lock.h>
  19. #include "vnode.h"
  20. #include "volume.h"
  21. #include <rxrpc/call.h>
  22. #include "super.h"
  23. #include "internal.h"
  24. static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
  25. struct nameidata *nd);
  26. static int afs_dir_open(struct inode *inode, struct file *file);
  27. static int afs_dir_readdir(struct file *file, void *dirent, filldir_t filldir);
  28. static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
  29. static int afs_d_delete(struct dentry *dentry);
  30. static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
  31. loff_t fpos, u64 ino, unsigned dtype);
  32. const struct file_operations afs_dir_file_operations = {
  33. .open = afs_dir_open,
  34. .readdir = afs_dir_readdir,
  35. };
  36. struct inode_operations afs_dir_inode_operations = {
  37. .lookup = afs_dir_lookup,
  38. .getattr = afs_inode_getattr,
  39. #if 0 /* TODO */
  40. .create = afs_dir_create,
  41. .link = afs_dir_link,
  42. .unlink = afs_dir_unlink,
  43. .symlink = afs_dir_symlink,
  44. .mkdir = afs_dir_mkdir,
  45. .rmdir = afs_dir_rmdir,
  46. .mknod = afs_dir_mknod,
  47. .rename = afs_dir_rename,
  48. #endif
  49. };
  50. static struct dentry_operations afs_fs_dentry_operations = {
  51. .d_revalidate = afs_d_revalidate,
  52. .d_delete = afs_d_delete,
  53. };
  54. #define AFS_DIR_HASHTBL_SIZE 128
  55. #define AFS_DIR_DIRENT_SIZE 32
  56. #define AFS_DIRENT_PER_BLOCK 64
  57. union afs_dirent {
  58. struct {
  59. uint8_t valid;
  60. uint8_t unused[1];
  61. __be16 hash_next;
  62. __be32 vnode;
  63. __be32 unique;
  64. uint8_t name[16];
  65. uint8_t overflow[4]; /* if any char of the name (inc
  66. * NUL) reaches here, consume
  67. * the next dirent too */
  68. } u;
  69. uint8_t extended_name[32];
  70. };
  71. /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
  72. struct afs_dir_pagehdr {
  73. __be16 npages;
  74. __be16 magic;
  75. #define AFS_DIR_MAGIC htons(1234)
  76. uint8_t nentries;
  77. uint8_t bitmap[8];
  78. uint8_t pad[19];
  79. };
  80. /* directory block layout */
  81. union afs_dir_block {
  82. struct afs_dir_pagehdr pagehdr;
  83. struct {
  84. struct afs_dir_pagehdr pagehdr;
  85. uint8_t alloc_ctrs[128];
  86. /* dir hash table */
  87. uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
  88. } hdr;
  89. union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
  90. };
  91. /* layout on a linux VM page */
  92. struct afs_dir_page {
  93. union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
  94. };
  95. struct afs_dir_lookup_cookie {
  96. struct afs_fid fid;
  97. const char *name;
  98. size_t nlen;
  99. int found;
  100. };
  101. /*****************************************************************************/
  102. /*
  103. * check that a directory page is valid
  104. */
  105. static inline void afs_dir_check_page(struct inode *dir, struct page *page)
  106. {
  107. struct afs_dir_page *dbuf;
  108. loff_t latter;
  109. int tmp, qty;
  110. #if 0
  111. /* check the page count */
  112. qty = desc.size / sizeof(dbuf->blocks[0]);
  113. if (qty == 0)
  114. goto error;
  115. if (page->index==0 && qty!=ntohs(dbuf->blocks[0].pagehdr.npages)) {
  116. printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
  117. __FUNCTION__,dir->i_ino,qty,ntohs(dbuf->blocks[0].pagehdr.npages));
  118. goto error;
  119. }
  120. #endif
  121. /* determine how many magic numbers there should be in this page */
  122. latter = dir->i_size - page_offset(page);
  123. if (latter >= PAGE_SIZE)
  124. qty = PAGE_SIZE;
  125. else
  126. qty = latter;
  127. qty /= sizeof(union afs_dir_block);
  128. /* check them */
  129. dbuf = page_address(page);
  130. for (tmp = 0; tmp < qty; tmp++) {
  131. if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
  132. printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
  133. __FUNCTION__, dir->i_ino, tmp, qty,
  134. ntohs(dbuf->blocks[tmp].pagehdr.magic));
  135. goto error;
  136. }
  137. }
  138. SetPageChecked(page);
  139. return;
  140. error:
  141. SetPageChecked(page);
  142. SetPageError(page);
  143. } /* end afs_dir_check_page() */
  144. /*****************************************************************************/
  145. /*
  146. * discard a page cached in the pagecache
  147. */
  148. static inline void afs_dir_put_page(struct page *page)
  149. {
  150. kunmap(page);
  151. page_cache_release(page);
  152. } /* end afs_dir_put_page() */
  153. /*****************************************************************************/
  154. /*
  155. * get a page into the pagecache
  156. */
  157. static struct page *afs_dir_get_page(struct inode *dir, unsigned long index)
  158. {
  159. struct page *page;
  160. _enter("{%lu},%lu", dir->i_ino, index);
  161. page = read_mapping_page(dir->i_mapping, index, NULL);
  162. if (!IS_ERR(page)) {
  163. wait_on_page_locked(page);
  164. kmap(page);
  165. if (!PageUptodate(page))
  166. goto fail;
  167. if (!PageChecked(page))
  168. afs_dir_check_page(dir, page);
  169. if (PageError(page))
  170. goto fail;
  171. }
  172. return page;
  173. fail:
  174. afs_dir_put_page(page);
  175. return ERR_PTR(-EIO);
  176. } /* end afs_dir_get_page() */
  177. /*****************************************************************************/
  178. /*
  179. * open an AFS directory file
  180. */
  181. static int afs_dir_open(struct inode *inode, struct file *file)
  182. {
  183. _enter("{%lu}", inode->i_ino);
  184. BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
  185. BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
  186. if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED)
  187. return -ENOENT;
  188. _leave(" = 0");
  189. return 0;
  190. } /* end afs_dir_open() */
  191. /*****************************************************************************/
  192. /*
  193. * deal with one block in an AFS directory
  194. */
  195. static int afs_dir_iterate_block(unsigned *fpos,
  196. union afs_dir_block *block,
  197. unsigned blkoff,
  198. void *cookie,
  199. filldir_t filldir)
  200. {
  201. union afs_dirent *dire;
  202. unsigned offset, next, curr;
  203. size_t nlen;
  204. int tmp, ret;
  205. _enter("%u,%x,%p,,",*fpos,blkoff,block);
  206. curr = (*fpos - blkoff) / sizeof(union afs_dirent);
  207. /* walk through the block, an entry at a time */
  208. for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
  209. offset < AFS_DIRENT_PER_BLOCK;
  210. offset = next
  211. ) {
  212. next = offset + 1;
  213. /* skip entries marked unused in the bitmap */
  214. if (!(block->pagehdr.bitmap[offset / 8] &
  215. (1 << (offset % 8)))) {
  216. _debug("ENT[%Zu.%u]: unused\n",
  217. blkoff / sizeof(union afs_dir_block), offset);
  218. if (offset >= curr)
  219. *fpos = blkoff +
  220. next * sizeof(union afs_dirent);
  221. continue;
  222. }
  223. /* got a valid entry */
  224. dire = &block->dirents[offset];
  225. nlen = strnlen(dire->u.name,
  226. sizeof(*block) -
  227. offset * sizeof(union afs_dirent));
  228. _debug("ENT[%Zu.%u]: %s %Zu \"%s\"\n",
  229. blkoff / sizeof(union afs_dir_block), offset,
  230. (offset < curr ? "skip" : "fill"),
  231. nlen, dire->u.name);
  232. /* work out where the next possible entry is */
  233. for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
  234. if (next >= AFS_DIRENT_PER_BLOCK) {
  235. _debug("ENT[%Zu.%u]:"
  236. " %u travelled beyond end dir block"
  237. " (len %u/%Zu)\n",
  238. blkoff / sizeof(union afs_dir_block),
  239. offset, next, tmp, nlen);
  240. return -EIO;
  241. }
  242. if (!(block->pagehdr.bitmap[next / 8] &
  243. (1 << (next % 8)))) {
  244. _debug("ENT[%Zu.%u]:"
  245. " %u unmarked extension (len %u/%Zu)\n",
  246. blkoff / sizeof(union afs_dir_block),
  247. offset, next, tmp, nlen);
  248. return -EIO;
  249. }
  250. _debug("ENT[%Zu.%u]: ext %u/%Zu\n",
  251. blkoff / sizeof(union afs_dir_block),
  252. next, tmp, nlen);
  253. next++;
  254. }
  255. /* skip if starts before the current position */
  256. if (offset < curr)
  257. continue;
  258. /* found the next entry */
  259. ret = filldir(cookie,
  260. dire->u.name,
  261. nlen,
  262. blkoff + offset * sizeof(union afs_dirent),
  263. ntohl(dire->u.vnode),
  264. filldir == afs_dir_lookup_filldir ?
  265. ntohl(dire->u.unique) : DT_UNKNOWN);
  266. if (ret < 0) {
  267. _leave(" = 0 [full]");
  268. return 0;
  269. }
  270. *fpos = blkoff + next * sizeof(union afs_dirent);
  271. }
  272. _leave(" = 1 [more]");
  273. return 1;
  274. } /* end afs_dir_iterate_block() */
  275. /*****************************************************************************/
  276. /*
  277. * read an AFS directory
  278. */
  279. static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
  280. filldir_t filldir)
  281. {
  282. union afs_dir_block *dblock;
  283. struct afs_dir_page *dbuf;
  284. struct page *page;
  285. unsigned blkoff, limit;
  286. int ret;
  287. _enter("{%lu},%u,,", dir->i_ino, *fpos);
  288. if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
  289. _leave(" = -ESTALE");
  290. return -ESTALE;
  291. }
  292. /* round the file position up to the next entry boundary */
  293. *fpos += sizeof(union afs_dirent) - 1;
  294. *fpos &= ~(sizeof(union afs_dirent) - 1);
  295. /* walk through the blocks in sequence */
  296. ret = 0;
  297. while (*fpos < dir->i_size) {
  298. blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
  299. /* fetch the appropriate page from the directory */
  300. page = afs_dir_get_page(dir, blkoff / PAGE_SIZE);
  301. if (IS_ERR(page)) {
  302. ret = PTR_ERR(page);
  303. break;
  304. }
  305. limit = blkoff & ~(PAGE_SIZE - 1);
  306. dbuf = page_address(page);
  307. /* deal with the individual blocks stashed on this page */
  308. do {
  309. dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
  310. sizeof(union afs_dir_block)];
  311. ret = afs_dir_iterate_block(fpos, dblock, blkoff,
  312. cookie, filldir);
  313. if (ret != 1) {
  314. afs_dir_put_page(page);
  315. goto out;
  316. }
  317. blkoff += sizeof(union afs_dir_block);
  318. } while (*fpos < dir->i_size && blkoff < limit);
  319. afs_dir_put_page(page);
  320. ret = 0;
  321. }
  322. out:
  323. _leave(" = %d", ret);
  324. return ret;
  325. } /* end afs_dir_iterate() */
  326. /*****************************************************************************/
  327. /*
  328. * read an AFS directory
  329. */
  330. static int afs_dir_readdir(struct file *file, void *cookie, filldir_t filldir)
  331. {
  332. unsigned fpos;
  333. int ret;
  334. _enter("{%Ld,{%lu}}", file->f_pos, file->f_dentry->d_inode->i_ino);
  335. fpos = file->f_pos;
  336. ret = afs_dir_iterate(file->f_dentry->d_inode, &fpos, cookie, filldir);
  337. file->f_pos = fpos;
  338. _leave(" = %d", ret);
  339. return ret;
  340. } /* end afs_dir_readdir() */
  341. /*****************************************************************************/
  342. /*
  343. * search the directory for a name
  344. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  345. * uniquifier through dtype
  346. */
  347. static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
  348. loff_t fpos, u64 ino, unsigned dtype)
  349. {
  350. struct afs_dir_lookup_cookie *cookie = _cookie;
  351. _enter("{%s,%Zu},%s,%u,,%lu,%u",
  352. cookie->name, cookie->nlen, name, nlen, ino, dtype);
  353. if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
  354. _leave(" = 0 [no]");
  355. return 0;
  356. }
  357. cookie->fid.vnode = ino;
  358. cookie->fid.unique = dtype;
  359. cookie->found = 1;
  360. _leave(" = -1 [found]");
  361. return -1;
  362. } /* end afs_dir_lookup_filldir() */
  363. /*****************************************************************************/
  364. /*
  365. * look up an entry in a directory
  366. */
  367. static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
  368. struct nameidata *nd)
  369. {
  370. struct afs_dir_lookup_cookie cookie;
  371. struct afs_super_info *as;
  372. struct afs_vnode *vnode;
  373. struct inode *inode;
  374. unsigned fpos;
  375. int ret;
  376. _enter("{%lu},%p{%s}", dir->i_ino, dentry, dentry->d_name.name);
  377. /* insanity checks first */
  378. BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
  379. BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
  380. if (dentry->d_name.len > 255) {
  381. _leave(" = -ENAMETOOLONG");
  382. return ERR_PTR(-ENAMETOOLONG);
  383. }
  384. vnode = AFS_FS_I(dir);
  385. if (vnode->flags & AFS_VNODE_DELETED) {
  386. _leave(" = -ESTALE");
  387. return ERR_PTR(-ESTALE);
  388. }
  389. as = dir->i_sb->s_fs_info;
  390. /* search the directory */
  391. cookie.name = dentry->d_name.name;
  392. cookie.nlen = dentry->d_name.len;
  393. cookie.fid.vid = as->volume->vid;
  394. cookie.found = 0;
  395. fpos = 0;
  396. ret = afs_dir_iterate(dir, &fpos, &cookie, afs_dir_lookup_filldir);
  397. if (ret < 0) {
  398. _leave(" = %d", ret);
  399. return ERR_PTR(ret);
  400. }
  401. ret = -ENOENT;
  402. if (!cookie.found) {
  403. _leave(" = %d", ret);
  404. return ERR_PTR(ret);
  405. }
  406. /* instantiate the dentry */
  407. ret = afs_iget(dir->i_sb, &cookie.fid, &inode);
  408. if (ret < 0) {
  409. _leave(" = %d", ret);
  410. return ERR_PTR(ret);
  411. }
  412. dentry->d_op = &afs_fs_dentry_operations;
  413. dentry->d_fsdata = (void *) (unsigned long) vnode->status.version;
  414. d_add(dentry, inode);
  415. _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
  416. cookie.fid.vnode,
  417. cookie.fid.unique,
  418. dentry->d_inode->i_ino,
  419. dentry->d_inode->i_version);
  420. return NULL;
  421. } /* end afs_dir_lookup() */
  422. /*****************************************************************************/
  423. /*
  424. * check that a dentry lookup hit has found a valid entry
  425. * - NOTE! the hit can be a negative hit too, so we can't assume we have an
  426. * inode
  427. * (derived from nfs_lookup_revalidate)
  428. */
  429. static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
  430. {
  431. struct afs_dir_lookup_cookie cookie;
  432. struct dentry *parent;
  433. struct inode *inode, *dir;
  434. unsigned fpos;
  435. int ret;
  436. _enter("{sb=%p n=%s},", dentry->d_sb, dentry->d_name.name);
  437. /* lock down the parent dentry so we can peer at it */
  438. parent = dget_parent(dentry->d_parent);
  439. dir = parent->d_inode;
  440. inode = dentry->d_inode;
  441. /* handle a negative dentry */
  442. if (!inode)
  443. goto out_bad;
  444. /* handle a bad inode */
  445. if (is_bad_inode(inode)) {
  446. printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
  447. dentry->d_parent->d_name.name, dentry->d_name.name);
  448. goto out_bad;
  449. }
  450. /* force a full look up if the parent directory changed since last the
  451. * server was consulted
  452. * - otherwise this inode must still exist, even if the inode details
  453. * themselves have changed
  454. */
  455. if (AFS_FS_I(dir)->flags & AFS_VNODE_CHANGED)
  456. afs_vnode_fetch_status(AFS_FS_I(dir));
  457. if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
  458. _debug("%s: parent dir deleted", dentry->d_name.name);
  459. goto out_bad;
  460. }
  461. if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED) {
  462. _debug("%s: file already deleted", dentry->d_name.name);
  463. goto out_bad;
  464. }
  465. if ((unsigned long) dentry->d_fsdata !=
  466. (unsigned long) AFS_FS_I(dir)->status.version) {
  467. _debug("%s: parent changed %lu -> %u",
  468. dentry->d_name.name,
  469. (unsigned long) dentry->d_fsdata,
  470. (unsigned) AFS_FS_I(dir)->status.version);
  471. /* search the directory for this vnode */
  472. cookie.name = dentry->d_name.name;
  473. cookie.nlen = dentry->d_name.len;
  474. cookie.fid.vid = AFS_FS_I(inode)->volume->vid;
  475. cookie.found = 0;
  476. fpos = 0;
  477. ret = afs_dir_iterate(dir, &fpos, &cookie,
  478. afs_dir_lookup_filldir);
  479. if (ret < 0) {
  480. _debug("failed to iterate dir %s: %d",
  481. parent->d_name.name, ret);
  482. goto out_bad;
  483. }
  484. if (!cookie.found) {
  485. _debug("%s: dirent not found", dentry->d_name.name);
  486. goto not_found;
  487. }
  488. /* if the vnode ID has changed, then the dirent points to a
  489. * different file */
  490. if (cookie.fid.vnode != AFS_FS_I(inode)->fid.vnode) {
  491. _debug("%s: dirent changed", dentry->d_name.name);
  492. goto not_found;
  493. }
  494. /* if the vnode ID uniqifier has changed, then the file has
  495. * been deleted */
  496. if (cookie.fid.unique != AFS_FS_I(inode)->fid.unique) {
  497. _debug("%s: file deleted (uq %u -> %u I:%lu)",
  498. dentry->d_name.name,
  499. cookie.fid.unique,
  500. AFS_FS_I(inode)->fid.unique,
  501. inode->i_version);
  502. spin_lock(&AFS_FS_I(inode)->lock);
  503. AFS_FS_I(inode)->flags |= AFS_VNODE_DELETED;
  504. spin_unlock(&AFS_FS_I(inode)->lock);
  505. invalidate_remote_inode(inode);
  506. goto out_bad;
  507. }
  508. dentry->d_fsdata =
  509. (void *) (unsigned long) AFS_FS_I(dir)->status.version;
  510. }
  511. out_valid:
  512. dput(parent);
  513. _leave(" = 1 [valid]");
  514. return 1;
  515. /* the dirent, if it exists, now points to a different vnode */
  516. not_found:
  517. spin_lock(&dentry->d_lock);
  518. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  519. spin_unlock(&dentry->d_lock);
  520. out_bad:
  521. if (inode) {
  522. /* don't unhash if we have submounts */
  523. if (have_submounts(dentry))
  524. goto out_valid;
  525. }
  526. shrink_dcache_parent(dentry);
  527. _debug("dropping dentry %s/%s",
  528. dentry->d_parent->d_name.name, dentry->d_name.name);
  529. d_drop(dentry);
  530. dput(parent);
  531. _leave(" = 0 [bad]");
  532. return 0;
  533. } /* end afs_d_revalidate() */
  534. /*****************************************************************************/
  535. /*
  536. * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  537. * sleep)
  538. * - called from dput() when d_count is going to 0.
  539. * - return 1 to request dentry be unhashed, 0 otherwise
  540. */
  541. static int afs_d_delete(struct dentry *dentry)
  542. {
  543. _enter("%s", dentry->d_name.name);
  544. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  545. goto zap;
  546. if (dentry->d_inode) {
  547. if (AFS_FS_I(dentry->d_inode)->flags & AFS_VNODE_DELETED)
  548. goto zap;
  549. }
  550. _leave(" = 0 [keep]");
  551. return 0;
  552. zap:
  553. _leave(" = 1 [zap]");
  554. return 1;
  555. } /* end afs_d_delete() */