dir.c 16 KB

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