dir.c 27 KB

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