dir.c 26 KB

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