dir.c 26 KB

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