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