dir.c 27 KB

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