dir.c 26 KB

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