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/fs.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/ctype.h>
  17. #include <linux/sched.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. .lock = afs_lock,
  44. .llseek = generic_file_llseek,
  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 const 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. __func__, 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. __func__, 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. _enter("{%lu},%lu", dir->i_ino, index);
  170. page = read_cache_page(dir->i_mapping, index, afs_page_filler, key);
  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:%u},%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 >= AFSNAMEMAX) {
  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_CAST(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_CAST(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=%llu }",
  461. fid.vnode,
  462. fid.unique,
  463. dentry->d_inode->i_ino,
  464. (unsigned long long)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 uninitialized_var(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:%llu)",
  531. dentry->d_name.name, fid.unique,
  532. vnode->fid.unique,
  533. (unsigned long long)dentry->d_inode->i_version);
  534. spin_lock(&vnode->lock);
  535. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  536. spin_unlock(&vnode->lock);
  537. goto not_found;
  538. }
  539. goto out_valid;
  540. case -ENOENT:
  541. /* the filename is unknown */
  542. _debug("%s: dirent not found", dentry->d_name.name);
  543. if (dentry->d_inode)
  544. goto not_found;
  545. goto out_valid;
  546. default:
  547. _debug("failed to iterate dir %s: %d",
  548. parent->d_name.name, ret);
  549. goto out_bad;
  550. }
  551. out_valid:
  552. dentry->d_fsdata = dir_version;
  553. out_skip:
  554. dput(parent);
  555. key_put(key);
  556. _leave(" = 1 [valid]");
  557. return 1;
  558. /* the dirent, if it exists, now points to a different vnode */
  559. not_found:
  560. spin_lock(&dentry->d_lock);
  561. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  562. spin_unlock(&dentry->d_lock);
  563. out_bad:
  564. if (dentry->d_inode) {
  565. /* don't unhash if we have submounts */
  566. if (have_submounts(dentry))
  567. goto out_skip;
  568. }
  569. _debug("dropping dentry %s/%s",
  570. parent->d_name.name, dentry->d_name.name);
  571. shrink_dcache_parent(dentry);
  572. d_drop(dentry);
  573. dput(parent);
  574. key_put(key);
  575. _leave(" = 0 [bad]");
  576. return 0;
  577. }
  578. /*
  579. * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  580. * sleep)
  581. * - called from dput() when d_count is going to 0.
  582. * - return 1 to request dentry be unhashed, 0 otherwise
  583. */
  584. static int afs_d_delete(struct dentry *dentry)
  585. {
  586. _enter("%s", dentry->d_name.name);
  587. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  588. goto zap;
  589. if (dentry->d_inode &&
  590. test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags))
  591. goto zap;
  592. _leave(" = 0 [keep]");
  593. return 0;
  594. zap:
  595. _leave(" = 1 [zap]");
  596. return 1;
  597. }
  598. /*
  599. * handle dentry release
  600. */
  601. static void afs_d_release(struct dentry *dentry)
  602. {
  603. _enter("%s", dentry->d_name.name);
  604. }
  605. /*
  606. * create a directory on an AFS filesystem
  607. */
  608. static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  609. {
  610. struct afs_file_status status;
  611. struct afs_callback cb;
  612. struct afs_server *server;
  613. struct afs_vnode *dvnode, *vnode;
  614. struct afs_fid fid;
  615. struct inode *inode;
  616. struct key *key;
  617. int ret;
  618. dvnode = AFS_FS_I(dir);
  619. _enter("{%x:%u},{%s},%o",
  620. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
  621. ret = -ENAMETOOLONG;
  622. if (dentry->d_name.len >= AFSNAMEMAX)
  623. goto error;
  624. key = afs_request_key(dvnode->volume->cell);
  625. if (IS_ERR(key)) {
  626. ret = PTR_ERR(key);
  627. goto error;
  628. }
  629. mode |= S_IFDIR;
  630. ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
  631. mode, &fid, &status, &cb, &server);
  632. if (ret < 0)
  633. goto mkdir_error;
  634. inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
  635. if (IS_ERR(inode)) {
  636. /* ENOMEM at a really inconvenient time - just abandon the new
  637. * directory on the server */
  638. ret = PTR_ERR(inode);
  639. goto iget_error;
  640. }
  641. /* apply the status report we've got for the new vnode */
  642. vnode = AFS_FS_I(inode);
  643. spin_lock(&vnode->lock);
  644. vnode->update_cnt++;
  645. spin_unlock(&vnode->lock);
  646. afs_vnode_finalise_status_update(vnode, server);
  647. afs_put_server(server);
  648. d_instantiate(dentry, inode);
  649. if (d_unhashed(dentry)) {
  650. _debug("not hashed");
  651. d_rehash(dentry);
  652. }
  653. key_put(key);
  654. _leave(" = 0");
  655. return 0;
  656. iget_error:
  657. afs_put_server(server);
  658. mkdir_error:
  659. key_put(key);
  660. error:
  661. d_drop(dentry);
  662. _leave(" = %d", ret);
  663. return ret;
  664. }
  665. /*
  666. * remove a directory from an AFS filesystem
  667. */
  668. static int afs_rmdir(struct inode *dir, struct dentry *dentry)
  669. {
  670. struct afs_vnode *dvnode, *vnode;
  671. struct key *key;
  672. int ret;
  673. dvnode = AFS_FS_I(dir);
  674. _enter("{%x:%u},{%s}",
  675. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
  676. ret = -ENAMETOOLONG;
  677. if (dentry->d_name.len >= AFSNAMEMAX)
  678. goto error;
  679. key = afs_request_key(dvnode->volume->cell);
  680. if (IS_ERR(key)) {
  681. ret = PTR_ERR(key);
  682. goto error;
  683. }
  684. ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
  685. if (ret < 0)
  686. goto rmdir_error;
  687. if (dentry->d_inode) {
  688. vnode = AFS_FS_I(dentry->d_inode);
  689. clear_nlink(&vnode->vfs_inode);
  690. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  691. afs_discard_callback_on_delete(vnode);
  692. }
  693. key_put(key);
  694. _leave(" = 0");
  695. return 0;
  696. rmdir_error:
  697. key_put(key);
  698. error:
  699. _leave(" = %d", ret);
  700. return ret;
  701. }
  702. /*
  703. * remove a file from an AFS filesystem
  704. */
  705. static int afs_unlink(struct inode *dir, struct dentry *dentry)
  706. {
  707. struct afs_vnode *dvnode, *vnode;
  708. struct key *key;
  709. int ret;
  710. dvnode = AFS_FS_I(dir);
  711. _enter("{%x:%u},{%s}",
  712. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
  713. ret = -ENAMETOOLONG;
  714. if (dentry->d_name.len >= AFSNAMEMAX)
  715. goto error;
  716. key = afs_request_key(dvnode->volume->cell);
  717. if (IS_ERR(key)) {
  718. ret = PTR_ERR(key);
  719. goto error;
  720. }
  721. if (dentry->d_inode) {
  722. vnode = AFS_FS_I(dentry->d_inode);
  723. /* make sure we have a callback promise on the victim */
  724. ret = afs_validate(vnode, key);
  725. if (ret < 0)
  726. goto error;
  727. }
  728. ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
  729. if (ret < 0)
  730. goto remove_error;
  731. if (dentry->d_inode) {
  732. /* if the file wasn't deleted due to excess hard links, the
  733. * fileserver will break the callback promise on the file - if
  734. * it had one - before it returns to us, and if it was deleted,
  735. * it won't
  736. *
  737. * however, if we didn't have a callback promise outstanding,
  738. * or it was outstanding on a different server, then it won't
  739. * break it either...
  740. */
  741. vnode = AFS_FS_I(dentry->d_inode);
  742. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  743. _debug("AFS_VNODE_DELETED");
  744. if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
  745. _debug("AFS_VNODE_CB_BROKEN");
  746. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  747. ret = afs_validate(vnode, key);
  748. _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
  749. }
  750. key_put(key);
  751. _leave(" = 0");
  752. return 0;
  753. remove_error:
  754. key_put(key);
  755. error:
  756. _leave(" = %d", ret);
  757. return ret;
  758. }
  759. /*
  760. * create a regular file on an AFS filesystem
  761. */
  762. static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
  763. struct nameidata *nd)
  764. {
  765. struct afs_file_status status;
  766. struct afs_callback cb;
  767. struct afs_server *server;
  768. struct afs_vnode *dvnode, *vnode;
  769. struct afs_fid fid;
  770. struct inode *inode;
  771. struct key *key;
  772. int ret;
  773. dvnode = AFS_FS_I(dir);
  774. _enter("{%x:%u},{%s},%o,",
  775. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
  776. ret = -ENAMETOOLONG;
  777. if (dentry->d_name.len >= AFSNAMEMAX)
  778. goto error;
  779. key = afs_request_key(dvnode->volume->cell);
  780. if (IS_ERR(key)) {
  781. ret = PTR_ERR(key);
  782. goto error;
  783. }
  784. mode |= S_IFREG;
  785. ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
  786. mode, &fid, &status, &cb, &server);
  787. if (ret < 0)
  788. goto create_error;
  789. inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
  790. if (IS_ERR(inode)) {
  791. /* ENOMEM at a really inconvenient time - just abandon the new
  792. * directory on the server */
  793. ret = PTR_ERR(inode);
  794. goto iget_error;
  795. }
  796. /* apply the status report we've got for the new vnode */
  797. vnode = AFS_FS_I(inode);
  798. spin_lock(&vnode->lock);
  799. vnode->update_cnt++;
  800. spin_unlock(&vnode->lock);
  801. afs_vnode_finalise_status_update(vnode, server);
  802. afs_put_server(server);
  803. d_instantiate(dentry, inode);
  804. if (d_unhashed(dentry)) {
  805. _debug("not hashed");
  806. d_rehash(dentry);
  807. }
  808. key_put(key);
  809. _leave(" = 0");
  810. return 0;
  811. iget_error:
  812. afs_put_server(server);
  813. create_error:
  814. key_put(key);
  815. error:
  816. d_drop(dentry);
  817. _leave(" = %d", ret);
  818. return ret;
  819. }
  820. /*
  821. * create a hard link between files in an AFS filesystem
  822. */
  823. static int afs_link(struct dentry *from, struct inode *dir,
  824. struct dentry *dentry)
  825. {
  826. struct afs_vnode *dvnode, *vnode;
  827. struct key *key;
  828. int ret;
  829. vnode = AFS_FS_I(from->d_inode);
  830. dvnode = AFS_FS_I(dir);
  831. _enter("{%x:%u},{%x:%u},{%s}",
  832. vnode->fid.vid, vnode->fid.vnode,
  833. dvnode->fid.vid, dvnode->fid.vnode,
  834. dentry->d_name.name);
  835. ret = -ENAMETOOLONG;
  836. if (dentry->d_name.len >= AFSNAMEMAX)
  837. goto error;
  838. key = afs_request_key(dvnode->volume->cell);
  839. if (IS_ERR(key)) {
  840. ret = PTR_ERR(key);
  841. goto error;
  842. }
  843. ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
  844. if (ret < 0)
  845. goto link_error;
  846. atomic_inc(&vnode->vfs_inode.i_count);
  847. d_instantiate(dentry, &vnode->vfs_inode);
  848. key_put(key);
  849. _leave(" = 0");
  850. return 0;
  851. link_error:
  852. key_put(key);
  853. error:
  854. d_drop(dentry);
  855. _leave(" = %d", ret);
  856. return ret;
  857. }
  858. /*
  859. * create a symlink in an AFS filesystem
  860. */
  861. static int afs_symlink(struct inode *dir, struct dentry *dentry,
  862. const char *content)
  863. {
  864. struct afs_file_status status;
  865. struct afs_server *server;
  866. struct afs_vnode *dvnode, *vnode;
  867. struct afs_fid fid;
  868. struct inode *inode;
  869. struct key *key;
  870. int ret;
  871. dvnode = AFS_FS_I(dir);
  872. _enter("{%x:%u},{%s},%s",
  873. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
  874. content);
  875. ret = -ENAMETOOLONG;
  876. if (dentry->d_name.len >= AFSNAMEMAX)
  877. goto error;
  878. ret = -EINVAL;
  879. if (strlen(content) >= AFSPATHMAX)
  880. goto error;
  881. key = afs_request_key(dvnode->volume->cell);
  882. if (IS_ERR(key)) {
  883. ret = PTR_ERR(key);
  884. goto error;
  885. }
  886. ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
  887. &fid, &status, &server);
  888. if (ret < 0)
  889. goto create_error;
  890. inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
  891. if (IS_ERR(inode)) {
  892. /* ENOMEM at a really inconvenient time - just abandon the new
  893. * directory on the server */
  894. ret = PTR_ERR(inode);
  895. goto iget_error;
  896. }
  897. /* apply the status report we've got for the new vnode */
  898. vnode = AFS_FS_I(inode);
  899. spin_lock(&vnode->lock);
  900. vnode->update_cnt++;
  901. spin_unlock(&vnode->lock);
  902. afs_vnode_finalise_status_update(vnode, server);
  903. afs_put_server(server);
  904. d_instantiate(dentry, inode);
  905. if (d_unhashed(dentry)) {
  906. _debug("not hashed");
  907. d_rehash(dentry);
  908. }
  909. key_put(key);
  910. _leave(" = 0");
  911. return 0;
  912. iget_error:
  913. afs_put_server(server);
  914. create_error:
  915. key_put(key);
  916. error:
  917. d_drop(dentry);
  918. _leave(" = %d", ret);
  919. return ret;
  920. }
  921. /*
  922. * rename a file in an AFS filesystem and/or move it between directories
  923. */
  924. static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
  925. struct inode *new_dir, struct dentry *new_dentry)
  926. {
  927. struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
  928. struct key *key;
  929. int ret;
  930. vnode = AFS_FS_I(old_dentry->d_inode);
  931. orig_dvnode = AFS_FS_I(old_dir);
  932. new_dvnode = AFS_FS_I(new_dir);
  933. _enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
  934. orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
  935. vnode->fid.vid, vnode->fid.vnode,
  936. new_dvnode->fid.vid, new_dvnode->fid.vnode,
  937. new_dentry->d_name.name);
  938. ret = -ENAMETOOLONG;
  939. if (new_dentry->d_name.len >= AFSNAMEMAX)
  940. goto error;
  941. key = afs_request_key(orig_dvnode->volume->cell);
  942. if (IS_ERR(key)) {
  943. ret = PTR_ERR(key);
  944. goto error;
  945. }
  946. ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
  947. old_dentry->d_name.name,
  948. new_dentry->d_name.name);
  949. if (ret < 0)
  950. goto rename_error;
  951. key_put(key);
  952. _leave(" = 0");
  953. return 0;
  954. rename_error:
  955. key_put(key);
  956. error:
  957. d_drop(new_dentry);
  958. _leave(" = %d", ret);
  959. return ret;
  960. }