dir.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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, ino_t 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_cache_page(dir->i_mapping,index,
  162. (filler_t *) dir->i_mapping->a_ops->readpage,
  163. NULL);
  164. if (!IS_ERR(page)) {
  165. wait_on_page_locked(page);
  166. kmap(page);
  167. if (!PageUptodate(page))
  168. goto fail;
  169. if (!PageChecked(page))
  170. afs_dir_check_page(dir, page);
  171. if (PageError(page))
  172. goto fail;
  173. }
  174. return page;
  175. fail:
  176. afs_dir_put_page(page);
  177. return ERR_PTR(-EIO);
  178. } /* end afs_dir_get_page() */
  179. /*****************************************************************************/
  180. /*
  181. * open an AFS directory file
  182. */
  183. static int afs_dir_open(struct inode *inode, struct file *file)
  184. {
  185. _enter("{%lu}", inode->i_ino);
  186. BUG_ON(sizeof(union afs_dir_block) != 2048);
  187. BUG_ON(sizeof(union afs_dirent) != 32);
  188. if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED)
  189. return -ENOENT;
  190. _leave(" = 0");
  191. return 0;
  192. } /* end afs_dir_open() */
  193. /*****************************************************************************/
  194. /*
  195. * deal with one block in an AFS directory
  196. */
  197. static int afs_dir_iterate_block(unsigned *fpos,
  198. union afs_dir_block *block,
  199. unsigned blkoff,
  200. void *cookie,
  201. filldir_t filldir)
  202. {
  203. union afs_dirent *dire;
  204. unsigned offset, next, curr;
  205. size_t nlen;
  206. int tmp, ret;
  207. _enter("%u,%x,%p,,",*fpos,blkoff,block);
  208. curr = (*fpos - blkoff) / sizeof(union afs_dirent);
  209. /* walk through the block, an entry at a time */
  210. for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
  211. offset < AFS_DIRENT_PER_BLOCK;
  212. offset = next
  213. ) {
  214. next = offset + 1;
  215. /* skip entries marked unused in the bitmap */
  216. if (!(block->pagehdr.bitmap[offset / 8] &
  217. (1 << (offset % 8)))) {
  218. _debug("ENT[%Zu.%u]: unused\n",
  219. blkoff / sizeof(union afs_dir_block), offset);
  220. if (offset >= curr)
  221. *fpos = blkoff +
  222. next * sizeof(union afs_dirent);
  223. continue;
  224. }
  225. /* got a valid entry */
  226. dire = &block->dirents[offset];
  227. nlen = strnlen(dire->u.name,
  228. sizeof(*block) -
  229. offset * sizeof(union afs_dirent));
  230. _debug("ENT[%Zu.%u]: %s %Zu \"%s\"\n",
  231. blkoff / sizeof(union afs_dir_block), offset,
  232. (offset < curr ? "skip" : "fill"),
  233. nlen, dire->u.name);
  234. /* work out where the next possible entry is */
  235. for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
  236. if (next >= AFS_DIRENT_PER_BLOCK) {
  237. _debug("ENT[%Zu.%u]:"
  238. " %u travelled beyond end dir block"
  239. " (len %u/%Zu)\n",
  240. blkoff / sizeof(union afs_dir_block),
  241. offset, next, tmp, nlen);
  242. return -EIO;
  243. }
  244. if (!(block->pagehdr.bitmap[next / 8] &
  245. (1 << (next % 8)))) {
  246. _debug("ENT[%Zu.%u]:"
  247. " %u unmarked extension (len %u/%Zu)\n",
  248. blkoff / sizeof(union afs_dir_block),
  249. offset, next, tmp, nlen);
  250. return -EIO;
  251. }
  252. _debug("ENT[%Zu.%u]: ext %u/%Zu\n",
  253. blkoff / sizeof(union afs_dir_block),
  254. next, tmp, nlen);
  255. next++;
  256. }
  257. /* skip if starts before the current position */
  258. if (offset < curr)
  259. continue;
  260. /* found the next entry */
  261. ret = filldir(cookie,
  262. dire->u.name,
  263. nlen,
  264. blkoff + offset * sizeof(union afs_dirent),
  265. ntohl(dire->u.vnode),
  266. filldir == afs_dir_lookup_filldir ?
  267. ntohl(dire->u.unique) : DT_UNKNOWN);
  268. if (ret < 0) {
  269. _leave(" = 0 [full]");
  270. return 0;
  271. }
  272. *fpos = blkoff + next * sizeof(union afs_dirent);
  273. }
  274. _leave(" = 1 [more]");
  275. return 1;
  276. } /* end afs_dir_iterate_block() */
  277. /*****************************************************************************/
  278. /*
  279. * read an AFS directory
  280. */
  281. static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
  282. filldir_t filldir)
  283. {
  284. union afs_dir_block *dblock;
  285. struct afs_dir_page *dbuf;
  286. struct page *page;
  287. unsigned blkoff, limit;
  288. int ret;
  289. _enter("{%lu},%u,,", dir->i_ino, *fpos);
  290. if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
  291. _leave(" = -ESTALE");
  292. return -ESTALE;
  293. }
  294. /* round the file position up to the next entry boundary */
  295. *fpos += sizeof(union afs_dirent) - 1;
  296. *fpos &= ~(sizeof(union afs_dirent) - 1);
  297. /* walk through the blocks in sequence */
  298. ret = 0;
  299. while (*fpos < dir->i_size) {
  300. blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
  301. /* fetch the appropriate page from the directory */
  302. page = afs_dir_get_page(dir, blkoff / PAGE_SIZE);
  303. if (IS_ERR(page)) {
  304. ret = PTR_ERR(page);
  305. break;
  306. }
  307. limit = blkoff & ~(PAGE_SIZE - 1);
  308. dbuf = page_address(page);
  309. /* deal with the individual blocks stashed on this page */
  310. do {
  311. dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
  312. sizeof(union afs_dir_block)];
  313. ret = afs_dir_iterate_block(fpos, dblock, blkoff,
  314. cookie, filldir);
  315. if (ret != 1) {
  316. afs_dir_put_page(page);
  317. goto out;
  318. }
  319. blkoff += sizeof(union afs_dir_block);
  320. } while (*fpos < dir->i_size && blkoff < limit);
  321. afs_dir_put_page(page);
  322. ret = 0;
  323. }
  324. out:
  325. _leave(" = %d", ret);
  326. return ret;
  327. } /* end afs_dir_iterate() */
  328. /*****************************************************************************/
  329. /*
  330. * read an AFS directory
  331. */
  332. static int afs_dir_readdir(struct file *file, void *cookie, filldir_t filldir)
  333. {
  334. unsigned fpos;
  335. int ret;
  336. _enter("{%Ld,{%lu}}", file->f_pos, file->f_dentry->d_inode->i_ino);
  337. fpos = file->f_pos;
  338. ret = afs_dir_iterate(file->f_dentry->d_inode, &fpos, cookie, filldir);
  339. file->f_pos = fpos;
  340. _leave(" = %d", ret);
  341. return ret;
  342. } /* end afs_dir_readdir() */
  343. /*****************************************************************************/
  344. /*
  345. * search the directory for a name
  346. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  347. * uniquifier through dtype
  348. */
  349. static int afs_dir_lookup_filldir(void *_cookie, const char *name, int nlen,
  350. loff_t fpos, ino_t ino, unsigned dtype)
  351. {
  352. struct afs_dir_lookup_cookie *cookie = _cookie;
  353. _enter("{%s,%Zu},%s,%u,,%lu,%u",
  354. cookie->name, cookie->nlen, name, nlen, ino, dtype);
  355. if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
  356. _leave(" = 0 [no]");
  357. return 0;
  358. }
  359. cookie->fid.vnode = ino;
  360. cookie->fid.unique = dtype;
  361. cookie->found = 1;
  362. _leave(" = -1 [found]");
  363. return -1;
  364. } /* end afs_dir_lookup_filldir() */
  365. /*****************************************************************************/
  366. /*
  367. * look up an entry in a directory
  368. */
  369. static struct dentry *afs_dir_lookup(struct inode *dir, struct dentry *dentry,
  370. struct nameidata *nd)
  371. {
  372. struct afs_dir_lookup_cookie cookie;
  373. struct afs_super_info *as;
  374. struct afs_vnode *vnode;
  375. struct inode *inode;
  376. unsigned fpos;
  377. int ret;
  378. _enter("{%lu},%p{%s}", dir->i_ino, dentry, dentry->d_name.name);
  379. /* insanity checks first */
  380. BUG_ON(sizeof(union afs_dir_block) != 2048);
  381. BUG_ON(sizeof(union afs_dirent) != 32);
  382. if (dentry->d_name.len > 255) {
  383. _leave(" = -ENAMETOOLONG");
  384. return ERR_PTR(-ENAMETOOLONG);
  385. }
  386. vnode = AFS_FS_I(dir);
  387. if (vnode->flags & AFS_VNODE_DELETED) {
  388. _leave(" = -ESTALE");
  389. return ERR_PTR(-ESTALE);
  390. }
  391. as = dir->i_sb->s_fs_info;
  392. /* search the directory */
  393. cookie.name = dentry->d_name.name;
  394. cookie.nlen = dentry->d_name.len;
  395. cookie.fid.vid = as->volume->vid;
  396. cookie.found = 0;
  397. fpos = 0;
  398. ret = afs_dir_iterate(dir, &fpos, &cookie, afs_dir_lookup_filldir);
  399. if (ret < 0) {
  400. _leave(" = %d", ret);
  401. return ERR_PTR(ret);
  402. }
  403. ret = -ENOENT;
  404. if (!cookie.found) {
  405. _leave(" = %d", ret);
  406. return ERR_PTR(ret);
  407. }
  408. /* instantiate the dentry */
  409. ret = afs_iget(dir->i_sb, &cookie.fid, &inode);
  410. if (ret < 0) {
  411. _leave(" = %d", ret);
  412. return ERR_PTR(ret);
  413. }
  414. dentry->d_op = &afs_fs_dentry_operations;
  415. dentry->d_fsdata = (void *) (unsigned long) vnode->status.version;
  416. d_add(dentry, inode);
  417. _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
  418. cookie.fid.vnode,
  419. cookie.fid.unique,
  420. dentry->d_inode->i_ino,
  421. dentry->d_inode->i_version);
  422. return NULL;
  423. } /* end afs_dir_lookup() */
  424. /*****************************************************************************/
  425. /*
  426. * check that a dentry lookup hit has found a valid entry
  427. * - NOTE! the hit can be a negative hit too, so we can't assume we have an
  428. * inode
  429. * (derived from nfs_lookup_revalidate)
  430. */
  431. static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
  432. {
  433. struct afs_dir_lookup_cookie cookie;
  434. struct dentry *parent;
  435. struct inode *inode, *dir;
  436. unsigned fpos;
  437. int ret;
  438. _enter("{sb=%p n=%s},", dentry->d_sb, dentry->d_name.name);
  439. /* lock down the parent dentry so we can peer at it */
  440. parent = dget_parent(dentry->d_parent);
  441. dir = parent->d_inode;
  442. inode = dentry->d_inode;
  443. /* handle a negative dentry */
  444. if (!inode)
  445. goto out_bad;
  446. /* handle a bad inode */
  447. if (is_bad_inode(inode)) {
  448. printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
  449. dentry->d_parent->d_name.name, dentry->d_name.name);
  450. goto out_bad;
  451. }
  452. /* force a full look up if the parent directory changed since last the
  453. * server was consulted
  454. * - otherwise this inode must still exist, even if the inode details
  455. * themselves have changed
  456. */
  457. if (AFS_FS_I(dir)->flags & AFS_VNODE_CHANGED)
  458. afs_vnode_fetch_status(AFS_FS_I(dir));
  459. if (AFS_FS_I(dir)->flags & AFS_VNODE_DELETED) {
  460. _debug("%s: parent dir deleted", dentry->d_name.name);
  461. goto out_bad;
  462. }
  463. if (AFS_FS_I(inode)->flags & AFS_VNODE_DELETED) {
  464. _debug("%s: file already deleted", dentry->d_name.name);
  465. goto out_bad;
  466. }
  467. if ((unsigned long) dentry->d_fsdata !=
  468. (unsigned long) AFS_FS_I(dir)->status.version) {
  469. _debug("%s: parent changed %lu -> %u",
  470. dentry->d_name.name,
  471. (unsigned long) dentry->d_fsdata,
  472. (unsigned) AFS_FS_I(dir)->status.version);
  473. /* search the directory for this vnode */
  474. cookie.name = dentry->d_name.name;
  475. cookie.nlen = dentry->d_name.len;
  476. cookie.fid.vid = AFS_FS_I(inode)->volume->vid;
  477. cookie.found = 0;
  478. fpos = 0;
  479. ret = afs_dir_iterate(dir, &fpos, &cookie,
  480. afs_dir_lookup_filldir);
  481. if (ret < 0) {
  482. _debug("failed to iterate dir %s: %d",
  483. parent->d_name.name, ret);
  484. goto out_bad;
  485. }
  486. if (!cookie.found) {
  487. _debug("%s: dirent not found", dentry->d_name.name);
  488. goto not_found;
  489. }
  490. /* if the vnode ID has changed, then the dirent points to a
  491. * different file */
  492. if (cookie.fid.vnode != AFS_FS_I(inode)->fid.vnode) {
  493. _debug("%s: dirent changed", dentry->d_name.name);
  494. goto not_found;
  495. }
  496. /* if the vnode ID uniqifier has changed, then the file has
  497. * been deleted */
  498. if (cookie.fid.unique != AFS_FS_I(inode)->fid.unique) {
  499. _debug("%s: file deleted (uq %u -> %u I:%lu)",
  500. dentry->d_name.name,
  501. cookie.fid.unique,
  502. AFS_FS_I(inode)->fid.unique,
  503. inode->i_version);
  504. spin_lock(&AFS_FS_I(inode)->lock);
  505. AFS_FS_I(inode)->flags |= AFS_VNODE_DELETED;
  506. spin_unlock(&AFS_FS_I(inode)->lock);
  507. invalidate_remote_inode(inode);
  508. goto out_bad;
  509. }
  510. dentry->d_fsdata =
  511. (void *) (unsigned long) AFS_FS_I(dir)->status.version;
  512. }
  513. out_valid:
  514. dput(parent);
  515. _leave(" = 1 [valid]");
  516. return 1;
  517. /* the dirent, if it exists, now points to a different vnode */
  518. not_found:
  519. spin_lock(&dentry->d_lock);
  520. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  521. spin_unlock(&dentry->d_lock);
  522. out_bad:
  523. if (inode) {
  524. /* don't unhash if we have submounts */
  525. if (have_submounts(dentry))
  526. goto out_valid;
  527. }
  528. shrink_dcache_parent(dentry);
  529. _debug("dropping dentry %s/%s",
  530. dentry->d_parent->d_name.name, dentry->d_name.name);
  531. d_drop(dentry);
  532. dput(parent);
  533. _leave(" = 0 [bad]");
  534. return 0;
  535. } /* end afs_d_revalidate() */
  536. /*****************************************************************************/
  537. /*
  538. * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  539. * sleep)
  540. * - called from dput() when d_count is going to 0.
  541. * - return 1 to request dentry be unhashed, 0 otherwise
  542. */
  543. static int afs_d_delete(struct dentry *dentry)
  544. {
  545. _enter("%s", dentry->d_name.name);
  546. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  547. goto zap;
  548. if (dentry->d_inode) {
  549. if (AFS_FS_I(dentry->d_inode)->flags & AFS_VNODE_DELETED)
  550. goto zap;
  551. }
  552. _leave(" = 0 [keep]");
  553. return 0;
  554. zap:
  555. _leave(" = 1 [zap]");
  556. return 1;
  557. } /* end afs_d_delete() */