dir.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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/ctype.h>
  18. #include "internal.h"
  19. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  20. struct nameidata *nd);
  21. static int afs_dir_open(struct inode *inode, struct file *file);
  22. static int afs_readdir(struct file *file, void *dirent, filldir_t filldir);
  23. static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
  24. static int afs_d_delete(struct dentry *dentry);
  25. static void afs_d_release(struct dentry *dentry);
  26. static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
  27. loff_t fpos, u64 ino, unsigned dtype);
  28. static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
  29. struct nameidata *nd);
  30. static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  31. static int afs_rmdir(struct inode *dir, struct dentry *dentry);
  32. static int afs_unlink(struct inode *dir, struct dentry *dentry);
  33. static int afs_link(struct dentry *from, struct inode *dir,
  34. struct dentry *dentry);
  35. static int afs_symlink(struct inode *dir, struct dentry *dentry,
  36. const char *content);
  37. static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
  38. struct inode *new_dir, struct dentry *new_dentry);
  39. const struct file_operations afs_dir_file_operations = {
  40. .open = afs_dir_open,
  41. .release = afs_release,
  42. .readdir = afs_readdir,
  43. };
  44. const struct inode_operations afs_dir_inode_operations = {
  45. .create = afs_create,
  46. .lookup = afs_lookup,
  47. .link = afs_link,
  48. .unlink = afs_unlink,
  49. .symlink = afs_symlink,
  50. .mkdir = afs_mkdir,
  51. .rmdir = afs_rmdir,
  52. .rename = afs_rename,
  53. .permission = afs_permission,
  54. .getattr = afs_inode_getattr,
  55. };
  56. static struct dentry_operations afs_fs_dentry_operations = {
  57. .d_revalidate = afs_d_revalidate,
  58. .d_delete = afs_d_delete,
  59. .d_release = afs_d_release,
  60. };
  61. #define AFS_DIR_HASHTBL_SIZE 128
  62. #define AFS_DIR_DIRENT_SIZE 32
  63. #define AFS_DIRENT_PER_BLOCK 64
  64. union afs_dirent {
  65. struct {
  66. uint8_t valid;
  67. uint8_t unused[1];
  68. __be16 hash_next;
  69. __be32 vnode;
  70. __be32 unique;
  71. uint8_t name[16];
  72. uint8_t overflow[4]; /* if any char of the name (inc
  73. * NUL) reaches here, consume
  74. * the next dirent too */
  75. } u;
  76. uint8_t extended_name[32];
  77. };
  78. /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
  79. struct afs_dir_pagehdr {
  80. __be16 npages;
  81. __be16 magic;
  82. #define AFS_DIR_MAGIC htons(1234)
  83. uint8_t nentries;
  84. uint8_t bitmap[8];
  85. uint8_t pad[19];
  86. };
  87. /* directory block layout */
  88. union afs_dir_block {
  89. struct afs_dir_pagehdr pagehdr;
  90. struct {
  91. struct afs_dir_pagehdr pagehdr;
  92. uint8_t alloc_ctrs[128];
  93. /* dir hash table */
  94. uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
  95. } hdr;
  96. union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
  97. };
  98. /* layout on a linux VM page */
  99. struct afs_dir_page {
  100. union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
  101. };
  102. struct afs_lookup_cookie {
  103. struct afs_fid fid;
  104. const char *name;
  105. size_t nlen;
  106. int found;
  107. };
  108. /*
  109. * check that a directory page is valid
  110. */
  111. static inline void afs_dir_check_page(struct inode *dir, struct page *page)
  112. {
  113. struct afs_dir_page *dbuf;
  114. loff_t latter;
  115. int tmp, qty;
  116. #if 0
  117. /* check the page count */
  118. qty = desc.size / sizeof(dbuf->blocks[0]);
  119. if (qty == 0)
  120. goto error;
  121. if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
  122. printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
  123. __FUNCTION__, dir->i_ino, qty,
  124. ntohs(dbuf->blocks[0].pagehdr.npages));
  125. goto error;
  126. }
  127. #endif
  128. /* determine how many magic numbers there should be in this page */
  129. latter = dir->i_size - page_offset(page);
  130. if (latter >= PAGE_SIZE)
  131. qty = PAGE_SIZE;
  132. else
  133. qty = latter;
  134. qty /= sizeof(union afs_dir_block);
  135. /* check them */
  136. dbuf = page_address(page);
  137. for (tmp = 0; tmp < qty; tmp++) {
  138. if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
  139. printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
  140. __FUNCTION__, dir->i_ino, tmp, qty,
  141. ntohs(dbuf->blocks[tmp].pagehdr.magic));
  142. goto error;
  143. }
  144. }
  145. SetPageChecked(page);
  146. return;
  147. error:
  148. SetPageChecked(page);
  149. SetPageError(page);
  150. }
  151. /*
  152. * discard a page cached in the pagecache
  153. */
  154. static inline void afs_dir_put_page(struct page *page)
  155. {
  156. kunmap(page);
  157. page_cache_release(page);
  158. }
  159. /*
  160. * get a page into the pagecache
  161. */
  162. static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
  163. struct key *key)
  164. {
  165. struct page *page;
  166. struct file file = {
  167. .private_data = key,
  168. };
  169. _enter("{%lu},%lu", dir->i_ino, index);
  170. page = read_mapping_page(dir->i_mapping, index, &file);
  171. if (!IS_ERR(page)) {
  172. kmap(page);
  173. if (!PageChecked(page))
  174. afs_dir_check_page(dir, page);
  175. if (PageError(page))
  176. goto fail;
  177. }
  178. return page;
  179. fail:
  180. afs_dir_put_page(page);
  181. _leave(" = -EIO");
  182. return ERR_PTR(-EIO);
  183. }
  184. /*
  185. * open an AFS directory file
  186. */
  187. static int afs_dir_open(struct inode *inode, struct file *file)
  188. {
  189. _enter("{%lu}", inode->i_ino);
  190. BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
  191. BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
  192. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
  193. return -ENOENT;
  194. return afs_open(inode, file);
  195. }
  196. /*
  197. * deal with one block in an AFS directory
  198. */
  199. static int afs_dir_iterate_block(unsigned *fpos,
  200. union afs_dir_block *block,
  201. unsigned blkoff,
  202. void *cookie,
  203. filldir_t filldir)
  204. {
  205. union afs_dirent *dire;
  206. unsigned offset, next, curr;
  207. size_t nlen;
  208. int tmp, ret;
  209. _enter("%u,%x,%p,,",*fpos,blkoff,block);
  210. curr = (*fpos - blkoff) / sizeof(union afs_dirent);
  211. /* walk through the block, an entry at a time */
  212. for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
  213. offset < AFS_DIRENT_PER_BLOCK;
  214. offset = next
  215. ) {
  216. next = offset + 1;
  217. /* skip entries marked unused in the bitmap */
  218. if (!(block->pagehdr.bitmap[offset / 8] &
  219. (1 << (offset % 8)))) {
  220. _debug("ENT[%Zu.%u]: unused",
  221. blkoff / sizeof(union afs_dir_block), offset);
  222. if (offset >= curr)
  223. *fpos = blkoff +
  224. next * sizeof(union afs_dirent);
  225. continue;
  226. }
  227. /* got a valid entry */
  228. dire = &block->dirents[offset];
  229. nlen = strnlen(dire->u.name,
  230. sizeof(*block) -
  231. offset * sizeof(union afs_dirent));
  232. _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
  233. blkoff / sizeof(union afs_dir_block), offset,
  234. (offset < curr ? "skip" : "fill"),
  235. nlen, dire->u.name);
  236. /* work out where the next possible entry is */
  237. for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
  238. if (next >= AFS_DIRENT_PER_BLOCK) {
  239. _debug("ENT[%Zu.%u]:"
  240. " %u travelled beyond end dir block"
  241. " (len %u/%Zu)",
  242. blkoff / sizeof(union afs_dir_block),
  243. offset, next, tmp, nlen);
  244. return -EIO;
  245. }
  246. if (!(block->pagehdr.bitmap[next / 8] &
  247. (1 << (next % 8)))) {
  248. _debug("ENT[%Zu.%u]:"
  249. " %u unmarked extension (len %u/%Zu)",
  250. blkoff / sizeof(union afs_dir_block),
  251. offset, next, tmp, nlen);
  252. return -EIO;
  253. }
  254. _debug("ENT[%Zu.%u]: ext %u/%Zu",
  255. blkoff / sizeof(union afs_dir_block),
  256. next, tmp, nlen);
  257. next++;
  258. }
  259. /* skip if starts before the current position */
  260. if (offset < curr)
  261. continue;
  262. /* found the next entry */
  263. ret = filldir(cookie,
  264. dire->u.name,
  265. nlen,
  266. blkoff + offset * sizeof(union afs_dirent),
  267. ntohl(dire->u.vnode),
  268. filldir == afs_lookup_filldir ?
  269. ntohl(dire->u.unique) : DT_UNKNOWN);
  270. if (ret < 0) {
  271. _leave(" = 0 [full]");
  272. return 0;
  273. }
  274. *fpos = blkoff + next * sizeof(union afs_dirent);
  275. }
  276. _leave(" = 1 [more]");
  277. return 1;
  278. }
  279. /*
  280. * iterate through the data blob that lists the contents of an AFS directory
  281. */
  282. static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
  283. filldir_t filldir, struct key *key)
  284. {
  285. union afs_dir_block *dblock;
  286. struct afs_dir_page *dbuf;
  287. struct page *page;
  288. unsigned blkoff, limit;
  289. int ret;
  290. _enter("{%lu},%u,,", dir->i_ino, *fpos);
  291. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
  292. _leave(" = -ESTALE");
  293. return -ESTALE;
  294. }
  295. /* round the file position up to the next entry boundary */
  296. *fpos += sizeof(union afs_dirent) - 1;
  297. *fpos &= ~(sizeof(union afs_dirent) - 1);
  298. /* walk through the blocks in sequence */
  299. ret = 0;
  300. while (*fpos < dir->i_size) {
  301. blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
  302. /* fetch the appropriate page from the directory */
  303. page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
  304. if (IS_ERR(page)) {
  305. ret = PTR_ERR(page);
  306. break;
  307. }
  308. limit = blkoff & ~(PAGE_SIZE - 1);
  309. dbuf = page_address(page);
  310. /* deal with the individual blocks stashed on this page */
  311. do {
  312. dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
  313. sizeof(union afs_dir_block)];
  314. ret = afs_dir_iterate_block(fpos, dblock, blkoff,
  315. cookie, filldir);
  316. if (ret != 1) {
  317. afs_dir_put_page(page);
  318. goto out;
  319. }
  320. blkoff += sizeof(union afs_dir_block);
  321. } while (*fpos < dir->i_size && blkoff < limit);
  322. afs_dir_put_page(page);
  323. ret = 0;
  324. }
  325. out:
  326. _leave(" = %d", ret);
  327. return ret;
  328. }
  329. /*
  330. * read an AFS directory
  331. */
  332. static int afs_readdir(struct file *file, void *cookie, filldir_t filldir)
  333. {
  334. unsigned fpos;
  335. int ret;
  336. _enter("{%Ld,{%lu}}",
  337. file->f_pos, file->f_path.dentry->d_inode->i_ino);
  338. ASSERT(file->private_data != NULL);
  339. fpos = file->f_pos;
  340. ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos,
  341. cookie, filldir, file->private_data);
  342. file->f_pos = fpos;
  343. _leave(" = %d", ret);
  344. return ret;
  345. }
  346. /*
  347. * search the directory for a name
  348. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  349. * uniquifier through dtype
  350. */
  351. static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
  352. loff_t fpos, u64 ino, unsigned dtype)
  353. {
  354. struct afs_lookup_cookie *cookie = _cookie;
  355. _enter("{%s,%Zu},%s,%u,,%llu,%u",
  356. cookie->name, cookie->nlen, name, nlen,
  357. (unsigned long long) ino, dtype);
  358. /* insanity checks first */
  359. BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048);
  360. BUILD_BUG_ON(sizeof(union afs_dirent) != 32);
  361. if (cookie->nlen != nlen || memcmp(cookie->name, name, nlen) != 0) {
  362. _leave(" = 0 [no]");
  363. return 0;
  364. }
  365. cookie->fid.vnode = ino;
  366. cookie->fid.unique = dtype;
  367. cookie->found = 1;
  368. _leave(" = -1 [found]");
  369. return -1;
  370. }
  371. /*
  372. * do a lookup in a directory
  373. * - just returns the FID the dentry name maps to if found
  374. */
  375. static int afs_do_lookup(struct inode *dir, struct dentry *dentry,
  376. struct afs_fid *fid, struct key *key)
  377. {
  378. struct afs_lookup_cookie cookie;
  379. struct afs_super_info *as;
  380. unsigned fpos;
  381. int ret;
  382. _enter("{%lu},%p{%s},", dir->i_ino, dentry, dentry->d_name.name);
  383. as = dir->i_sb->s_fs_info;
  384. /* search the directory */
  385. cookie.name = dentry->d_name.name;
  386. cookie.nlen = dentry->d_name.len;
  387. cookie.fid.vid = as->volume->vid;
  388. cookie.found = 0;
  389. fpos = 0;
  390. ret = afs_dir_iterate(dir, &fpos, &cookie, afs_lookup_filldir,
  391. key);
  392. if (ret < 0) {
  393. _leave(" = %d [iter]", ret);
  394. return ret;
  395. }
  396. ret = -ENOENT;
  397. if (!cookie.found) {
  398. _leave(" = -ENOENT [not found]");
  399. return -ENOENT;
  400. }
  401. *fid = cookie.fid;
  402. _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
  403. return 0;
  404. }
  405. /*
  406. * look up an entry in a directory
  407. */
  408. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  409. struct nameidata *nd)
  410. {
  411. struct afs_vnode *vnode;
  412. struct afs_fid fid;
  413. struct inode *inode;
  414. struct key *key;
  415. int ret;
  416. vnode = AFS_FS_I(dir);
  417. _enter("{%x:%d},%p{%s},",
  418. vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
  419. ASSERTCMP(dentry->d_inode, ==, NULL);
  420. if (dentry->d_name.len > 255) {
  421. _leave(" = -ENAMETOOLONG");
  422. return ERR_PTR(-ENAMETOOLONG);
  423. }
  424. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  425. _leave(" = -ESTALE");
  426. return ERR_PTR(-ESTALE);
  427. }
  428. key = afs_request_key(vnode->volume->cell);
  429. if (IS_ERR(key)) {
  430. _leave(" = %ld [key]", PTR_ERR(key));
  431. return ERR_PTR(PTR_ERR(key));
  432. }
  433. ret = afs_validate(vnode, key);
  434. if (ret < 0) {
  435. key_put(key);
  436. _leave(" = %d [val]", ret);
  437. return ERR_PTR(ret);
  438. }
  439. ret = afs_do_lookup(dir, dentry, &fid, key);
  440. if (ret < 0) {
  441. key_put(key);
  442. if (ret == -ENOENT) {
  443. d_add(dentry, NULL);
  444. _leave(" = NULL [negative]");
  445. return NULL;
  446. }
  447. _leave(" = %d [do]", ret);
  448. return ERR_PTR(ret);
  449. }
  450. dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
  451. /* instantiate the dentry */
  452. inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
  453. key_put(key);
  454. if (IS_ERR(inode)) {
  455. _leave(" = %ld", PTR_ERR(inode));
  456. return ERR_PTR(PTR_ERR(inode));
  457. }
  458. dentry->d_op = &afs_fs_dentry_operations;
  459. d_add(dentry, inode);
  460. _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
  461. fid.vnode,
  462. fid.unique,
  463. dentry->d_inode->i_ino,
  464. dentry->d_inode->i_version);
  465. return NULL;
  466. }
  467. /*
  468. * check that a dentry lookup hit has found a valid entry
  469. * - NOTE! the hit can be a negative hit too, so we can't assume we have an
  470. * inode
  471. */
  472. static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
  473. {
  474. struct afs_vnode *vnode, *dir;
  475. struct afs_fid fid;
  476. struct dentry *parent;
  477. struct key *key;
  478. void *dir_version;
  479. int ret;
  480. vnode = AFS_FS_I(dentry->d_inode);
  481. if (dentry->d_inode)
  482. _enter("{v={%x:%u} n=%s fl=%lx},",
  483. vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
  484. vnode->flags);
  485. else
  486. _enter("{neg n=%s}", dentry->d_name.name);
  487. key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
  488. if (IS_ERR(key))
  489. key = NULL;
  490. /* lock down the parent dentry so we can peer at it */
  491. parent = dget_parent(dentry);
  492. if (!parent->d_inode)
  493. goto out_bad;
  494. dir = AFS_FS_I(parent->d_inode);
  495. /* validate the parent directory */
  496. if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
  497. afs_validate(dir, key);
  498. if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
  499. _debug("%s: parent dir deleted", dentry->d_name.name);
  500. goto out_bad;
  501. }
  502. dir_version = (void *) (unsigned long) dir->status.data_version;
  503. if (dentry->d_fsdata == dir_version)
  504. goto out_valid; /* the dir contents are unchanged */
  505. _debug("dir modified");
  506. /* search the directory for this vnode */
  507. ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
  508. switch (ret) {
  509. case 0:
  510. /* the filename maps to something */
  511. if (!dentry->d_inode)
  512. goto out_bad;
  513. if (is_bad_inode(dentry->d_inode)) {
  514. printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
  515. parent->d_name.name, dentry->d_name.name);
  516. goto out_bad;
  517. }
  518. /* if the vnode ID has changed, then the dirent points to a
  519. * different file */
  520. if (fid.vnode != vnode->fid.vnode) {
  521. _debug("%s: dirent changed [%u != %u]",
  522. dentry->d_name.name, fid.vnode,
  523. vnode->fid.vnode);
  524. goto not_found;
  525. }
  526. /* if the vnode ID uniqifier has changed, then the file has
  527. * been deleted and replaced, and the original vnode ID has
  528. * been reused */
  529. if (fid.unique != vnode->fid.unique) {
  530. _debug("%s: file deleted (uq %u -> %u I:%lu)",
  531. dentry->d_name.name, fid.unique,
  532. vnode->fid.unique, dentry->d_inode->i_version);
  533. spin_lock(&vnode->lock);
  534. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  535. spin_unlock(&vnode->lock);
  536. goto not_found;
  537. }
  538. goto out_valid;
  539. case -ENOENT:
  540. /* the filename is unknown */
  541. _debug("%s: dirent not found", dentry->d_name.name);
  542. if (dentry->d_inode)
  543. goto not_found;
  544. goto out_valid;
  545. default:
  546. _debug("failed to iterate dir %s: %d",
  547. parent->d_name.name, ret);
  548. goto out_bad;
  549. }
  550. out_valid:
  551. dentry->d_fsdata = dir_version;
  552. out_skip:
  553. dput(parent);
  554. key_put(key);
  555. _leave(" = 1 [valid]");
  556. return 1;
  557. /* the dirent, if it exists, now points to a different vnode */
  558. not_found:
  559. spin_lock(&dentry->d_lock);
  560. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  561. spin_unlock(&dentry->d_lock);
  562. out_bad:
  563. if (dentry->d_inode) {
  564. /* don't unhash if we have submounts */
  565. if (have_submounts(dentry))
  566. goto out_skip;
  567. }
  568. _debug("dropping dentry %s/%s",
  569. parent->d_name.name, dentry->d_name.name);
  570. shrink_dcache_parent(dentry);
  571. d_drop(dentry);
  572. dput(parent);
  573. key_put(key);
  574. _leave(" = 0 [bad]");
  575. return 0;
  576. }
  577. /*
  578. * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  579. * sleep)
  580. * - called from dput() when d_count is going to 0.
  581. * - return 1 to request dentry be unhashed, 0 otherwise
  582. */
  583. static int afs_d_delete(struct dentry *dentry)
  584. {
  585. _enter("%s", dentry->d_name.name);
  586. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  587. goto zap;
  588. if (dentry->d_inode &&
  589. test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags))
  590. goto zap;
  591. _leave(" = 0 [keep]");
  592. return 0;
  593. zap:
  594. _leave(" = 1 [zap]");
  595. return 1;
  596. }
  597. /*
  598. * handle dentry release
  599. */
  600. static void afs_d_release(struct dentry *dentry)
  601. {
  602. _enter("%s", dentry->d_name.name);
  603. }
  604. /*
  605. * create a directory on an AFS filesystem
  606. */
  607. static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  608. {
  609. struct afs_file_status status;
  610. struct afs_callback cb;
  611. struct afs_server *server;
  612. struct afs_vnode *dvnode, *vnode;
  613. struct afs_fid fid;
  614. struct inode *inode;
  615. struct key *key;
  616. int ret;
  617. dvnode = AFS_FS_I(dir);
  618. _enter("{%x:%d},{%s},%o",
  619. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
  620. ret = -ENAMETOOLONG;
  621. if (dentry->d_name.len > 255)
  622. goto error;
  623. key = afs_request_key(dvnode->volume->cell);
  624. if (IS_ERR(key)) {
  625. ret = PTR_ERR(key);
  626. goto error;
  627. }
  628. mode |= S_IFDIR;
  629. ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
  630. mode, &fid, &status, &cb, &server);
  631. if (ret < 0)
  632. goto mkdir_error;
  633. inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
  634. if (IS_ERR(inode)) {
  635. /* ENOMEM at a really inconvenient time - just abandon the new
  636. * directory on the server */
  637. ret = PTR_ERR(inode);
  638. goto iget_error;
  639. }
  640. /* apply the status report we've got for the new vnode */
  641. vnode = AFS_FS_I(inode);
  642. spin_lock(&vnode->lock);
  643. vnode->update_cnt++;
  644. spin_unlock(&vnode->lock);
  645. afs_vnode_finalise_status_update(vnode, server);
  646. afs_put_server(server);
  647. d_instantiate(dentry, inode);
  648. if (d_unhashed(dentry)) {
  649. _debug("not hashed");
  650. d_rehash(dentry);
  651. }
  652. key_put(key);
  653. _leave(" = 0");
  654. return 0;
  655. iget_error:
  656. afs_put_server(server);
  657. mkdir_error:
  658. key_put(key);
  659. error:
  660. d_drop(dentry);
  661. _leave(" = %d", ret);
  662. return ret;
  663. }
  664. /*
  665. * remove a directory from an AFS filesystem
  666. */
  667. static int afs_rmdir(struct inode *dir, struct dentry *dentry)
  668. {
  669. struct afs_vnode *dvnode, *vnode;
  670. struct key *key;
  671. int ret;
  672. dvnode = AFS_FS_I(dir);
  673. _enter("{%x:%d},{%s}",
  674. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
  675. ret = -ENAMETOOLONG;
  676. if (dentry->d_name.len > 255)
  677. goto error;
  678. key = afs_request_key(dvnode->volume->cell);
  679. if (IS_ERR(key)) {
  680. ret = PTR_ERR(key);
  681. goto error;
  682. }
  683. ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
  684. if (ret < 0)
  685. goto rmdir_error;
  686. if (dentry->d_inode) {
  687. vnode = AFS_FS_I(dentry->d_inode);
  688. clear_nlink(&vnode->vfs_inode);
  689. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  690. afs_discard_callback_on_delete(vnode);
  691. }
  692. key_put(key);
  693. _leave(" = 0");
  694. return 0;
  695. rmdir_error:
  696. key_put(key);
  697. error:
  698. _leave(" = %d", ret);
  699. return ret;
  700. }
  701. /*
  702. * remove a file from an AFS filesystem
  703. */
  704. static int afs_unlink(struct inode *dir, struct dentry *dentry)
  705. {
  706. struct afs_vnode *dvnode, *vnode;
  707. struct key *key;
  708. int ret;
  709. dvnode = AFS_FS_I(dir);
  710. _enter("{%x:%d},{%s}",
  711. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
  712. ret = -ENAMETOOLONG;
  713. if (dentry->d_name.len > 255)
  714. goto error;
  715. key = afs_request_key(dvnode->volume->cell);
  716. if (IS_ERR(key)) {
  717. ret = PTR_ERR(key);
  718. goto error;
  719. }
  720. if (dentry->d_inode) {
  721. vnode = AFS_FS_I(dentry->d_inode);
  722. /* make sure we have a callback promise on the victim */
  723. ret = afs_validate(vnode, key);
  724. if (ret < 0)
  725. goto error;
  726. }
  727. ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
  728. if (ret < 0)
  729. goto remove_error;
  730. if (dentry->d_inode) {
  731. /* if the file wasn't deleted due to excess hard links, the
  732. * fileserver will break the callback promise on the file - if
  733. * it had one - before it returns to us, and if it was deleted,
  734. * it won't
  735. *
  736. * however, if we didn't have a callback promise outstanding,
  737. * or it was outstanding on a different server, then it won't
  738. * break it either...
  739. */
  740. vnode = AFS_FS_I(dentry->d_inode);
  741. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  742. _debug("AFS_VNODE_DELETED");
  743. if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
  744. _debug("AFS_VNODE_CB_BROKEN");
  745. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  746. ret = afs_validate(vnode, key);
  747. _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
  748. }
  749. key_put(key);
  750. _leave(" = 0");
  751. return 0;
  752. remove_error:
  753. key_put(key);
  754. error:
  755. _leave(" = %d", ret);
  756. return ret;
  757. }
  758. /*
  759. * create a regular file on an AFS filesystem
  760. */
  761. static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
  762. struct nameidata *nd)
  763. {
  764. struct afs_file_status status;
  765. struct afs_callback cb;
  766. struct afs_server *server;
  767. struct afs_vnode *dvnode, *vnode;
  768. struct afs_fid fid;
  769. struct inode *inode;
  770. struct key *key;
  771. int ret;
  772. dvnode = AFS_FS_I(dir);
  773. _enter("{%x:%d},{%s},%o,",
  774. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
  775. ret = -ENAMETOOLONG;
  776. if (dentry->d_name.len > 255)
  777. goto error;
  778. key = afs_request_key(dvnode->volume->cell);
  779. if (IS_ERR(key)) {
  780. ret = PTR_ERR(key);
  781. goto error;
  782. }
  783. mode |= S_IFREG;
  784. ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
  785. mode, &fid, &status, &cb, &server);
  786. if (ret < 0)
  787. goto create_error;
  788. inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
  789. if (IS_ERR(inode)) {
  790. /* ENOMEM at a really inconvenient time - just abandon the new
  791. * directory on the server */
  792. ret = PTR_ERR(inode);
  793. goto iget_error;
  794. }
  795. /* apply the status report we've got for the new vnode */
  796. vnode = AFS_FS_I(inode);
  797. spin_lock(&vnode->lock);
  798. vnode->update_cnt++;
  799. spin_unlock(&vnode->lock);
  800. afs_vnode_finalise_status_update(vnode, server);
  801. afs_put_server(server);
  802. d_instantiate(dentry, inode);
  803. if (d_unhashed(dentry)) {
  804. _debug("not hashed");
  805. d_rehash(dentry);
  806. }
  807. key_put(key);
  808. _leave(" = 0");
  809. return 0;
  810. iget_error:
  811. afs_put_server(server);
  812. create_error:
  813. key_put(key);
  814. error:
  815. d_drop(dentry);
  816. _leave(" = %d", ret);
  817. return ret;
  818. }
  819. /*
  820. * create a hard link between files in an AFS filesystem
  821. */
  822. static int afs_link(struct dentry *from, struct inode *dir,
  823. struct dentry *dentry)
  824. {
  825. struct afs_vnode *dvnode, *vnode;
  826. struct key *key;
  827. int ret;
  828. vnode = AFS_FS_I(from->d_inode);
  829. dvnode = AFS_FS_I(dir);
  830. _enter("{%x:%d},{%x:%d},{%s}",
  831. vnode->fid.vid, vnode->fid.vnode,
  832. dvnode->fid.vid, dvnode->fid.vnode,
  833. dentry->d_name.name);
  834. ret = -ENAMETOOLONG;
  835. if (dentry->d_name.len > 255)
  836. goto error;
  837. key = afs_request_key(dvnode->volume->cell);
  838. if (IS_ERR(key)) {
  839. ret = PTR_ERR(key);
  840. goto error;
  841. }
  842. ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
  843. if (ret < 0)
  844. goto link_error;
  845. atomic_inc(&vnode->vfs_inode.i_count);
  846. d_instantiate(dentry, &vnode->vfs_inode);
  847. key_put(key);
  848. _leave(" = 0");
  849. return 0;
  850. link_error:
  851. key_put(key);
  852. error:
  853. d_drop(dentry);
  854. _leave(" = %d", ret);
  855. return ret;
  856. }
  857. /*
  858. * create a symlink in an AFS filesystem
  859. */
  860. static int afs_symlink(struct inode *dir, struct dentry *dentry,
  861. const char *content)
  862. {
  863. struct afs_file_status status;
  864. struct afs_server *server;
  865. struct afs_vnode *dvnode, *vnode;
  866. struct afs_fid fid;
  867. struct inode *inode;
  868. struct key *key;
  869. int ret;
  870. dvnode = AFS_FS_I(dir);
  871. _enter("{%x:%d},{%s},%s",
  872. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
  873. content);
  874. ret = -ENAMETOOLONG;
  875. if (dentry->d_name.len > 255)
  876. goto error;
  877. ret = -EINVAL;
  878. if (strlen(content) > 1023)
  879. goto error;
  880. key = afs_request_key(dvnode->volume->cell);
  881. if (IS_ERR(key)) {
  882. ret = PTR_ERR(key);
  883. goto error;
  884. }
  885. ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
  886. &fid, &status, &server);
  887. if (ret < 0)
  888. goto create_error;
  889. inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
  890. if (IS_ERR(inode)) {
  891. /* ENOMEM at a really inconvenient time - just abandon the new
  892. * directory on the server */
  893. ret = PTR_ERR(inode);
  894. goto iget_error;
  895. }
  896. /* apply the status report we've got for the new vnode */
  897. vnode = AFS_FS_I(inode);
  898. spin_lock(&vnode->lock);
  899. vnode->update_cnt++;
  900. spin_unlock(&vnode->lock);
  901. afs_vnode_finalise_status_update(vnode, server);
  902. afs_put_server(server);
  903. d_instantiate(dentry, inode);
  904. if (d_unhashed(dentry)) {
  905. _debug("not hashed");
  906. d_rehash(dentry);
  907. }
  908. key_put(key);
  909. _leave(" = 0");
  910. return 0;
  911. iget_error:
  912. afs_put_server(server);
  913. create_error:
  914. key_put(key);
  915. error:
  916. d_drop(dentry);
  917. _leave(" = %d", ret);
  918. return ret;
  919. }
  920. /*
  921. * rename a file in an AFS filesystem and/or move it between directories
  922. */
  923. static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
  924. struct inode *new_dir, struct dentry *new_dentry)
  925. {
  926. struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
  927. struct key *key;
  928. int ret;
  929. vnode = AFS_FS_I(old_dentry->d_inode);
  930. orig_dvnode = AFS_FS_I(old_dir);
  931. new_dvnode = AFS_FS_I(new_dir);
  932. _enter("{%x:%d},{%x:%d},{%x:%d},{%s}",
  933. orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
  934. vnode->fid.vid, vnode->fid.vnode,
  935. new_dvnode->fid.vid, new_dvnode->fid.vnode,
  936. new_dentry->d_name.name);
  937. ret = -ENAMETOOLONG;
  938. if (new_dentry->d_name.len > 255)
  939. goto error;
  940. key = afs_request_key(orig_dvnode->volume->cell);
  941. if (IS_ERR(key)) {
  942. ret = PTR_ERR(key);
  943. goto error;
  944. }
  945. ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
  946. old_dentry->d_name.name,
  947. new_dentry->d_name.name);
  948. if (ret < 0)
  949. goto rename_error;
  950. key_put(key);
  951. _leave(" = 0");
  952. return 0;
  953. rename_error:
  954. key_put(key);
  955. error:
  956. d_drop(new_dentry);
  957. _leave(" = %d", ret);
  958. return ret;
  959. }