dir.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Directory operations for Coda filesystem
  3. * Original version: (C) 1996 P. Braam and M. Callahan
  4. * Rewritten for Linux 2.1. (C) 1997 Carnegie Mellon University
  5. *
  6. * Carnegie Mellon encourages users to contribute improvements to
  7. * the Coda project. Contact Peter Braam (coda@cs.cmu.edu).
  8. */
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/time.h>
  12. #include <linux/fs.h>
  13. #include <linux/slab.h>
  14. #include <linux/file.h>
  15. #include <linux/stat.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/namei.h>
  20. #include <asm/uaccess.h>
  21. #include <linux/coda.h>
  22. #include <linux/coda_psdev.h>
  23. #include "coda_linux.h"
  24. #include "coda_cache.h"
  25. #include "coda_int.h"
  26. /* dir inode-ops */
  27. static int coda_create(struct inode *dir, struct dentry *new, umode_t mode, struct nameidata *nd);
  28. static struct dentry *coda_lookup(struct inode *dir, struct dentry *target, struct nameidata *nd);
  29. static int coda_link(struct dentry *old_dentry, struct inode *dir_inode,
  30. struct dentry *entry);
  31. static int coda_unlink(struct inode *dir_inode, struct dentry *entry);
  32. static int coda_symlink(struct inode *dir_inode, struct dentry *entry,
  33. const char *symname);
  34. static int coda_mkdir(struct inode *dir_inode, struct dentry *entry, umode_t mode);
  35. static int coda_rmdir(struct inode *dir_inode, struct dentry *entry);
  36. static int coda_rename(struct inode *old_inode, struct dentry *old_dentry,
  37. struct inode *new_inode, struct dentry *new_dentry);
  38. /* dir file-ops */
  39. static int coda_readdir(struct file *file, void *buf, filldir_t filldir);
  40. /* dentry ops */
  41. static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd);
  42. static int coda_dentry_delete(const struct dentry *);
  43. /* support routines */
  44. static int coda_venus_readdir(struct file *coda_file, void *buf,
  45. filldir_t filldir);
  46. /* same as fs/bad_inode.c */
  47. static int coda_return_EIO(void)
  48. {
  49. return -EIO;
  50. }
  51. #define CODA_EIO_ERROR ((void *) (coda_return_EIO))
  52. const struct dentry_operations coda_dentry_operations =
  53. {
  54. .d_revalidate = coda_dentry_revalidate,
  55. .d_delete = coda_dentry_delete,
  56. };
  57. const struct inode_operations coda_dir_inode_operations =
  58. {
  59. .create = coda_create,
  60. .lookup = coda_lookup,
  61. .link = coda_link,
  62. .unlink = coda_unlink,
  63. .symlink = coda_symlink,
  64. .mkdir = coda_mkdir,
  65. .rmdir = coda_rmdir,
  66. .mknod = CODA_EIO_ERROR,
  67. .rename = coda_rename,
  68. .permission = coda_permission,
  69. .getattr = coda_getattr,
  70. .setattr = coda_setattr,
  71. };
  72. const struct file_operations coda_dir_operations = {
  73. .llseek = generic_file_llseek,
  74. .read = generic_read_dir,
  75. .readdir = coda_readdir,
  76. .open = coda_open,
  77. .release = coda_release,
  78. .fsync = coda_fsync,
  79. };
  80. /* inode operations for directories */
  81. /* access routines: lookup, readlink, permission */
  82. static struct dentry *coda_lookup(struct inode *dir, struct dentry *entry, struct nameidata *nd)
  83. {
  84. struct super_block *sb = dir->i_sb;
  85. const char *name = entry->d_name.name;
  86. size_t length = entry->d_name.len;
  87. struct inode *inode;
  88. int type = 0;
  89. if (length > CODA_MAXNAMLEN) {
  90. printk(KERN_ERR "name too long: lookup, %s (%*s)\n",
  91. coda_i2s(dir), (int)length, name);
  92. return ERR_PTR(-ENAMETOOLONG);
  93. }
  94. /* control object, create inode on the fly */
  95. if (coda_isroot(dir) && coda_iscontrol(name, length)) {
  96. inode = coda_cnode_makectl(sb);
  97. type = CODA_NOCACHE;
  98. } else {
  99. struct CodaFid fid = { { 0, } };
  100. int error = venus_lookup(sb, coda_i2f(dir), name, length,
  101. &type, &fid);
  102. inode = !error ? coda_cnode_make(&fid, sb) : ERR_PTR(error);
  103. }
  104. if (!IS_ERR(inode) && (type & CODA_NOCACHE))
  105. coda_flag_inode(inode, C_VATTR | C_PURGE);
  106. if (inode == ERR_PTR(-ENOENT))
  107. inode = NULL;
  108. return d_splice_alias(inode, entry);
  109. }
  110. int coda_permission(struct inode *inode, int mask)
  111. {
  112. int error;
  113. if (mask & MAY_NOT_BLOCK)
  114. return -ECHILD;
  115. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  116. if (!mask)
  117. return 0;
  118. if ((mask & MAY_EXEC) && !execute_ok(inode))
  119. return -EACCES;
  120. if (coda_cache_check(inode, mask))
  121. return 0;
  122. error = venus_access(inode->i_sb, coda_i2f(inode), mask);
  123. if (!error)
  124. coda_cache_enter(inode, mask);
  125. return error;
  126. }
  127. static inline void coda_dir_update_mtime(struct inode *dir)
  128. {
  129. #ifdef REQUERY_VENUS_FOR_MTIME
  130. /* invalidate the directory cnode's attributes so we refetch the
  131. * attributes from venus next time the inode is referenced */
  132. coda_flag_inode(dir, C_VATTR);
  133. #else
  134. /* optimistically we can also act as if our nose bleeds. The
  135. * granularity of the mtime is coarse anyways so we might actually be
  136. * right most of the time. Note: we only do this for directories. */
  137. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  138. #endif
  139. }
  140. /* we have to wrap inc_nlink/drop_nlink because sometimes userspace uses a
  141. * trick to fool GNU find's optimizations. If we can't be sure of the link
  142. * (because of volume mount points) we set i_nlink to 1 which forces find
  143. * to consider every child as a possible directory. We should also never
  144. * see an increment or decrement for deleted directories where i_nlink == 0 */
  145. static inline void coda_dir_inc_nlink(struct inode *dir)
  146. {
  147. if (dir->i_nlink >= 2)
  148. inc_nlink(dir);
  149. }
  150. static inline void coda_dir_drop_nlink(struct inode *dir)
  151. {
  152. if (dir->i_nlink > 2)
  153. drop_nlink(dir);
  154. }
  155. /* creation routines: create, mknod, mkdir, link, symlink */
  156. static int coda_create(struct inode *dir, struct dentry *de, umode_t mode, struct nameidata *nd)
  157. {
  158. int error;
  159. const char *name=de->d_name.name;
  160. int length=de->d_name.len;
  161. struct inode *inode;
  162. struct CodaFid newfid;
  163. struct coda_vattr attrs;
  164. if (coda_isroot(dir) && coda_iscontrol(name, length))
  165. return -EPERM;
  166. error = venus_create(dir->i_sb, coda_i2f(dir), name, length,
  167. 0, mode, &newfid, &attrs);
  168. if (error)
  169. goto err_out;
  170. inode = coda_iget(dir->i_sb, &newfid, &attrs);
  171. if (IS_ERR(inode)) {
  172. error = PTR_ERR(inode);
  173. goto err_out;
  174. }
  175. /* invalidate the directory cnode's attributes */
  176. coda_dir_update_mtime(dir);
  177. d_instantiate(de, inode);
  178. return 0;
  179. err_out:
  180. d_drop(de);
  181. return error;
  182. }
  183. static int coda_mkdir(struct inode *dir, struct dentry *de, umode_t mode)
  184. {
  185. struct inode *inode;
  186. struct coda_vattr attrs;
  187. const char *name = de->d_name.name;
  188. int len = de->d_name.len;
  189. int error;
  190. struct CodaFid newfid;
  191. if (coda_isroot(dir) && coda_iscontrol(name, len))
  192. return -EPERM;
  193. attrs.va_mode = mode;
  194. error = venus_mkdir(dir->i_sb, coda_i2f(dir),
  195. name, len, &newfid, &attrs);
  196. if (error)
  197. goto err_out;
  198. inode = coda_iget(dir->i_sb, &newfid, &attrs);
  199. if (IS_ERR(inode)) {
  200. error = PTR_ERR(inode);
  201. goto err_out;
  202. }
  203. /* invalidate the directory cnode's attributes */
  204. coda_dir_inc_nlink(dir);
  205. coda_dir_update_mtime(dir);
  206. d_instantiate(de, inode);
  207. return 0;
  208. err_out:
  209. d_drop(de);
  210. return error;
  211. }
  212. /* try to make de an entry in dir_inodde linked to source_de */
  213. static int coda_link(struct dentry *source_de, struct inode *dir_inode,
  214. struct dentry *de)
  215. {
  216. struct inode *inode = source_de->d_inode;
  217. const char * name = de->d_name.name;
  218. int len = de->d_name.len;
  219. int error;
  220. if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
  221. return -EPERM;
  222. error = venus_link(dir_inode->i_sb, coda_i2f(inode),
  223. coda_i2f(dir_inode), (const char *)name, len);
  224. if (error) {
  225. d_drop(de);
  226. return error;
  227. }
  228. coda_dir_update_mtime(dir_inode);
  229. ihold(inode);
  230. d_instantiate(de, inode);
  231. inc_nlink(inode);
  232. return 0;
  233. }
  234. static int coda_symlink(struct inode *dir_inode, struct dentry *de,
  235. const char *symname)
  236. {
  237. const char *name = de->d_name.name;
  238. int len = de->d_name.len;
  239. int symlen;
  240. int error;
  241. if (coda_isroot(dir_inode) && coda_iscontrol(name, len))
  242. return -EPERM;
  243. symlen = strlen(symname);
  244. if (symlen > CODA_MAXPATHLEN)
  245. return -ENAMETOOLONG;
  246. /*
  247. * This entry is now negative. Since we do not create
  248. * an inode for the entry we have to drop it.
  249. */
  250. d_drop(de);
  251. error = venus_symlink(dir_inode->i_sb, coda_i2f(dir_inode), name, len,
  252. symname, symlen);
  253. /* mtime is no good anymore */
  254. if (!error)
  255. coda_dir_update_mtime(dir_inode);
  256. return error;
  257. }
  258. /* destruction routines: unlink, rmdir */
  259. static int coda_unlink(struct inode *dir, struct dentry *de)
  260. {
  261. int error;
  262. const char *name = de->d_name.name;
  263. int len = de->d_name.len;
  264. error = venus_remove(dir->i_sb, coda_i2f(dir), name, len);
  265. if (error)
  266. return error;
  267. coda_dir_update_mtime(dir);
  268. drop_nlink(de->d_inode);
  269. return 0;
  270. }
  271. static int coda_rmdir(struct inode *dir, struct dentry *de)
  272. {
  273. const char *name = de->d_name.name;
  274. int len = de->d_name.len;
  275. int error;
  276. error = venus_rmdir(dir->i_sb, coda_i2f(dir), name, len);
  277. if (!error) {
  278. /* VFS may delete the child */
  279. if (de->d_inode)
  280. clear_nlink(de->d_inode);
  281. /* fix the link count of the parent */
  282. coda_dir_drop_nlink(dir);
  283. coda_dir_update_mtime(dir);
  284. }
  285. return error;
  286. }
  287. /* rename */
  288. static int coda_rename(struct inode *old_dir, struct dentry *old_dentry,
  289. struct inode *new_dir, struct dentry *new_dentry)
  290. {
  291. const char *old_name = old_dentry->d_name.name;
  292. const char *new_name = new_dentry->d_name.name;
  293. int old_length = old_dentry->d_name.len;
  294. int new_length = new_dentry->d_name.len;
  295. int error;
  296. error = venus_rename(old_dir->i_sb, coda_i2f(old_dir),
  297. coda_i2f(new_dir), old_length, new_length,
  298. (const char *) old_name, (const char *)new_name);
  299. if (!error) {
  300. if (new_dentry->d_inode) {
  301. if (S_ISDIR(new_dentry->d_inode->i_mode)) {
  302. coda_dir_drop_nlink(old_dir);
  303. coda_dir_inc_nlink(new_dir);
  304. }
  305. coda_dir_update_mtime(old_dir);
  306. coda_dir_update_mtime(new_dir);
  307. coda_flag_inode(new_dentry->d_inode, C_VATTR);
  308. } else {
  309. coda_flag_inode(old_dir, C_VATTR);
  310. coda_flag_inode(new_dir, C_VATTR);
  311. }
  312. }
  313. return error;
  314. }
  315. /* file operations for directories */
  316. static int coda_readdir(struct file *coda_file, void *buf, filldir_t filldir)
  317. {
  318. struct coda_file_info *cfi;
  319. struct file *host_file;
  320. int ret;
  321. cfi = CODA_FTOC(coda_file);
  322. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  323. host_file = cfi->cfi_container;
  324. if (!host_file->f_op)
  325. return -ENOTDIR;
  326. if (host_file->f_op->readdir) {
  327. /* potemkin case: we were handed a directory inode.
  328. * We can't use vfs_readdir because we have to keep the file
  329. * position in sync between the coda_file and the host_file.
  330. * and as such we need grab the inode mutex. */
  331. struct inode *host_inode = host_file->f_path.dentry->d_inode;
  332. mutex_lock(&host_inode->i_mutex);
  333. host_file->f_pos = coda_file->f_pos;
  334. ret = -ENOENT;
  335. if (!IS_DEADDIR(host_inode)) {
  336. ret = host_file->f_op->readdir(host_file, buf, filldir);
  337. file_accessed(host_file);
  338. }
  339. coda_file->f_pos = host_file->f_pos;
  340. mutex_unlock(&host_inode->i_mutex);
  341. } else if (host_file->f_op->iterate) {
  342. struct inode *host_inode = file_inode(host_file);
  343. struct dir_context *ctx = buf;
  344. mutex_lock(&host_inode->i_mutex);
  345. ret = -ENOENT;
  346. if (!IS_DEADDIR(host_inode)) {
  347. ret = host_file->f_op->iterate(host_file, ctx);
  348. file_accessed(host_file);
  349. }
  350. mutex_unlock(&host_inode->i_mutex);
  351. coda_file->f_pos = ctx->pos;
  352. } else /* Venus: we must read Venus dirents from a file */
  353. ret = coda_venus_readdir(coda_file, buf, filldir);
  354. return ret;
  355. }
  356. static inline unsigned int CDT2DT(unsigned char cdt)
  357. {
  358. unsigned int dt;
  359. switch(cdt) {
  360. case CDT_UNKNOWN: dt = DT_UNKNOWN; break;
  361. case CDT_FIFO: dt = DT_FIFO; break;
  362. case CDT_CHR: dt = DT_CHR; break;
  363. case CDT_DIR: dt = DT_DIR; break;
  364. case CDT_BLK: dt = DT_BLK; break;
  365. case CDT_REG: dt = DT_REG; break;
  366. case CDT_LNK: dt = DT_LNK; break;
  367. case CDT_SOCK: dt = DT_SOCK; break;
  368. case CDT_WHT: dt = DT_WHT; break;
  369. default: dt = DT_UNKNOWN; break;
  370. }
  371. return dt;
  372. }
  373. /* support routines */
  374. static int coda_venus_readdir(struct file *coda_file, void *buf,
  375. filldir_t filldir)
  376. {
  377. int result = 0; /* # of entries returned */
  378. struct coda_file_info *cfi;
  379. struct coda_inode_info *cii;
  380. struct file *host_file;
  381. struct dentry *de;
  382. struct venus_dirent *vdir;
  383. unsigned long vdir_size = offsetof(struct venus_dirent, d_name);
  384. unsigned int type;
  385. struct qstr name;
  386. ino_t ino;
  387. int ret;
  388. cfi = CODA_FTOC(coda_file);
  389. BUG_ON(!cfi || cfi->cfi_magic != CODA_MAGIC);
  390. host_file = cfi->cfi_container;
  391. de = coda_file->f_path.dentry;
  392. cii = ITOC(de->d_inode);
  393. vdir = kmalloc(sizeof(*vdir), GFP_KERNEL);
  394. if (!vdir) return -ENOMEM;
  395. if (coda_file->f_pos == 0) {
  396. ret = filldir(buf, ".", 1, 0, de->d_inode->i_ino, DT_DIR);
  397. if (ret < 0)
  398. goto out;
  399. result++;
  400. coda_file->f_pos++;
  401. }
  402. if (coda_file->f_pos == 1) {
  403. ret = filldir(buf, "..", 2, 1, parent_ino(de), DT_DIR);
  404. if (ret < 0)
  405. goto out;
  406. result++;
  407. coda_file->f_pos++;
  408. }
  409. while (1) {
  410. /* read entries from the directory file */
  411. ret = kernel_read(host_file, coda_file->f_pos - 2, (char *)vdir,
  412. sizeof(*vdir));
  413. if (ret < 0) {
  414. printk(KERN_ERR "coda readdir: read dir %s failed %d\n",
  415. coda_f2s(&cii->c_fid), ret);
  416. break;
  417. }
  418. if (ret == 0) break; /* end of directory file reached */
  419. /* catch truncated reads */
  420. if (ret < vdir_size || ret < vdir_size + vdir->d_namlen) {
  421. printk(KERN_ERR "coda readdir: short read on %s\n",
  422. coda_f2s(&cii->c_fid));
  423. ret = -EBADF;
  424. break;
  425. }
  426. /* validate whether the directory file actually makes sense */
  427. if (vdir->d_reclen < vdir_size + vdir->d_namlen) {
  428. printk(KERN_ERR "coda readdir: invalid dir %s\n",
  429. coda_f2s(&cii->c_fid));
  430. ret = -EBADF;
  431. break;
  432. }
  433. name.len = vdir->d_namlen;
  434. name.name = vdir->d_name;
  435. /* Make sure we skip '.' and '..', we already got those */
  436. if (name.name[0] == '.' && (name.len == 1 ||
  437. (vdir->d_name[1] == '.' && name.len == 2)))
  438. vdir->d_fileno = name.len = 0;
  439. /* skip null entries */
  440. if (vdir->d_fileno && name.len) {
  441. /* try to look up this entry in the dcache, that way
  442. * userspace doesn't have to worry about breaking
  443. * getcwd by having mismatched inode numbers for
  444. * internal volume mountpoints. */
  445. ino = find_inode_number(de, &name);
  446. if (!ino) ino = vdir->d_fileno;
  447. type = CDT2DT(vdir->d_type);
  448. ret = filldir(buf, name.name, name.len,
  449. coda_file->f_pos, ino, type);
  450. /* failure means no space for filling in this round */
  451. if (ret < 0) break;
  452. result++;
  453. }
  454. /* we'll always have progress because d_reclen is unsigned and
  455. * we've already established it is non-zero. */
  456. coda_file->f_pos += vdir->d_reclen;
  457. }
  458. out:
  459. kfree(vdir);
  460. return result ? result : ret;
  461. }
  462. /* called when a cache lookup succeeds */
  463. static int coda_dentry_revalidate(struct dentry *de, struct nameidata *nd)
  464. {
  465. struct inode *inode;
  466. struct coda_inode_info *cii;
  467. if (nd->flags & LOOKUP_RCU)
  468. return -ECHILD;
  469. inode = de->d_inode;
  470. if (!inode || coda_isroot(inode))
  471. goto out;
  472. if (is_bad_inode(inode))
  473. goto bad;
  474. cii = ITOC(de->d_inode);
  475. if (!(cii->c_flags & (C_PURGE | C_FLUSH)))
  476. goto out;
  477. shrink_dcache_parent(de);
  478. /* propagate for a flush */
  479. if (cii->c_flags & C_FLUSH)
  480. coda_flag_inode_children(inode, C_FLUSH);
  481. if (de->d_count > 1)
  482. /* pretend it's valid, but don't change the flags */
  483. goto out;
  484. /* clear the flags. */
  485. spin_lock(&cii->c_lock);
  486. cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
  487. spin_unlock(&cii->c_lock);
  488. bad:
  489. return 0;
  490. out:
  491. return 1;
  492. }
  493. /*
  494. * This is the callback from dput() when d_count is going to 0.
  495. * We use this to unhash dentries with bad inodes.
  496. */
  497. static int coda_dentry_delete(const struct dentry * dentry)
  498. {
  499. int flags;
  500. if (!dentry->d_inode)
  501. return 0;
  502. flags = (ITOC(dentry->d_inode)->c_flags) & C_PURGE;
  503. if (is_bad_inode(dentry->d_inode) || flags) {
  504. return 1;
  505. }
  506. return 0;
  507. }
  508. /*
  509. * This is called when we want to check if the inode has
  510. * changed on the server. Coda makes this easy since the
  511. * cache manager Venus issues a downcall to the kernel when this
  512. * happens
  513. */
  514. int coda_revalidate_inode(struct dentry *dentry)
  515. {
  516. struct coda_vattr attr;
  517. int error;
  518. int old_mode;
  519. ino_t old_ino;
  520. struct inode *inode = dentry->d_inode;
  521. struct coda_inode_info *cii = ITOC(inode);
  522. if (!cii->c_flags)
  523. return 0;
  524. if (cii->c_flags & (C_VATTR | C_PURGE | C_FLUSH)) {
  525. error = venus_getattr(inode->i_sb, &(cii->c_fid), &attr);
  526. if (error)
  527. return -EIO;
  528. /* this inode may be lost if:
  529. - it's ino changed
  530. - type changes must be permitted for repair and
  531. missing mount points.
  532. */
  533. old_mode = inode->i_mode;
  534. old_ino = inode->i_ino;
  535. coda_vattr_to_iattr(inode, &attr);
  536. if ((old_mode & S_IFMT) != (inode->i_mode & S_IFMT)) {
  537. printk("Coda: inode %ld, fid %s changed type!\n",
  538. inode->i_ino, coda_f2s(&(cii->c_fid)));
  539. }
  540. /* the following can happen when a local fid is replaced
  541. with a global one, here we lose and declare the inode bad */
  542. if (inode->i_ino != old_ino)
  543. return -EIO;
  544. coda_flag_inode_children(inode, C_FLUSH);
  545. spin_lock(&cii->c_lock);
  546. cii->c_flags &= ~(C_VATTR | C_PURGE | C_FLUSH);
  547. spin_unlock(&cii->c_lock);
  548. }
  549. return 0;
  550. }