dir.c 26 KB

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