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