dir.c 27 KB

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