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 <linux/sched.h>
  19. #include "internal.h"
  20. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  21. struct nameidata *nd);
  22. static int afs_dir_open(struct inode *inode, struct file *file);
  23. static int afs_readdir(struct file *file, void *dirent, filldir_t filldir);
  24. static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
  25. static int afs_d_delete(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, int mode,
  30. struct nameidata *nd);
  31. static int afs_mkdir(struct inode *dir, struct dentry *dentry, int 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. .readdir = afs_readdir,
  44. };
  45. const struct inode_operations afs_dir_inode_operations = {
  46. .create = afs_create,
  47. .lookup = afs_lookup,
  48. .link = afs_link,
  49. .unlink = afs_unlink,
  50. .symlink = afs_symlink,
  51. .mkdir = afs_mkdir,
  52. .rmdir = afs_rmdir,
  53. .rename = afs_rename,
  54. .permission = afs_permission,
  55. .getattr = afs_getattr,
  56. .setattr = afs_setattr,
  57. };
  58. static struct dentry_operations afs_fs_dentry_operations = {
  59. .d_revalidate = afs_d_revalidate,
  60. .d_delete = afs_d_delete,
  61. .d_release = afs_d_release,
  62. };
  63. #define AFS_DIR_HASHTBL_SIZE 128
  64. #define AFS_DIR_DIRENT_SIZE 32
  65. #define AFS_DIRENT_PER_BLOCK 64
  66. union afs_dirent {
  67. struct {
  68. uint8_t valid;
  69. uint8_t unused[1];
  70. __be16 hash_next;
  71. __be32 vnode;
  72. __be32 unique;
  73. uint8_t name[16];
  74. uint8_t overflow[4]; /* if any char of the name (inc
  75. * NUL) reaches here, consume
  76. * the next dirent too */
  77. } u;
  78. uint8_t extended_name[32];
  79. };
  80. /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
  81. struct afs_dir_pagehdr {
  82. __be16 npages;
  83. __be16 magic;
  84. #define AFS_DIR_MAGIC htons(1234)
  85. uint8_t nentries;
  86. uint8_t bitmap[8];
  87. uint8_t pad[19];
  88. };
  89. /* directory block layout */
  90. union afs_dir_block {
  91. struct afs_dir_pagehdr pagehdr;
  92. struct {
  93. struct afs_dir_pagehdr pagehdr;
  94. uint8_t alloc_ctrs[128];
  95. /* dir hash table */
  96. uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
  97. } hdr;
  98. union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
  99. };
  100. /* layout on a linux VM page */
  101. struct afs_dir_page {
  102. union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
  103. };
  104. struct afs_lookup_cookie {
  105. struct afs_fid fid;
  106. const char *name;
  107. size_t nlen;
  108. int found;
  109. };
  110. /*
  111. * check that a directory page is valid
  112. */
  113. static inline void afs_dir_check_page(struct inode *dir, struct page *page)
  114. {
  115. struct afs_dir_page *dbuf;
  116. loff_t latter;
  117. int tmp, qty;
  118. #if 0
  119. /* check the page count */
  120. qty = desc.size / sizeof(dbuf->blocks[0]);
  121. if (qty == 0)
  122. goto error;
  123. if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
  124. printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
  125. __FUNCTION__, dir->i_ino, qty,
  126. ntohs(dbuf->blocks[0].pagehdr.npages));
  127. goto error;
  128. }
  129. #endif
  130. /* determine how many magic numbers there should be in this page */
  131. latter = dir->i_size - page_offset(page);
  132. if (latter >= PAGE_SIZE)
  133. qty = PAGE_SIZE;
  134. else
  135. qty = latter;
  136. qty /= sizeof(union afs_dir_block);
  137. /* check them */
  138. dbuf = page_address(page);
  139. for (tmp = 0; tmp < qty; tmp++) {
  140. if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
  141. printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
  142. __FUNCTION__, dir->i_ino, tmp, qty,
  143. ntohs(dbuf->blocks[tmp].pagehdr.magic));
  144. goto error;
  145. }
  146. }
  147. SetPageChecked(page);
  148. return;
  149. error:
  150. SetPageChecked(page);
  151. SetPageError(page);
  152. }
  153. /*
  154. * discard a page cached in the pagecache
  155. */
  156. static inline void afs_dir_put_page(struct page *page)
  157. {
  158. kunmap(page);
  159. page_cache_release(page);
  160. }
  161. /*
  162. * get a page into the pagecache
  163. */
  164. static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
  165. struct key *key)
  166. {
  167. struct page *page;
  168. struct file file = {
  169. .private_data = key,
  170. };
  171. _enter("{%lu},%lu", dir->i_ino, index);
  172. page = read_mapping_page(dir->i_mapping, index, &file);
  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(unsigned *fpos,
  202. union afs_dir_block *block,
  203. unsigned blkoff,
  204. void *cookie,
  205. filldir_t filldir)
  206. {
  207. union afs_dirent *dire;
  208. unsigned offset, next, curr;
  209. size_t nlen;
  210. int tmp, ret;
  211. _enter("%u,%x,%p,,",*fpos,blkoff,block);
  212. curr = (*fpos - blkoff) / sizeof(union afs_dirent);
  213. /* walk through the block, an entry at a time */
  214. for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries;
  215. offset < AFS_DIRENT_PER_BLOCK;
  216. offset = next
  217. ) {
  218. next = offset + 1;
  219. /* skip entries marked unused in the bitmap */
  220. if (!(block->pagehdr.bitmap[offset / 8] &
  221. (1 << (offset % 8)))) {
  222. _debug("ENT[%Zu.%u]: unused",
  223. blkoff / sizeof(union afs_dir_block), offset);
  224. if (offset >= curr)
  225. *fpos = blkoff +
  226. next * sizeof(union afs_dirent);
  227. continue;
  228. }
  229. /* got a valid entry */
  230. dire = &block->dirents[offset];
  231. nlen = strnlen(dire->u.name,
  232. sizeof(*block) -
  233. offset * sizeof(union afs_dirent));
  234. _debug("ENT[%Zu.%u]: %s %Zu \"%s\"",
  235. blkoff / sizeof(union afs_dir_block), offset,
  236. (offset < curr ? "skip" : "fill"),
  237. nlen, dire->u.name);
  238. /* work out where the next possible entry is */
  239. for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) {
  240. if (next >= AFS_DIRENT_PER_BLOCK) {
  241. _debug("ENT[%Zu.%u]:"
  242. " %u travelled beyond end dir block"
  243. " (len %u/%Zu)",
  244. blkoff / sizeof(union afs_dir_block),
  245. offset, next, tmp, nlen);
  246. return -EIO;
  247. }
  248. if (!(block->pagehdr.bitmap[next / 8] &
  249. (1 << (next % 8)))) {
  250. _debug("ENT[%Zu.%u]:"
  251. " %u unmarked extension (len %u/%Zu)",
  252. blkoff / sizeof(union afs_dir_block),
  253. offset, next, tmp, nlen);
  254. return -EIO;
  255. }
  256. _debug("ENT[%Zu.%u]: ext %u/%Zu",
  257. blkoff / sizeof(union afs_dir_block),
  258. next, tmp, nlen);
  259. next++;
  260. }
  261. /* skip if starts before the current position */
  262. if (offset < curr)
  263. continue;
  264. /* found the next entry */
  265. ret = filldir(cookie,
  266. dire->u.name,
  267. nlen,
  268. blkoff + offset * sizeof(union afs_dirent),
  269. ntohl(dire->u.vnode),
  270. filldir == afs_lookup_filldir ?
  271. ntohl(dire->u.unique) : DT_UNKNOWN);
  272. if (ret < 0) {
  273. _leave(" = 0 [full]");
  274. return 0;
  275. }
  276. *fpos = blkoff + next * sizeof(union afs_dirent);
  277. }
  278. _leave(" = 1 [more]");
  279. return 1;
  280. }
  281. /*
  282. * iterate through the data blob that lists the contents of an AFS directory
  283. */
  284. static int afs_dir_iterate(struct inode *dir, unsigned *fpos, void *cookie,
  285. filldir_t filldir, struct key *key)
  286. {
  287. union afs_dir_block *dblock;
  288. struct afs_dir_page *dbuf;
  289. struct page *page;
  290. unsigned blkoff, limit;
  291. int ret;
  292. _enter("{%lu},%u,,", dir->i_ino, *fpos);
  293. if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
  294. _leave(" = -ESTALE");
  295. return -ESTALE;
  296. }
  297. /* round the file position up to the next entry boundary */
  298. *fpos += sizeof(union afs_dirent) - 1;
  299. *fpos &= ~(sizeof(union afs_dirent) - 1);
  300. /* walk through the blocks in sequence */
  301. ret = 0;
  302. while (*fpos < dir->i_size) {
  303. blkoff = *fpos & ~(sizeof(union afs_dir_block) - 1);
  304. /* fetch the appropriate page from the directory */
  305. page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key);
  306. if (IS_ERR(page)) {
  307. ret = PTR_ERR(page);
  308. break;
  309. }
  310. limit = blkoff & ~(PAGE_SIZE - 1);
  311. dbuf = page_address(page);
  312. /* deal with the individual blocks stashed on this page */
  313. do {
  314. dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
  315. sizeof(union afs_dir_block)];
  316. ret = afs_dir_iterate_block(fpos, dblock, blkoff,
  317. cookie, filldir);
  318. if (ret != 1) {
  319. afs_dir_put_page(page);
  320. goto out;
  321. }
  322. blkoff += sizeof(union afs_dir_block);
  323. } while (*fpos < dir->i_size && blkoff < limit);
  324. afs_dir_put_page(page);
  325. ret = 0;
  326. }
  327. out:
  328. _leave(" = %d", ret);
  329. return ret;
  330. }
  331. /*
  332. * read an AFS directory
  333. */
  334. static int afs_readdir(struct file *file, void *cookie, filldir_t filldir)
  335. {
  336. unsigned fpos;
  337. int ret;
  338. _enter("{%Ld,{%lu}}",
  339. file->f_pos, file->f_path.dentry->d_inode->i_ino);
  340. ASSERT(file->private_data != NULL);
  341. fpos = file->f_pos;
  342. ret = afs_dir_iterate(file->f_path.dentry->d_inode, &fpos,
  343. cookie, filldir, file->private_data);
  344. file->f_pos = fpos;
  345. _leave(" = %d", ret);
  346. return ret;
  347. }
  348. /*
  349. * search the directory for a name
  350. * - if afs_dir_iterate_block() spots this function, it'll pass the FID
  351. * uniquifier through dtype
  352. */
  353. static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
  354. loff_t fpos, u64 ino, unsigned dtype)
  355. {
  356. struct afs_lookup_cookie *cookie = _cookie;
  357. _enter("{%s,%Zu},%s,%u,,%llu,%u",
  358. cookie->name, cookie->nlen, name, nlen,
  359. (unsigned long long) 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:%u},%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 >= AFSNAMEMAX) {
  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:%u},{%s},%o",
  621. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
  622. ret = -ENAMETOOLONG;
  623. if (dentry->d_name.len >= AFSNAMEMAX)
  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:%u},{%s}",
  676. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
  677. ret = -ENAMETOOLONG;
  678. if (dentry->d_name.len >= AFSNAMEMAX)
  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:%u},{%s}",
  713. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
  714. ret = -ENAMETOOLONG;
  715. if (dentry->d_name.len >= AFSNAMEMAX)
  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:%u},{%s},%o,",
  776. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
  777. ret = -ENAMETOOLONG;
  778. if (dentry->d_name.len >= AFSNAMEMAX)
  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:%u},{%x:%u},{%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 >= AFSNAMEMAX)
  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:%u},{%s},%s",
  874. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
  875. content);
  876. ret = -ENAMETOOLONG;
  877. if (dentry->d_name.len >= AFSNAMEMAX)
  878. goto error;
  879. ret = -EINVAL;
  880. if (strlen(content) >= AFSPATHMAX)
  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:%u},{%x:%u},{%x:%u},{%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 >= AFSNAMEMAX)
  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. }