dir.c 16 KB

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