dir.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /* dir.c: AFS filesystem directory handling
  2. *
  3. * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/ctype.h>
  18. #include "internal.h"
  19. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  20. struct nameidata *nd);
  21. static int afs_dir_open(struct inode *inode, struct file *file);
  22. static int afs_readdir(struct file *file, void *dirent, filldir_t filldir);
  23. static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd);
  24. static int afs_d_delete(struct dentry *dentry);
  25. static void afs_d_release(struct dentry *dentry);
  26. static int afs_lookup_filldir(void *_cookie, const char *name, int nlen,
  27. loff_t fpos, u64 ino, unsigned dtype);
  28. static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
  29. struct nameidata *nd);
  30. static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
  31. static int afs_rmdir(struct inode *dir, struct dentry *dentry);
  32. static int afs_unlink(struct inode *dir, struct dentry *dentry);
  33. static int afs_link(struct dentry *from, struct inode *dir,
  34. struct dentry *dentry);
  35. static int afs_symlink(struct inode *dir, struct dentry *dentry,
  36. const char *content);
  37. static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
  38. struct inode *new_dir, struct dentry *new_dentry);
  39. const struct file_operations afs_dir_file_operations = {
  40. .open = afs_dir_open,
  41. .release = afs_release,
  42. .readdir = afs_readdir,
  43. };
  44. const struct inode_operations afs_dir_inode_operations = {
  45. .create = afs_create,
  46. .lookup = afs_lookup,
  47. .link = afs_link,
  48. .unlink = afs_unlink,
  49. .symlink = afs_symlink,
  50. .mkdir = afs_mkdir,
  51. .rmdir = afs_rmdir,
  52. .rename = afs_rename,
  53. .permission = afs_permission,
  54. .getattr = afs_getattr,
  55. .setattr = afs_setattr,
  56. };
  57. static struct dentry_operations afs_fs_dentry_operations = {
  58. .d_revalidate = afs_d_revalidate,
  59. .d_delete = afs_d_delete,
  60. .d_release = afs_d_release,
  61. };
  62. #define AFS_DIR_HASHTBL_SIZE 128
  63. #define AFS_DIR_DIRENT_SIZE 32
  64. #define AFS_DIRENT_PER_BLOCK 64
  65. union afs_dirent {
  66. struct {
  67. uint8_t valid;
  68. uint8_t unused[1];
  69. __be16 hash_next;
  70. __be32 vnode;
  71. __be32 unique;
  72. uint8_t name[16];
  73. uint8_t overflow[4]; /* if any char of the name (inc
  74. * NUL) reaches here, consume
  75. * the next dirent too */
  76. } u;
  77. uint8_t extended_name[32];
  78. };
  79. /* AFS directory page header (one at the beginning of every 2048-byte chunk) */
  80. struct afs_dir_pagehdr {
  81. __be16 npages;
  82. __be16 magic;
  83. #define AFS_DIR_MAGIC htons(1234)
  84. uint8_t nentries;
  85. uint8_t bitmap[8];
  86. uint8_t pad[19];
  87. };
  88. /* directory block layout */
  89. union afs_dir_block {
  90. struct afs_dir_pagehdr pagehdr;
  91. struct {
  92. struct afs_dir_pagehdr pagehdr;
  93. uint8_t alloc_ctrs[128];
  94. /* dir hash table */
  95. uint16_t hashtable[AFS_DIR_HASHTBL_SIZE];
  96. } hdr;
  97. union afs_dirent dirents[AFS_DIRENT_PER_BLOCK];
  98. };
  99. /* layout on a linux VM page */
  100. struct afs_dir_page {
  101. union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)];
  102. };
  103. struct afs_lookup_cookie {
  104. struct afs_fid fid;
  105. const char *name;
  106. size_t nlen;
  107. int found;
  108. };
  109. /*
  110. * check that a directory page is valid
  111. */
  112. static inline void afs_dir_check_page(struct inode *dir, struct page *page)
  113. {
  114. struct afs_dir_page *dbuf;
  115. loff_t latter;
  116. int tmp, qty;
  117. #if 0
  118. /* check the page count */
  119. qty = desc.size / sizeof(dbuf->blocks[0]);
  120. if (qty == 0)
  121. goto error;
  122. if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) {
  123. printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n",
  124. __FUNCTION__, dir->i_ino, qty,
  125. ntohs(dbuf->blocks[0].pagehdr.npages));
  126. goto error;
  127. }
  128. #endif
  129. /* determine how many magic numbers there should be in this page */
  130. latter = dir->i_size - page_offset(page);
  131. if (latter >= PAGE_SIZE)
  132. qty = PAGE_SIZE;
  133. else
  134. qty = latter;
  135. qty /= sizeof(union afs_dir_block);
  136. /* check them */
  137. dbuf = page_address(page);
  138. for (tmp = 0; tmp < qty; tmp++) {
  139. if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) {
  140. printk("kAFS: %s(%lu): bad magic %d/%d is %04hx\n",
  141. __FUNCTION__, dir->i_ino, tmp, qty,
  142. ntohs(dbuf->blocks[tmp].pagehdr.magic));
  143. goto error;
  144. }
  145. }
  146. SetPageChecked(page);
  147. return;
  148. error:
  149. SetPageChecked(page);
  150. SetPageError(page);
  151. }
  152. /*
  153. * discard a page cached in the pagecache
  154. */
  155. static inline void afs_dir_put_page(struct page *page)
  156. {
  157. kunmap(page);
  158. page_cache_release(page);
  159. }
  160. /*
  161. * get a page into the pagecache
  162. */
  163. static struct page *afs_dir_get_page(struct inode *dir, unsigned long index,
  164. struct key *key)
  165. {
  166. struct page *page;
  167. struct file file = {
  168. .private_data = key,
  169. };
  170. _enter("{%lu},%lu", dir->i_ino, index);
  171. page = read_mapping_page(dir->i_mapping, index, &file);
  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. * look up an entry in a directory
  408. */
  409. static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
  410. struct nameidata *nd)
  411. {
  412. struct afs_vnode *vnode;
  413. struct afs_fid fid;
  414. struct inode *inode;
  415. struct key *key;
  416. int ret;
  417. vnode = AFS_FS_I(dir);
  418. _enter("{%x:%u},%p{%s},",
  419. vnode->fid.vid, vnode->fid.vnode, dentry, dentry->d_name.name);
  420. ASSERTCMP(dentry->d_inode, ==, NULL);
  421. if (dentry->d_name.len >= AFSNAMEMAX) {
  422. _leave(" = -ENAMETOOLONG");
  423. return ERR_PTR(-ENAMETOOLONG);
  424. }
  425. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  426. _leave(" = -ESTALE");
  427. return ERR_PTR(-ESTALE);
  428. }
  429. key = afs_request_key(vnode->volume->cell);
  430. if (IS_ERR(key)) {
  431. _leave(" = %ld [key]", PTR_ERR(key));
  432. return ERR_PTR(PTR_ERR(key));
  433. }
  434. ret = afs_validate(vnode, key);
  435. if (ret < 0) {
  436. key_put(key);
  437. _leave(" = %d [val]", ret);
  438. return ERR_PTR(ret);
  439. }
  440. ret = afs_do_lookup(dir, dentry, &fid, key);
  441. if (ret < 0) {
  442. key_put(key);
  443. if (ret == -ENOENT) {
  444. d_add(dentry, NULL);
  445. _leave(" = NULL [negative]");
  446. return NULL;
  447. }
  448. _leave(" = %d [do]", ret);
  449. return ERR_PTR(ret);
  450. }
  451. dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version;
  452. /* instantiate the dentry */
  453. inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL);
  454. key_put(key);
  455. if (IS_ERR(inode)) {
  456. _leave(" = %ld", PTR_ERR(inode));
  457. return ERR_PTR(PTR_ERR(inode));
  458. }
  459. dentry->d_op = &afs_fs_dentry_operations;
  460. d_add(dentry, inode);
  461. _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%lu }",
  462. fid.vnode,
  463. fid.unique,
  464. dentry->d_inode->i_ino,
  465. dentry->d_inode->i_version);
  466. return NULL;
  467. }
  468. /*
  469. * check that a dentry lookup hit has found a valid entry
  470. * - NOTE! the hit can be a negative hit too, so we can't assume we have an
  471. * inode
  472. */
  473. static int afs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
  474. {
  475. struct afs_vnode *vnode, *dir;
  476. struct afs_fid fid;
  477. struct dentry *parent;
  478. struct key *key;
  479. void *dir_version;
  480. int ret;
  481. vnode = AFS_FS_I(dentry->d_inode);
  482. if (dentry->d_inode)
  483. _enter("{v={%x:%u} n=%s fl=%lx},",
  484. vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
  485. vnode->flags);
  486. else
  487. _enter("{neg n=%s}", dentry->d_name.name);
  488. key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
  489. if (IS_ERR(key))
  490. key = NULL;
  491. /* lock down the parent dentry so we can peer at it */
  492. parent = dget_parent(dentry);
  493. if (!parent->d_inode)
  494. goto out_bad;
  495. dir = AFS_FS_I(parent->d_inode);
  496. /* validate the parent directory */
  497. if (test_bit(AFS_VNODE_MODIFIED, &dir->flags))
  498. afs_validate(dir, key);
  499. if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
  500. _debug("%s: parent dir deleted", dentry->d_name.name);
  501. goto out_bad;
  502. }
  503. dir_version = (void *) (unsigned long) dir->status.data_version;
  504. if (dentry->d_fsdata == dir_version)
  505. goto out_valid; /* the dir contents are unchanged */
  506. _debug("dir modified");
  507. /* search the directory for this vnode */
  508. ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key);
  509. switch (ret) {
  510. case 0:
  511. /* the filename maps to something */
  512. if (!dentry->d_inode)
  513. goto out_bad;
  514. if (is_bad_inode(dentry->d_inode)) {
  515. printk("kAFS: afs_d_revalidate: %s/%s has bad inode\n",
  516. parent->d_name.name, dentry->d_name.name);
  517. goto out_bad;
  518. }
  519. /* if the vnode ID has changed, then the dirent points to a
  520. * different file */
  521. if (fid.vnode != vnode->fid.vnode) {
  522. _debug("%s: dirent changed [%u != %u]",
  523. dentry->d_name.name, fid.vnode,
  524. vnode->fid.vnode);
  525. goto not_found;
  526. }
  527. /* if the vnode ID uniqifier has changed, then the file has
  528. * been deleted and replaced, and the original vnode ID has
  529. * been reused */
  530. if (fid.unique != vnode->fid.unique) {
  531. _debug("%s: file deleted (uq %u -> %u I:%lu)",
  532. dentry->d_name.name, fid.unique,
  533. vnode->fid.unique, dentry->d_inode->i_version);
  534. spin_lock(&vnode->lock);
  535. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  536. spin_unlock(&vnode->lock);
  537. goto not_found;
  538. }
  539. goto out_valid;
  540. case -ENOENT:
  541. /* the filename is unknown */
  542. _debug("%s: dirent not found", dentry->d_name.name);
  543. if (dentry->d_inode)
  544. goto not_found;
  545. goto out_valid;
  546. default:
  547. _debug("failed to iterate dir %s: %d",
  548. parent->d_name.name, ret);
  549. goto out_bad;
  550. }
  551. out_valid:
  552. dentry->d_fsdata = dir_version;
  553. out_skip:
  554. dput(parent);
  555. key_put(key);
  556. _leave(" = 1 [valid]");
  557. return 1;
  558. /* the dirent, if it exists, now points to a different vnode */
  559. not_found:
  560. spin_lock(&dentry->d_lock);
  561. dentry->d_flags |= DCACHE_NFSFS_RENAMED;
  562. spin_unlock(&dentry->d_lock);
  563. out_bad:
  564. if (dentry->d_inode) {
  565. /* don't unhash if we have submounts */
  566. if (have_submounts(dentry))
  567. goto out_skip;
  568. }
  569. _debug("dropping dentry %s/%s",
  570. parent->d_name.name, dentry->d_name.name);
  571. shrink_dcache_parent(dentry);
  572. d_drop(dentry);
  573. dput(parent);
  574. key_put(key);
  575. _leave(" = 0 [bad]");
  576. return 0;
  577. }
  578. /*
  579. * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
  580. * sleep)
  581. * - called from dput() when d_count is going to 0.
  582. * - return 1 to request dentry be unhashed, 0 otherwise
  583. */
  584. static int afs_d_delete(struct dentry *dentry)
  585. {
  586. _enter("%s", dentry->d_name.name);
  587. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  588. goto zap;
  589. if (dentry->d_inode &&
  590. test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dentry->d_inode)->flags))
  591. goto zap;
  592. _leave(" = 0 [keep]");
  593. return 0;
  594. zap:
  595. _leave(" = 1 [zap]");
  596. return 1;
  597. }
  598. /*
  599. * handle dentry release
  600. */
  601. static void afs_d_release(struct dentry *dentry)
  602. {
  603. _enter("%s", dentry->d_name.name);
  604. }
  605. /*
  606. * create a directory on an AFS filesystem
  607. */
  608. static int afs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  609. {
  610. struct afs_file_status status;
  611. struct afs_callback cb;
  612. struct afs_server *server;
  613. struct afs_vnode *dvnode, *vnode;
  614. struct afs_fid fid;
  615. struct inode *inode;
  616. struct key *key;
  617. int ret;
  618. dvnode = AFS_FS_I(dir);
  619. _enter("{%x:%u},{%s},%o",
  620. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
  621. ret = -ENAMETOOLONG;
  622. if (dentry->d_name.len >= AFSNAMEMAX)
  623. goto error;
  624. key = afs_request_key(dvnode->volume->cell);
  625. if (IS_ERR(key)) {
  626. ret = PTR_ERR(key);
  627. goto error;
  628. }
  629. mode |= S_IFDIR;
  630. ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
  631. mode, &fid, &status, &cb, &server);
  632. if (ret < 0)
  633. goto mkdir_error;
  634. inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
  635. if (IS_ERR(inode)) {
  636. /* ENOMEM at a really inconvenient time - just abandon the new
  637. * directory on the server */
  638. ret = PTR_ERR(inode);
  639. goto iget_error;
  640. }
  641. /* apply the status report we've got for the new vnode */
  642. vnode = AFS_FS_I(inode);
  643. spin_lock(&vnode->lock);
  644. vnode->update_cnt++;
  645. spin_unlock(&vnode->lock);
  646. afs_vnode_finalise_status_update(vnode, server);
  647. afs_put_server(server);
  648. d_instantiate(dentry, inode);
  649. if (d_unhashed(dentry)) {
  650. _debug("not hashed");
  651. d_rehash(dentry);
  652. }
  653. key_put(key);
  654. _leave(" = 0");
  655. return 0;
  656. iget_error:
  657. afs_put_server(server);
  658. mkdir_error:
  659. key_put(key);
  660. error:
  661. d_drop(dentry);
  662. _leave(" = %d", ret);
  663. return ret;
  664. }
  665. /*
  666. * remove a directory from an AFS filesystem
  667. */
  668. static int afs_rmdir(struct inode *dir, struct dentry *dentry)
  669. {
  670. struct afs_vnode *dvnode, *vnode;
  671. struct key *key;
  672. int ret;
  673. dvnode = AFS_FS_I(dir);
  674. _enter("{%x:%u},{%s}",
  675. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
  676. ret = -ENAMETOOLONG;
  677. if (dentry->d_name.len >= AFSNAMEMAX)
  678. goto error;
  679. key = afs_request_key(dvnode->volume->cell);
  680. if (IS_ERR(key)) {
  681. ret = PTR_ERR(key);
  682. goto error;
  683. }
  684. ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, true);
  685. if (ret < 0)
  686. goto rmdir_error;
  687. if (dentry->d_inode) {
  688. vnode = AFS_FS_I(dentry->d_inode);
  689. clear_nlink(&vnode->vfs_inode);
  690. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  691. afs_discard_callback_on_delete(vnode);
  692. }
  693. key_put(key);
  694. _leave(" = 0");
  695. return 0;
  696. rmdir_error:
  697. key_put(key);
  698. error:
  699. _leave(" = %d", ret);
  700. return ret;
  701. }
  702. /*
  703. * remove a file from an AFS filesystem
  704. */
  705. static int afs_unlink(struct inode *dir, struct dentry *dentry)
  706. {
  707. struct afs_vnode *dvnode, *vnode;
  708. struct key *key;
  709. int ret;
  710. dvnode = AFS_FS_I(dir);
  711. _enter("{%x:%u},{%s}",
  712. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name);
  713. ret = -ENAMETOOLONG;
  714. if (dentry->d_name.len >= AFSNAMEMAX)
  715. goto error;
  716. key = afs_request_key(dvnode->volume->cell);
  717. if (IS_ERR(key)) {
  718. ret = PTR_ERR(key);
  719. goto error;
  720. }
  721. if (dentry->d_inode) {
  722. vnode = AFS_FS_I(dentry->d_inode);
  723. /* make sure we have a callback promise on the victim */
  724. ret = afs_validate(vnode, key);
  725. if (ret < 0)
  726. goto error;
  727. }
  728. ret = afs_vnode_remove(dvnode, key, dentry->d_name.name, false);
  729. if (ret < 0)
  730. goto remove_error;
  731. if (dentry->d_inode) {
  732. /* if the file wasn't deleted due to excess hard links, the
  733. * fileserver will break the callback promise on the file - if
  734. * it had one - before it returns to us, and if it was deleted,
  735. * it won't
  736. *
  737. * however, if we didn't have a callback promise outstanding,
  738. * or it was outstanding on a different server, then it won't
  739. * break it either...
  740. */
  741. vnode = AFS_FS_I(dentry->d_inode);
  742. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  743. _debug("AFS_VNODE_DELETED");
  744. if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags))
  745. _debug("AFS_VNODE_CB_BROKEN");
  746. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  747. ret = afs_validate(vnode, key);
  748. _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
  749. }
  750. key_put(key);
  751. _leave(" = 0");
  752. return 0;
  753. remove_error:
  754. key_put(key);
  755. error:
  756. _leave(" = %d", ret);
  757. return ret;
  758. }
  759. /*
  760. * create a regular file on an AFS filesystem
  761. */
  762. static int afs_create(struct inode *dir, struct dentry *dentry, int mode,
  763. struct nameidata *nd)
  764. {
  765. struct afs_file_status status;
  766. struct afs_callback cb;
  767. struct afs_server *server;
  768. struct afs_vnode *dvnode, *vnode;
  769. struct afs_fid fid;
  770. struct inode *inode;
  771. struct key *key;
  772. int ret;
  773. dvnode = AFS_FS_I(dir);
  774. _enter("{%x:%u},{%s},%o,",
  775. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name, mode);
  776. ret = -ENAMETOOLONG;
  777. if (dentry->d_name.len >= AFSNAMEMAX)
  778. goto error;
  779. key = afs_request_key(dvnode->volume->cell);
  780. if (IS_ERR(key)) {
  781. ret = PTR_ERR(key);
  782. goto error;
  783. }
  784. mode |= S_IFREG;
  785. ret = afs_vnode_create(dvnode, key, dentry->d_name.name,
  786. mode, &fid, &status, &cb, &server);
  787. if (ret < 0)
  788. goto create_error;
  789. inode = afs_iget(dir->i_sb, key, &fid, &status, &cb);
  790. if (IS_ERR(inode)) {
  791. /* ENOMEM at a really inconvenient time - just abandon the new
  792. * directory on the server */
  793. ret = PTR_ERR(inode);
  794. goto iget_error;
  795. }
  796. /* apply the status report we've got for the new vnode */
  797. vnode = AFS_FS_I(inode);
  798. spin_lock(&vnode->lock);
  799. vnode->update_cnt++;
  800. spin_unlock(&vnode->lock);
  801. afs_vnode_finalise_status_update(vnode, server);
  802. afs_put_server(server);
  803. d_instantiate(dentry, inode);
  804. if (d_unhashed(dentry)) {
  805. _debug("not hashed");
  806. d_rehash(dentry);
  807. }
  808. key_put(key);
  809. _leave(" = 0");
  810. return 0;
  811. iget_error:
  812. afs_put_server(server);
  813. create_error:
  814. key_put(key);
  815. error:
  816. d_drop(dentry);
  817. _leave(" = %d", ret);
  818. return ret;
  819. }
  820. /*
  821. * create a hard link between files in an AFS filesystem
  822. */
  823. static int afs_link(struct dentry *from, struct inode *dir,
  824. struct dentry *dentry)
  825. {
  826. struct afs_vnode *dvnode, *vnode;
  827. struct key *key;
  828. int ret;
  829. vnode = AFS_FS_I(from->d_inode);
  830. dvnode = AFS_FS_I(dir);
  831. _enter("{%x:%u},{%x:%u},{%s}",
  832. vnode->fid.vid, vnode->fid.vnode,
  833. dvnode->fid.vid, dvnode->fid.vnode,
  834. dentry->d_name.name);
  835. ret = -ENAMETOOLONG;
  836. if (dentry->d_name.len >= AFSNAMEMAX)
  837. goto error;
  838. key = afs_request_key(dvnode->volume->cell);
  839. if (IS_ERR(key)) {
  840. ret = PTR_ERR(key);
  841. goto error;
  842. }
  843. ret = afs_vnode_link(dvnode, vnode, key, dentry->d_name.name);
  844. if (ret < 0)
  845. goto link_error;
  846. atomic_inc(&vnode->vfs_inode.i_count);
  847. d_instantiate(dentry, &vnode->vfs_inode);
  848. key_put(key);
  849. _leave(" = 0");
  850. return 0;
  851. link_error:
  852. key_put(key);
  853. error:
  854. d_drop(dentry);
  855. _leave(" = %d", ret);
  856. return ret;
  857. }
  858. /*
  859. * create a symlink in an AFS filesystem
  860. */
  861. static int afs_symlink(struct inode *dir, struct dentry *dentry,
  862. const char *content)
  863. {
  864. struct afs_file_status status;
  865. struct afs_server *server;
  866. struct afs_vnode *dvnode, *vnode;
  867. struct afs_fid fid;
  868. struct inode *inode;
  869. struct key *key;
  870. int ret;
  871. dvnode = AFS_FS_I(dir);
  872. _enter("{%x:%u},{%s},%s",
  873. dvnode->fid.vid, dvnode->fid.vnode, dentry->d_name.name,
  874. content);
  875. ret = -ENAMETOOLONG;
  876. if (dentry->d_name.len >= AFSNAMEMAX)
  877. goto error;
  878. ret = -EINVAL;
  879. if (strlen(content) >= AFSPATHMAX)
  880. goto error;
  881. key = afs_request_key(dvnode->volume->cell);
  882. if (IS_ERR(key)) {
  883. ret = PTR_ERR(key);
  884. goto error;
  885. }
  886. ret = afs_vnode_symlink(dvnode, key, dentry->d_name.name, content,
  887. &fid, &status, &server);
  888. if (ret < 0)
  889. goto create_error;
  890. inode = afs_iget(dir->i_sb, key, &fid, &status, NULL);
  891. if (IS_ERR(inode)) {
  892. /* ENOMEM at a really inconvenient time - just abandon the new
  893. * directory on the server */
  894. ret = PTR_ERR(inode);
  895. goto iget_error;
  896. }
  897. /* apply the status report we've got for the new vnode */
  898. vnode = AFS_FS_I(inode);
  899. spin_lock(&vnode->lock);
  900. vnode->update_cnt++;
  901. spin_unlock(&vnode->lock);
  902. afs_vnode_finalise_status_update(vnode, server);
  903. afs_put_server(server);
  904. d_instantiate(dentry, inode);
  905. if (d_unhashed(dentry)) {
  906. _debug("not hashed");
  907. d_rehash(dentry);
  908. }
  909. key_put(key);
  910. _leave(" = 0");
  911. return 0;
  912. iget_error:
  913. afs_put_server(server);
  914. create_error:
  915. key_put(key);
  916. error:
  917. d_drop(dentry);
  918. _leave(" = %d", ret);
  919. return ret;
  920. }
  921. /*
  922. * rename a file in an AFS filesystem and/or move it between directories
  923. */
  924. static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
  925. struct inode *new_dir, struct dentry *new_dentry)
  926. {
  927. struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
  928. struct key *key;
  929. int ret;
  930. vnode = AFS_FS_I(old_dentry->d_inode);
  931. orig_dvnode = AFS_FS_I(old_dir);
  932. new_dvnode = AFS_FS_I(new_dir);
  933. _enter("{%x:%u},{%x:%u},{%x:%u},{%s}",
  934. orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
  935. vnode->fid.vid, vnode->fid.vnode,
  936. new_dvnode->fid.vid, new_dvnode->fid.vnode,
  937. new_dentry->d_name.name);
  938. ret = -ENAMETOOLONG;
  939. if (new_dentry->d_name.len >= AFSNAMEMAX)
  940. goto error;
  941. key = afs_request_key(orig_dvnode->volume->cell);
  942. if (IS_ERR(key)) {
  943. ret = PTR_ERR(key);
  944. goto error;
  945. }
  946. ret = afs_vnode_rename(orig_dvnode, new_dvnode, key,
  947. old_dentry->d_name.name,
  948. new_dentry->d_name.name);
  949. if (ret < 0)
  950. goto rename_error;
  951. key_put(key);
  952. _leave(" = 0");
  953. return 0;
  954. rename_error:
  955. key_put(key);
  956. error:
  957. d_drop(new_dentry);
  958. _leave(" = %d", ret);
  959. return ret;
  960. }