dir.c 26 KB

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