dir.c 26 KB

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